I'm pretty new to the interface method.

I'm trying to make the addition and multiplication classes work together in an interface based on the exercise I've seen.

For the class, Addition, I have to take two instances of IntegerExpression, the left and right-hand side arguments of the + operation. I have to use two instance variables to store references to the two subexpressions. The getValue should return the result of adding the values provided by the two subexpressions.

For the other class, Multiplication, getValue() should return the result by multiplying the values provided by the two subexpressions.

I've created 3 classes that use IntegerExpression but my code results in compilation errors, which is the reason for this question:

public interface IntegerExpression {
    int getValue();
}


public class IntegerConstant implements IntegerExpression {
private int val;
public int getValue(){
    return val;
}  
public int IntegerConstant(int num){
    val = num;
    return val;
    }
}


public class Addition implements IntegerExpression {
private int numberOne; 
private int numberTwo;
public Addition(int one, int two)
{
    int result = numberOne+numberTwo;
    return result;
}
public int getValue(){
    return Addition(int numberOne, int numberTwo);
    }
}


public class Multiplication implements IntegerExpression{
    public int getValue(){
        return numberOne*numberTwo;
     }
}

Assuming I am able to fix the above code - I expect to be able to write the following code:

IntegerExpression p1 = new Addition(new IntegerConstant(1), new IntegerConstant(2));

IntegerExpression p2 = new Multiplication(p1, new IntegerConstant(3));

2 Answers

1
Rann Lifshitz On Best Solutions

There were several issues in the code provided in the OP, I've taken the liberty of refactoring the code as follows (verified using the following Java IDE):

public interface IntegerExpression {
    int getValue();
}

public abstract class TwoOperandOperator implements IntegerExpression {
   protected IntegerExpression leftNum; 
   protected IntegerExpression rightNum;
   protected TwoOperandOperator(IntegerExpression leftNum, IntegerExpression rightNum) {
      this.leftNum = leftNum;
      this.rightNum = leftNum;
   }
}

public class IntegerConstant implements IntegerExpression {
   private int val;
   @Override
   public int getValue(){
      return val;
   }  
   public /*int*/ IntegerConstant(int val){
      this.val = val;
      // No need for the code below - constructors do not return values 
      // return val;
   } 
}


public class Addition extends TwoOperandOperator {
   public Addition(IntegerExpression leftNum, IntegerExpression rightNum)
   {
      super(leftNum,rightNum);
   }
   @Override
   public int getValue(){
      return (leftNum.getValue() + rightNum.getValue());
   }
}


public class Multiplication extends TwoOperandOperator {
   public Multiplication (IntegerExpression leftNum, IntegerExpression rightNum)
   {
      super(leftNum,rightNum);
   }
   @Override
   public int getValue(){
        return (leftNum.getValue() * rightNum.getValue());
   }
}

I suggest you use an abstract implementation for the two operand based Integer Constants, as implemented above - the construction code is recurring.

-1
Sanjiv On

For Addition, you have returned sum in the constructor. The constructor is generally used to initialize the class's variables. The sum should have been done in the getValue method itself.

public class Addition implements IntegerExpression {
    private int numberOne; 
    private int numberTwo;
    public Addition(int one, int two) {
        this.numberOne = one;
        this.numberTwo = two;
    }
    public int getValue(){
        int result = this.numberOne+this.numberTwo;
        return result;
    }
}

}