四则运算--计应192--郜艳朋
作者:互联网
四则运算
计应192+4组+郜艳朋
1、eclipse创建Java project,并建立相应的包、类(calculate.java)
2、右击项目,选择Build Path → Add Libraries,打开的对话框选择 JUnit,选择Junit 4 ,点击Finish。
3、在calculate类中编写加减乘除代码
package com.calculate; public class Calculate { public int add(int a,int b) { return (a+b); }; public int subtract(int a,int b) { return (a-b); }; public int multiply(int a,int b) { return (a*b); }; public int divide(int a,int b) { if(b==0) { System.out.print("分母不能为0"); return 0; } else { return (a/b); } }; }
4、编写主函数main
package com.calculate; public class test { public static void main(String[] args) { Calculate test=new Calculate(); System.out.println("加法运算:"+test.add(12, 13)); System.out.println("减法运算:"+test.subtract(18, 2)); System.out.println("乘法运算:"+test.multiply(2, 6)); System.out.println("除法运算:"+test.divide(4, 4)); } }
5、编写测试用例
package com.calculate; import static org.junit.Assert.*; import org.junit.*; public class Calculatetest { Calculate mycal=new Calculate(); @Before public void setUp(){ System.out.println("测试开始"); } @After public void down() { System.out.println("测试结束"); } @Test public void testAdd() { assertEquals(8,mycal.add(2, 6)); } @Test public void testsubtract() { assertEquals(2,mycal.subtract(4,2)); assertEquals(7,mycal.subtract(9, 2)); } @Test public void testmultiply() { assertEquals(15,mycal.multiply(3, 5)); assertEquals(12,mycal.multiply(3, 4)); } @Test public void testdivide() { assertEquals(1,mycal.divide(4, 4)); assertEquals(0,mycal.divide(4, 0)); assertEquals(2,mycal.divide(4, 2)); } }
标签:--,assertEquals,System,郜艳朋,int,计应,mycal,public,out 来源: https://www.cnblogs.com/hnzj-jy192-JT/p/14645154.html