zoukankan      html  css  js  c++  java
  • 作业五 5.2 5.3

    实验五(5.2&5.3)

    一、结对伙伴

    林惠映   博客:http://www.cnblogs.com/LHYwin/

    二、分工

    首先我们两共同讨论出本次实验要完成的任务,然后按照任务逐步完善程序。

    三、代码

    1.源代码:

    import java.math.BigDecimal;
    import java.util.*;
    public class Unit {
        int num,a,b,c;
        public Unit() throws Exception
        {
        num = (int) (Math.random() * 6);
        a = (int) (Math.random() * 200) - 100;//-100~100
        b = (int) (Math.random() * 200) - 100;//-100~100
        c = (int) (Math.random() * 200) - 100;//-100~100
        if (num == 0)//num为0时,是加减法运算
        {
            addjian(a,b,c);  
        } 
        else if (num == 1)  //num为1表示减法运算
        {
            addchen(a,b,c);
            
        } 
        else if (num == 2) 
        {   //num为2表示乘法运算
            addchu(a,b,c);
        }
         else if (num == 3) 
         {   //num为3表示除法运算,并且要考虑除数不能为0的情况,因此b!=0
            
             jianchen(a,b,c);
         }
         else if(num==4)
         {
             jianchu(a,b,c);
         }
         else if(num==5)
         {
             chenchu(a,b,c);
         }
         } 
        public static int addjian(int a,int b,int c) throws Exception
        {
            if((b<-1000||b>1000)||(a<-1000||a>1000)||(c<-1000||c>1000))
            {
                throw new Exception("a,b的值超出范围");
            }
            return a+b-c;
        }
        public static int addchen(int a,int b,int c) throws Exception
        {
            
            if((b<-1000||b>1000)||(a<-1000||a>1000)||(c<-1000||c>1000))
            {
                throw new Exception("a或b或c的值超出范围");
            }
            return a+b*c;
        }
        public static float addchu(int a,int b,int c) throws Exception
        {
            float d,d1;
            if((b<-1000||b>1000)||(a<-1000||a>1000)||(c<-1000||c>1000))
            {
                throw new Exception("a,b的值超出范围");
            }
            if(c==0)
            {
                throw new Exception("除数不能为0");
            }
    
             d= a+(float)b/(float)c;  //将整型a,b强制转换为浮点型,保证结果正确
            BigDecimal bg = new BigDecimal(d);   //将除法结果保留小数点后两位
            d1 =(float)bg.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
            return d1;
        }
        public static int jianchen(int a,int b,int c) throws Exception
        {
            if((b<-1000||b>1000)||(a<-1000||a>1000)||(c<-1000||c>1000))
            {
                throw new Exception("a,b的值超出范围");
            }
            
            return a-b*c;
        }
        public static float jianchu(int a,int b,int c) throws Exception
        {
            float d,d1;
            if((b<-1000||b>1000)||(a<-1000||a>1000)||(c<-1000||c>1000))
            {
                throw new Exception("a,b的值超出范围");
            }
            if(c==0)
            {
                throw new Exception("除数不能为0");
            }
    
             d= a-(float)b/(float)c;  //将整型a,b强制转换为浮点型,保证结果正确
            BigDecimal bg = new BigDecimal(d);   //将除法结果保留小数点后两位
            d1 =(float)bg.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
            return d1;
        }
        public static float chenchu(int a,int b,int c) throws Exception
        {
            float d,d1;
            if((b<-1000||b>1000)||(a<-1000||a>1000)||(c<-1000||c>1000))
            {
                throw new Exception("a,b的值超出范围");
            }
            if(c==0)
            {
                throw new Exception("除数不能为0");
            }
    
             d= a*(float)b/(float)c;  //将整型a,b强制转换为浮点型,保证结果正确
            BigDecimal bg = new BigDecimal(d);   //将除法结果保留小数点后两位
            d1 =(float)bg.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
            return d1;
        }
    
    }

    2.单元测试代码:

    import static org.junit.Assert.*;
    import junit.framework.Assert;
    
    import org.junit.Before;
    import org.junit.Test;
    
    
    public class UnitTest {
        Unit unit;
        @Before
        public void setUp() throws Exception {
            unit=new Unit();
        }
    
        @Test
        public void testAddjian() {
            long Res=0;
            try
            {
            Res=unit.addjian(-2, -3,3);
            }
            catch(Exception e)
            {
                e.printStackTrace();
                Assert.fail("没有抛出异常,测试失败");
            }
                assertEquals(-8,Res);
                System.out.println("加减混合运算");
        }
    
    
        @Test
        public void testAddchen() {
            long Res=0;
            try
            {
            Res=unit.addchen(-2, -3,1);
            }
            catch(Exception e)
            {
                e.printStackTrace();
                Assert.fail("没有抛出异常,测试失败");
            }
                assertEquals(-5,Res);
                System.out.println("加乘混合运算");
        }
    
        @Test
        public void testAddchu() {
            float Res=0;
            String r1 = null;
            String r=null;
            try
            {
            Res=new Unit().addchu(10,3,4);
            r=String.valueOf(Res);
            r1=String.valueOf(10.75);
            }
            catch(Exception e)
            {
                e.printStackTrace();
                Assert.fail("没有抛出异常,测试失败");
            //fail("尚未实现");
            }
            Assert.assertEquals(r1,r);
            System.out.println("加除混合运算");
        }
        @Test
        public void testJianchen() {
            int Res=0;
            try
            {
            Res=unit.jianchen(-2, -3,3);
            }
            catch(Exception e)
            {
                e.printStackTrace();
                Assert.fail("没有抛出异常,测试失败");
            }
                assertEquals(7,Res);
                System.out.println("减乘混合运算");
        }
        @Test
        public void testJianchu() {
            float Res=0;
            String r1 = null;
            String r=null;
            try
            {
            Res=new Unit().jianchu(2,3,4);
            r=String.valueOf(Res);
            r1=String.valueOf(1.25);
            }
            catch(Exception e)
            {
                e.printStackTrace();
                Assert.fail("没有抛出异常,测试失败");
            //fail("尚未实现");
            }
            Assert.assertEquals(r1,r);
            System.out.println("减除混合运算");
        }
        @Test
        public void testChenchu() {
            float Res=0;
            String r1 = null;
            String r=null;
            try
            {
            Res=new Unit().chenchu(10,3,4);
            r=String.valueOf(Res);
            r1=String.valueOf(7.5);
            }
            catch(Exception e)
            {
                e.printStackTrace();
                Assert.fail("没有抛出异常,测试失败");
            //fail("尚未实现");
            }
            Assert.assertEquals(r1,r);
            System.out.println("乘除混合运算");
        }
    
    }

    3.测试结果:

    四.代码覆盖:

              方法:我们是在每个方法的测试中加入一个输出代码,用来显示代码的运行情况

              结果:每个方法都运行成功。

    五、异常处理

    1.超出范围的异常处理(a、b、c的取值范围都在-1000~1000间)

          我们先将a,b的值设置为超出范围的数

         结果:

    2.当除数为零的异常处理

        我们将其中一个除数的值设为0;

        结果:

    六、负数运算

    七、对于5.1实验的更新与改进

         在5.1的实验中除法不能精确到小数位,经我们讨论后改进了这个功能。

         更新的代码:

            float Res=0;
            String r1 = null;
            String r=null;
            try
            {
            Res=new Unit().jianchu(2,3,4);
            r=String.valueOf(Res);
            r1=String.valueOf(1.25);
            }
            catch(Exception e)
            {
                e.printStackTrace();
                Assert.fail("没有抛出异常,测试失败");
            //fail("尚未实现");
            }
            Assert.assertEquals(r1,r);

       结果:

    八、出现的问题及解决方法

         问题:在改进计算除法可以精确到小数位的时候AssertEquals不接受float型

         解决方法:将结果强制转换为String型,代码实现如下:

            Res=new Unit().addchu(10,3,4);
            r=String.valueOf(Res);
            r1=String.valueOf(10.75);

    九、心得与体会

    在结对合作中,我充分的感受到了团队合作的优点:提高编程效率和可以提升自己的能力。两个人一起编程,大家分工合作,大大的减少了时间,可以再较短时间内完成老师布置的任务。而且也可以在小伙伴的身上学习到很多东西,也能发现到自己的不足以及缺点。

      

  • 相关阅读:
    森田疗法
    “不支持一个STA线程上针对多个句柄的WaitAll。”的解决方案
    烽火HG226信息
    祝贺小牛队得NBA总冠军
    .net4调用非托管代码出现:PInvoke调用导致堆栈不对称
    大怪路子逻辑
    C#内存复制与比较
    【收藏】UDP广播和多播
    Objectivec NSString
    Objectivec NSDictionary(NSMutableDictionary)
  • 原文地址:https://www.cnblogs.com/c-19/p/4482304.html
Copyright © 2011-2022 走看看