Question

I need to program a simple arithmetic expression representation in Java. the arithmetic expression can have only +/- signs and should represent in classes in the following way:

Expression - Abstract class

AtomicExpression - extends Expression class and should represent an atomic number (such as:5/9/332...)

CompoundExpression - Abstract class that extends Expression and should contain 2 Expression instance.

AdditionExpression/SubtractionExpression - two classes that extends CompoundExpression and will represent addition/subtraction expression (e.g 5+3/9-2...)

my problem is with the CompoundExpression class and the two classes that should inherit from it (AdditionExpression and SubtractionExpression) I dont know how to write those classes, which parameters i should put in each class and how to represent them. This is what I wrote until now:

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

}

package Q1;

//Class representing an addition expression public class AdditionExpression extends 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;
    }

}
Was it helpful?

Solution

I think you have not understood the principle of this exercise, and the principle of abstract class and polymorphism.

An expression has a calculate() method, which returns the value of this expression. This method should be abstract, because depending on the actual subclass of Expression, the calculate() method won't do the same thing.

Here's an atomic expression: 15. For such an expression, the calculate() method is very easy to implement: it should simply return the value of the number: 15. An AtomicExpression thus has a single field of type double, and its calculate() method simply returns this double value.

A compound expression is a bit more complex: it has two operands (i.e. two fields), of type Expression. The two operands are combined together to calculate the actual value. But CompoundExpression, once again, is an abstract class: the way the two operands are combined depends on the actual subclass.

AdditionExpression is a concrete subclass of CompoundExpression: it takes its two operands, calculates their value (using their calculate() method), and combines them by adding them:

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

The SubtractionExpression does the same thing, but with a subtraction instead:

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

The goal here is not to make you parse a string. The goal is simply to make you represent an arithmetic expression as an object, using polymorpishm:

(a + b) - (c + d)

is a

Subtraction(a + b, c + d)

which is a

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

which is a

Subtraction(Addition(Atomic(a), Atomic(b)), Addition(Atomic(c), Atomic(d)))
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top