模块化软件构造2

本文介绍了使用Java设计一个基础类Equation和其子类AddEquation和SubEquation,用于表示带操作符的算式。同时还设计了Exercise类生成指定数量的不重复算式,以及Printer类用于打印这些算式。主要展示了如何运用抽象方法和Java集合处理随机生成的算术问题。
摘要由CSDN通过智能技术生成

1、在实验一的基础上改进设计,功能点如下:

(1)设计类Equation,它代表一个有两个操作数(short),一个操作符(char)的算式。除了字段对应的getter、setter,还应覆盖实现equalshashCode方法。并有抽象(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文档:

Random (Java Platform SE 8 )

java.util.HashSet类的API文档:

HashSet (Java Platform SE 8 )

Java集合: HashSet 哈希集详解 - 知乎

java.util.Scanner类的API文档:

Scanner (Java Platform SE 8 )

java读取整数_java 读入整数-CSDN博客

关于equalshashCode方法:

Java中的equals()方法_public boolean equals(object obj) { if (obj instan-CSDN博客

详解equals()方法和hashCode()方法 - 知乎

关于Java集合的遍历:

Java 实例 – 集合遍历 | 菜鸟教程

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值