0

I have two classes in Java which basically act the same but one represents a multiplier and the other represents a divider. I want to create a parent class which both of them can extend but I don't know how to do that. What should I pass to the parent class? this are both of the classes I have created:

import java.util.HashMap;

public class Divide extends Operator {
    
    private String DIVIDE = " / ";
    
    public Divide(Expression leftExp, Expression rightExp){
        super(leftExp,rightExp);
    }
    
    /*
     * We assume that our divider is not zero. If it is, then we can handle it with exceptions*
     */
    
    @Override
    public float calculate(HashMap<String,Integer> map) {
        return this.leftExp.calculate(map) / this.rightExp.calculate(map);
    }
    
    private String isWithParentheses(Expression exp) {
        return exp.getClass().getSimpleName() == "Variable" || 
                exp.getClass().getSimpleName() == "Number" ? 
                        exp.toString() : "(" + exp.toString() + ")";
    }
    
    @Override
    public String toString() {
        String leftExpString = this.isWithParentheses(this.leftExp);
        String rightExpString = this.isWithParentheses(this.rightExp);
        
        return leftExpString + DIVIDE + rightExpString;
    }
    
    @Override
    public void print() {
        System.out.println(this.toString());
        
    }
    
}

import java.util.HashMap;

public class Multiply extends Operator {
    
    private String MULTIPLY = " * ";
    
    public Multiply(Expression leftExp, Expression rightExp){
        super(leftExp,rightExp);
    }
    
    @Override
    public float calculate(HashMap<String,Integer> map) {
        return this.leftExp.calculate(map) * this.rightExp.calculate(map);
    }
    
    private String isWithParentheses(Expression exp) {
        return exp.getClass().getSimpleName() == "Variable" || 
                exp.getClass().getSimpleName() == "Number" ? 
                        exp.toString() : "(" + exp.toString() + ")";
    }
     
    @Override
    public String toString() {
        String leftExpString = this.isWithParentheses(this.leftExp);
        String rightExpString = this.isWithParentheses(this.rightExp);
                
        return leftExpString + MULTIPLY + rightExpString;
    }
    
    @Override
    public void print() {
        System.out.println(this.toString());
        
    }
    
}

2
  • Why are you passing a Map to calculate() rather than just (Integer, Integer)? Commented Jul 14, 2020 at 21:31
  • BTW: exp.getClass().getSimpleName() == "Variable" should be something like exp.getClass().equals(Variable.class). Commented Jul 14, 2020 at 21:36

1 Answer 1

3

Pass the symbol and the math operation to the super constructor:

public Multiply(Expression leftExp, Expression rightExp){
    super(leftExp,rightExp, " * ", (a, b) -> a * b);
}

and

public Divide(Expression leftExp, Expression rightExp){
    super(leftExp,rightExp, " / ", (a, b) -> a / b);
}

Then you can store these parameters in appropriate fields, and use them in the methods, e.g.

return leftExpString + symbol + rightExpString;

return fn.apply(this.leftExp.calculate(map), this.rightExp.calculate(map));
Sign up to request clarification or add additional context in comments.

4 Comments

Sorry for the stupid question but can a constructor receive a function? for example you wrote : (a, b) -> a / b
Yes. That is, for example, a BinaryOperator<Float>, or a BiFunction<Integer, Integer, Float>, or whatever.
So in this case it's BinaryOperator<Float,Float> ?
No, BinaryOperator only has a single type parameter: it takes two Ts and returns a T. So, BinaryOperator<Float>.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.