Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Inheritance and Method Overriding

$
0
0
Here is all of my code so far:
public abstract class MathExpression {

		public abstract double evaluate();
}



public class MathValue extends MathExpression {

	private double doubValue;
	
	public MathValue(double d){
		doubValue = d;
	}
	public double evaluate(){
		return doubValue;
	}
	public String toString(){
		String strDoubValue = doubValue + "";
		return strDoubValue;
	}
	
}



public abstract class MathBinaryExpression extends MathExpression{
	
	protected MathExpression leftExpression;
	protected MathExpression rightExpression;

	public MathBinaryExpression(MathExpression l, MathExpression r){
		if(l != null)
			leftExpression = l;
		if (r != null)
			rightExpression = r;	
	}
	public MathExpression getLeftExpression(){
		return leftExpression;
	}
	public MathExpression getRightExpression(){
		return rightExpression;
	}
	
}



public class AddExpression extends MathBinaryExpression {

	public AddExpression(MathExpression left, MathExpression right){
		super(left, right);
	}
	public double evaluate(){
		return leftExpression + rightExpression; //this is wrong. How do I do this?
	}
}



I'm supposed to override the evaluate() method so that it returns the result of adding leftExpression operator rightExpression. And then it says "(be sure to call the correct methods to do this)". And now I'm completely stumped... What methods am I supposed to use? Its not supposed to be an imported method from the Java library is it? Or am I supposed to use something from my code so far? This has me totally confused. I wrote down what I would logically want to do, but I know that its wrong.

I'll be writing more classes too that subtract, multiply, and divide, but they will be of similar format to this and therefore I need to figure out how to do this part first.

Thank you for your time!

Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>