作业5 四则运算 测试与封装 5.2
//运算类封装 package avshabi; import java.util.StringTokenizer; public class Calculator { int numerator; // 分子 int denominator; // 分母 Calculator(){ } Calculator(int a,int b){ if(a == 0){ numerator = 0; denominator = 1; } else{ setNumeratorAndDenominator(a,b); } } void setNumeratorAndDenominator(int a, int b){ // 设置分子和分母 int c = f(Math.abs(a),Math.abs(b)); // 计算最大公约数 numerator = a / c; denominator = b / c; if(numerator<0 && denominator<0){ numerator = - numerator; denominator = - denominator; } } int getNumerator(){ return numerator; } int getDenominator(){ return denominator; } int f(int a,int b){ // 求a和b的最大公约数 if(a < b){ int c = a; a = b; b = c; } int r = a % b; while(r != 0){ a = b; b = r;; r = a % b; } return b; } Calculator add(Calculator r){ // 加法运算 int a = r.getNumerator(); int b = r.getDenominator(); int newNumerator = numerator * b + denominator * a; int newDenominator = denominator * b; Calculator result = new Calculator(newNumerator,newDenominator); return result; } Calculator sub(Calculator r){ // 减法运算 int a = r.getNumerator(); int b = r.getDenominator(); int newNumerator = numerator * b - denominator * a; int newDenominator = denominator * b; Calculator result = new Calculator(newNumerator,newDenominator); return result; } Calculator muti(Calculator r){ // 乘法运算 int a = r.getNumerator(); int b = r.getDenominator(); int newNumerator = numerator * a; int newDenominator = denominator * b; Calculator result = new Calculator(newNumerator,newDenominator); return result; } Calculator div(Calculator r){ // 除法运算 int a = r.getNumerator(); int b = r.getDenominator(); int newNumerator = numerator * b; int newDenominator = denominator * a; Calculator result = new Calculator(newNumerator,newDenominator); return result; } // 封装了具体运算,主要为对输入进行转换,对输出封装 public static void compute(String data1,String operation,String data2){ StringTokenizer fenxi = new StringTokenizer(data1,"/"); int data1_1 = Integer.parseInt(fenxi.nextToken()); int data1_2 = Integer.parseInt(fenxi.nextToken()); fenxi = new StringTokenizer(data2,"/"); int data2_1 = Integer.parseInt(fenxi.nextToken()); int data2_2 = Integer.parseInt(fenxi.nextToken()); Calculator r1 = new Calculator(data1_1,data1_2); Calculator r2 = new Calculator(data2_1,data2_2); Calculator result; int a,b; if(operation.equals("+")){ result = r1.add(r2); a = result.getNumerator(); b = result.getDenominator(); System.out.println(data1+" "+operation+" " +data2+" = " + a + "/" + b); } if(operation.equals("-")){ result = r1.sub(r2); a = result.getNumerator(); b = result.getDenominator(); System.out.println(data1+" "+operation+" " +data2+" = " + a + "/" + b); } if(operation.equals("*")){ result = r1.muti(r2); a = result.getNumerator(); b = result.getDenominator(); System.out.println(data1+" "+operation+" " +data2+" = " + a + "/" + b); } if(operation.equals("/")){ result = r1.div(r2); a = result.getNumerator(); b = result.getDenominator(); System.out.println(data1+" "+operation+" " +data2+" = " + a + "/" + b); } } } // 测试类 package avshabi; import java.util.Scanner; public class TestCal { public static void main(String[] args) { // TODO Auto-generated method stub // 用户输入两分数和运算符 Scanner input = new Scanner(System.in); System.out.println("请用户输入第一个分数(格式a/b)"); String data1 = input.next(); System.out.println("请用户输入要进行运算的运算符(+-*/)"); String operation = input.next(); System.out.println("请用户输入第二个分数(格式c/d)"); String data2 = input.next(); // 根据用户输入进行具体运算 Calculator cal = new Calculator(); System.out.println("运算结果为:"); cal.compute(data1, operation, data2); // } } } //JUint 应用测试举例 package avshabi; import static org.junit.Assert.*; import org.junit.Test; public class CalculatorTest { @Test public void testAdd() { Calculator cal = new Calculator(); cal.add(cal); } @Test public void testSub() { Calculator cal = new Calculator(); cal.sub(cal); } @Test public void testMuti() { Calculator cal = new Calculator(); cal.muti(cal); } @Test public void testDiv() { Calculator cal = new Calculator(); cal.div(cal); } @Test public void testCompute() { Calculator cal = new Calculator(); cal.compute("1/2", "+", "2/3"); cal.compute("1/2", "-", "2/3"); cal.compute("1/2", "*", "2/3"); cal.compute("1/2", "/", "2/3"); } }
以下是刚创建的时候会出现如下图:
其中测试有5个方法,当改了其中2个,其他添改代码的时候会出现如下图错误提醒
当我全部方法添加了改动后的类对象以及测试方法对了就会出现下图情况
附上以下上一次主代码测运行图以及工作图
我们的作业在之前的基础上修改了添加了一个类作为测试类,利用原来那个类对象,去调用测试方法,其中包括了加减乘除四个功能以及答案输出的方法,在努力尝试后,测试成功了,虽然测试修改大概花了好几个多小时,但让程序变得更可靠。 不过我发现会出现错误,容错功能不好,在这里我想问问助教,我的分数一开始是用字符串的形式,然后是通过分析字符串利用”/“来截取从而来获取数值,但这样捕获异常就会很不好来弄,我要是利用ac开码去捕获异常的话就算是我输入数值也算是字符而出错,纵使经过我努力修改,仍然无法想出好的方法解决,但是至少输入正确数据程序是能运行的。
结对同伴:
姓名 |
学号 |
博客地址 |
领航员 侯圣燊 |
201306114402 |
http://home.cnblogs.com/u/sihouzi/
|
驾驶员 陈其林 |
201306114409 |
http://www.cnblogs.com/chaney/ |