【问题标题】:classes and constructors design for Arithmetic expression program in JavaJava中算术表达式程序的类和构造函数设计
【发布时间】:2014-06-12 21:59:21
【问题描述】:

我需要用 Java 编写一个简单的算术表达式表示。 算术表达式只能有 +/- 符号,并且应该在类中以下列方式表示:

表达式 - 抽象类

AtomicExpression - 扩展了 Expression 类,应该表示一个原子序数(例如:5/9/332...)

CompoundExpression - 扩展 Expression 的抽象类,应包含 2 个 Expression 实例。

AdditionExpression/SubtractionExpression - 两个扩展 CompoundExpression 的类,将表示加法/减法表达式(例如 5+3/9-2...)

我的问题在于 CompoundExpression 类和应该从它继承的两个类(AdditionExpression 和 SubtractionExpression) 我不知道如何编写这些类,我应该在每个类中放置哪些参数以及如何表示它们。 这是我写到现在的:

package Q1;

public abstract class Expression {

    /*Empty Constructor for Expression object */
    public Expression(){
    }


    /*Calculate the value of the expression
     * @return double This the value of the expression
     */
    public double calculate(){
        String toClac = new String(this.expression); //Copy the expression to calculate to temporary object
        double answer=0;
        int i=1, operator = 0; //0 signal + operator and 1 signal - operator.

        while (toClac!=null && !toClac.isEmpty()){
            //Assumes correct input - start with a digit and not a sign '+' or '-' 
            while (i<toClac.length() && Character.isDigit(toClac.charAt(i)))
                    i++;
            // i equal to the first index that is not a number in the expression


            if (operator == 0){
                answer += Integer.parseInt(toClac.substring(0,i));
            }else answer -= Integer.parseInt(toClac.substring(0,i));

            if (i<toClac.length()) //mean that while stopped because found char that is not number
            {
                //check if the next operator is - or +
                if (toClac.charAt(i)=='-')
                    operator = 1;
                else operator = 0;
                toClac = toClac.substring(i+1,toClac.length()); //cut and save the continue of the string to parse
            }else toClac = null;
            i=0;
        }
        return answer;
    }

    /*
     * Check if two expressions are equal. two expressions are equals if their calculated value is the same 
     * @Override equals - return true if two expressions are equals
     */
    public boolean equals(Object second)
    {
        double firstAnswer, secondAnswer;
        firstAnswer = this.calculate();
        secondAnswer = ((Expression)(second)).calculate();
        return (firstAnswer == secondAnswer);
    }
}

package Q1;

public class AtomicExpression extends Expression {

    int numericExpression;

    //maybe delete
    /*Empty Constructor for AtomicExpression object */
    public AtomicExpression(){
        super();
    }

    /*Constructor for AtomicExpression object*/
    public AtomicExpression(int realNum){
        super();
        this.numericExpression = realNum;
    }

    /*Return the string representation of the expression  
     */
    public String toString(){
        return Integer.toString(this.numericExpression);
    }
}

package Q1;

public abstract class CompoundExpression extends Expression {

    Expression firstOperand, secondOperand;

    //Constructor of CompundExpression object containing two expressions
    public CompoundExpression(Object first, Object second){
        if(first instanceof Integer)
            this.firstOperand = new AtomicExpression((Integer)(first));
        else this.firstOperand = (Expression)first;

        if(second instanceof Integer)
            this.secondOperand = new AtomicExpression((Integer)(second));
        else this.secondOperand = (Expression)second;
    }

}

包装Q1;

//表示加法表达式的类 公共类 AdditionExpression 扩展 CompoundExpression{

public AdditionExpression(Expression first, Expression second){
    super(first, second);
}


public String toString(){
    String str = this.firstOperand.expression+ " + " + this.secondOperand.expression;
    return str;
}

}

package Q1;

//Class representing an subtraction expression
public class SubtractionExpression extends CompoundExpression{
    int left, right;

    public SubtractionExpression(Expression first, Expression second){
        super(first, second);

    }

    public String toString(){
        String str = this.firstOperand.expression+ " - " + this.secondOperand.expression;
        return str;
    }

}

【问题讨论】:

    标签: java arithmetic-expressions


    【解决方案1】:

    我想你还没有理解这个练习的原理,抽象类和多态的原理。

    一个表达式有一个 calculate() 方法,它返回这个表达式的值。这个方法应该是抽象的,因为根据 Expression 的实际子类,calculate() 方法不会做同样的事情。

    这是一个原子表达式:15。对于这样的表达式,calculate() 方法很容易实现:它应该简单地返回数字的值:15。因此,AtomicExpression 有一个 double 类型的字段,并且它的 calculate() 方法只返回这个双精度值。

    复合表达式稍微复杂一点:它有两个表达式类型的操作数(即两个字段)。将两个操作数组合在一起以计算实际值。但是 CompoundExpression 还是一个抽象类:两个操作数的组合方式取决于实际的子类。

    AdditionExpression 是 CompoundExpression 的一个具体子类:它接受它的两个操作数,计算它们的值(使用它们的 calculate() 方法),然后通过将它们相加来组合它们:

    public double calculate() {
        return a.calculate() + b.calculate();
    }
    

    SubtractionExpression 做同样的事情,但用减法代替:

    public double calculate() {
        return a.calculate() - b.calculate();
    }
    

    这里的目标不是让你解析一个字符串。目标只是让您使用 polymorphishm 将算术表达式表示为对象:

    (a + b) - (c + d)
    

    是一个

    Subtraction(a + b, c + d)
    

    这是一个

    Subtraction(Addition(a, b), Addition(c, d))
    

    这是一个

    Subtraction(Addition(Atomic(a), Atomic(b)), Addition(Atomic(c), Atomic(d)))
    

    【讨论】:

    • 非常感谢!!我想我开始明白该怎么做了。我不明白的是如何在测试器类中声明表达式?例如如何在测试器类中声明原子序数?
    • Expression a = new AtomicExpression(15); Expression b = new AtomicExpression(18); Expression aPlusB = new AdditionExpression(a, b); double result = aPlusB.calculate();
    • 那么如果... CompoundExpression 类应该是什么?如果它是一个抽象类并且其中没有方法(?)
    • 是不是 CompoundExpression 会是这样的:package Q1;公共抽象类 CompoundExpression 扩展表达式 { 表达式 firstOperand, secondOperand; }
    • 基本上,是的。它应该只包含两个操作数,并定义一个构造函数。字段和构造函数应该受到保护。它还可以使字段私有,具有抽象方法double doCalculate(Expression e1, Expression e2),并将calculate() 方法实现为public final double calculate() {return doCalculate(operand1, operand2);},然后减法和加法子类将覆盖doCalculate(e1, e2)。但我会让它变得简单,简单地保护这些字段。
    猜你喜欢
    • 2020-04-16
    • 2012-11-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-15
    • 1970-01-01
    相关资源
    最近更新 更多