1、在实验一的基础上改进设计,功能点如下:
(1)设计类Equation,它代表一个有两个操作数(short),一个操作符(char)的算式。除了字段对应的getter、setter,还应覆盖实现equals、hashCode方法。并有抽象(abstract)方法calculate,返回算式计算结果。
(2)设计子类AddEquation、SubEquation,它们继承Equation,并实现calculate方法。
(3)设计习题类Exercise,它可以产生指定数目的算式,放入Java容器中(HashSet)。要求使用随机数Random实例,随机产生的算式操作数及结果应在0到100内,另外习题中的算式不重复。
(4)设计打印类Printer,可打印(标准输出)习题、结果,按照指定的列数打印Java容器中的习题及结果
(5)设计Main类。读入数目m和n。产生m个不重复的算式Equation(加法或减法),放入Excercise中,使用Printer打印Exercise的习题,打印时每列n个算式。
import java.util.Objects;
abstract class Equation {
private short operand1;
private short operand2;
private char operator;
//有参构造
public Equation(short operand1, short operand2, char operator) {
this.operand1 = operand1;
this.operand2 = operand2;
this.operator = operator;
}
//无参构造
public Equation(){
}
//getter,setter
public short getOperand1() {
return operand1;
}
public void setOperand1(short operand1) {
this.operand1 = operand1;
}
public short getOperand2() {
return operand2;
}
public void setOperand2(short operand2) {
this.operand2 = operand2;
}
public char getOperator() {
return operator;
}
public void setOperator(char operator) {
this.operator = operator;
}
@Override
public boolean equals(Object o) {
if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false;
Equation equation = (Equation) o;
return operand1 == equation.operand1 &&
operand2 == equation.operand2 &&
operator == equation.operator;
}
@Override
public int hashCode() {
return Objects.hash(operand1, operand2, operator);
}
public abstract short calculate();
}
public class AddEquation extends Equation {
//有参构造
public AddEquation(short operand1,short operand2){
super(operand1 , operand2 ,'+');
}
//无参构造
public AddEquation(){
}
//对calculate进行重写
@Override
public short calculate(){
return (short)(getOperand1()+getOperand2());
}
}
public class SubEquation extends Equation {
//有参构造
public SubEquation (short operand1,short operand2){
super(operand1 , operand2 , '-');
}
//无参构造
public SubEquation(){
}
@Override
public short calculate(){
return (short) (short)(getOperand1()-getOperand2());
}
}
import java.util.Random;
import java.util.HashSet;
import java.util.Set;
class Exercise {
private Set<Equation> equations = new HashSet<>();
private Random random = new Random();
public void addEquation(Equation equation) {
equations.add(equation);
}
public Set<Equation> generateExercise(int size) {
while(equations.size() < size) {
short operand1 = (short) random.nextInt(101);
short operand2 = (short) random.nextInt(101);
char operator = random.nextBoolean() ? '+' : '-';
Equation equation;
if(operator == '+') {
//结果不在0-100直接需要重新生成数字
while((operand1 + operand2) > 100) {
operand1 = (short) random.nextInt(101);
operand2 = (short) random.nextInt(101);
}
equation = new AddEquation(operand1, operand2);
} else {
//结果不在0-100直接需要重新生成数字
while((operand1 - operand2) < 0 || (operand1 - operand2) > 100) {
operand1 = (short) random.nextInt(101);
operand2 = (short) random.nextInt(101);
}
equation = new SubEquation(operand1, operand2);
}
//判断算式是否重复
if(!equations.contains(equation)) {
equations.add(equation);
}
}
return equations;
}
}
import java.util.Set;
public class Printer {
public void print(Set<Equation>equations, int column){
int count = 0;
for (Equation equation : equations) {
if (count % column == 0) {
System.out.println(); // 换行
}
else {
System.out.print("\t"); // 对齐
}
System.out.print(equation.getOperand1() + " " + equation.getOperator() + " " +
equation.getOperand2() + " = " + equation.calculate()+" ");
count++;
}
// 如果算式总数不是列数的倍数,需要额外换行
if (count % column != 0) {
System.out.println();
}
}
}
import java.util.*;
public class Main {
public static void main(String[] args){
Scanner scan=new Scanner(System.in);
System.out.println("请输入要生成的算式数量:");
int m= scan.nextInt();
System.out.println("请输入每行要生成的算式个数:");
int n= scan.nextInt();
scan.close();
Exercise exercise = new Exercise();
exercise.generateExercise(m);
Printer printer = new Printer();
printer.print(exercise.generateExercise(m),n);
}
}
java.util.Random类的API文档:
java.util.HashSet类的API文档:
java.util.Scanner类的API文档:
关于equals、hashCode方法:
Java中的equals()方法_public boolean equals(object obj) { if (obj instan-CSDN博客
详解equals()方法和hashCode()方法 - 知乎
关于Java集合的遍历: