能多次进行计算,知道用户不想练习
将前缀表达式转化为后缀表达式
完善代码UML图
本程序除了测试类,共有四个类。
我认为关键代码就是Fraction.java和IntNumber.java这两个类,对此我在代码中有相应的注释
Fraction.java
import java.util.Random;
public class Fraction { //除法加减乘除
private int numerator, denominator; //分别是分子、分母
public Fraction (int numer, int denom) { //写成一个分数形式
if(denom == 0 ) //分母为零
denom = 1;
if (denom < 0) { //分母为负数
numer = numer * -1;
denom = denom * -1;
}
numerator = numer;
denominator = denom;
reduce();
}
public int getNumerator() {
return numerator;
}
public int getDenominator() {
return denominator;
}
public Fraction add(Fraction op2) { //加法
int commonDenominator = denominator * op2.getDenominator();
int numerator1 = numerator * op2.getDenominator();
int numerator2 = op2.getNumerator() * denominator;
int sum = numerator1 + numerator2;
System.out.print("("+this.toString()+")" + " + " + "("+op2.toString()+")" + "=");
return new Fraction (sum, commonDenominator);
}
public Fraction subtract(Fraction op2) { //减法
int commonDenominator = denominator * op2.getDenominator();
int numerator1 = numerator * op2.getDenominator();
int numerator2 = op2.getNumerator() * denominator;
int difference = numerator1 - numerator2;
System.out.print("("+this.toString()+")" + " - " + "("+op2.toString()+")" + "=");
return new Fraction(difference,commonDenominator);
}
public Fraction multiply (Fraction op2) { //乘法
int numer = numerator * op2.getNumerator();
int denom = denominator * op2.getDenominator();
System.out.print("("+this.toString()+")" + " * " + "("+op2.toString()+")" + "=");
return new Fraction (numer, denom);
}
public Fraction divide (Fraction op2) { //除法
int numer = numerator * op2.getDenominator();
int denom = denominator * op2.getNumerator();
System.out.print("("+this.toString()+")" + " / " + "("+op2.toString()+")" + "=");
return new Fraction (numer, denom);
}
public String toString() {
String result;
if (numerator == 0)
result = "0";
else if(denominator == 0)
return "错误!分母不能为0";
else if (denominator == 1)
result = numerator + "";
else
result = numerator + "/" + denominator;
return result;
}
private void reduce() { //约去公因子
if (numerator != 0) {
int common = gcd (Math.abs(numerator), denominator);
numerator = numerator / common;
denominator = denominator / common;
}
}
private int gcd (int num1, int num2) { //最大公因子
if(num2==0)
return num1;
else
return gcd(num2,num1%num2);
}
public static Fraction obj(){ //随机生成一个真分数的分子和分母
Random ran = new Random();
return new Fraction(ran.nextInt(100),ran.nextInt(100));
}
}
IntNumber.java
import java.util.Random;
public class IntNumber { //整数加减乘除
private int A;
public IntNumber(int A){
this.A = A;
}
public int add(IntNumber op1){ //加法
int result = A + op1.A;
System.out.print(A + " + " + op1.A + "=");
return result;
}
public int subtract(IntNumber op1){ //减法
int result = A - op1.A;
System.out.print(A + " - " + op1.A + "=");
return result;
}
public int multiply(IntNumber op1){ //乘法
int result = A * op1.A;
System.out.print(A + " * " + op1.A + "=");
return result;
}
public String divide(Fraction op1){ //除法
System.out.print(op1.getNumerator() + " / " + op1.getDenominator() + "=");
return op1.toString();
}
public static IntNumber obj(){ //随机生成一个整数
Random ran = new Random();
return new IntNumber(ran.nextInt(100));
}
}
这两个类有相应的测试类IntNumberTest.java和FractionTest.java,进行测试
我的小伙伴给了我想法,并且从一个旁观者的角度,测试代码,用边界情况测试,使得学习更加高效。不断磨合,默契度还是增加的,因为也是室友,所以学习的时间也比较好约定。
PSP2.1 | Personal Software Process Stages | 预估耗时(分钟) | 实际耗时(分钟) |
---|---|---|---|
Planning | 计划 | 60 | 120 |
Estimate | 估计这个任务需要多少时间 | 360 | 600 |
Development | 开发 | 200 | 400 |
Analysis | 需求分析 (包括学习新技术) | 60 | 120 |
Design Spec | 生成设计文档 | 30 | 45 |
Design Review | 设计复审 (和同事审核设计文档) | 30 | 120 |
Coding Standard | 代码规范 (为目前的开发制定合适的规范) | 40 | 150 |
Design | 具体设计 | 50 | 40 |
Coding | 具体编码 | 180 | 260 |
Code Review | 代码复审 | 30 | 50 |
Test | 测试(自我测试,修改代码,提交修改) | 60 | 90 |
Reporting | 报告 | 30 | 30 |
Test Report | 测试报告 | 30 | 30 |
Size Measurement | 计算工作量 | 30 | 20 |
Postmortem & Process Improvement Plan | 事后总结, 并提出过程改进计划 | 60 | 120 |
合计 | 1250 | 2195 |
原文:https://www.cnblogs.com/cxgg/p/8910277.html