Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/346.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java中算术表达式程序的类和构造函数设计_Java_Arithmetic Expressions - Fatal编程技术网

Java中算术表达式程序的类和构造函数设计

Java中算术表达式程序的类和构造函数设计,java,arithmetic-expressions,Java,Arithmetic Expressions,我需要用Java编写一个简单的算术表达式。 算术表达式只能有+/-符号,并应按以下方式在类中表示: 表达式-抽象类 AtomicExpression-扩展表达式类并应表示一个原子数(例如:5/9/332…) CompoundExpression—扩展表达式并应包含2个表达式实例的抽象类 加法表达式/减法表达式-扩展CompoundExpression并表示加法/减法表达式的两个类(例如5+3/9-2…) 我的问题是CompoundExpression类和应从中继承的两个类(AdditionExp

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

表达式-抽象类

AtomicExpression-扩展表达式类并应表示一个原子数(例如:5/9/332…)

CompoundExpression—扩展表达式并应包含2个表达式实例的抽象类

加法表达式/减法表达式-扩展CompoundExpression并表示加法/减法表达式的两个类(例如5+3/9-2…)

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

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;
    }

}
}


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

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

这是一个原子表达式:15。对于这样的表达式,calculate()方法非常容易实现:它应该只返回数字15的值。因此,原子表达式只有一个double类型的字段,其calculate()方法只返回这个double值

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

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

public double calculate() {
    return a.calculate() + b.calculate();
}
减法表达式执行相同的操作,但使用减法:

public double calculate() {
    return a.calculate() - b.calculate();
}
这里的目标不是让您解析字符串。目标只是让您使用polymorpishm将算术表达式表示为对象:

(a + b) - (c + d)
是一个

哪一个是

Subtraction(Addition(a, b), Addition(c, d))
Subtraction(Addition(Atomic(a), Atomic(b)), Addition(Atomic(c), Atomic(d)))
哪一个是

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

非常感谢你!!我想我开始明白怎么做了。我不明白的是tester类中的表达式应该如何声明?例如,如何在tester类中声明原子序数?
表达式a=新的原子表达式(15);表达式b=新的原子表达式(18);表达式APLUBS=新的附加表达式(a,b);双重结果=aplub.calculate()所以如果。。。CompoundExpression类中应该包含什么?如果它是一个抽象类,并且其中没有方法(?),那么CompoundExpression会是这样的:包Q1;公共抽象类CompoundExpression扩展了表达式{Expression FirstOperator,SecondOperator;}基本上是的。它应该只包含两个操作数,并定义一个构造函数。应该保护字段和构造函数。它还可以将字段设置为私有,使用抽象方法
double-doCalculate(表达式e1,表达式e2)
,并将calculate()方法实现为
public final-double-calculate(){return-doCalculate(operan1,operan2);}
然后减法和加法子类将重写
doCalculate(e1,e2)
。但我会让它变得简单,简单地保护字段。
Subtraction(Addition(Atomic(a), Atomic(b)), Addition(Atomic(c), Atomic(d)))