A Simple Calculator Program Help
Hey guys, I basically need to make a simple calculator, but am a bit stuck on how to make it.
It consists of a main class called Expression and branches off to Number Class, Sum Class, etc, which are all very similiar.
In Expression.java consists of this:
publicabstractclass Expression{
publicabstract String toString();
publicabstractdouble evaluateMe();
}
These two methods need to be in all classes.
The main idea for this is to compute various mathmatical expressions and return the answer to the nearest 2nd decimal place.
For Example:
Sum s1 = new Sum(new Number(7.123456789), new Number(11.3344));
Sum s2 = new Sum(s1, new Product(s1, new Number(3)));
System.out.println("s2 = " + s2 + " with value " + s2.evaluateMe());
This should, when executed print out:
s2 = ((7.12 + 11.33) + ((7.12 + 11.33) * 3)) with value 73.831427156
So far I have this:
//Expression.java
publicabstractclass Expression{
publicabstract String toString();
publicabstractdouble evaluateMe();
}
//Sum.java
publicclass Sumextends Expression{
Expression leftside;
Expression rightside;
public String toString(){
return"(" + leftside.toString() +")" +"+" +"(" + rightside.toString() +")";
}
publicdouble evaluateMe(){
return leftside.evaluateMe() + rightside.evaluateMe();
}
}
//And Finally Number.java This is where I am having difficulties
publicclass Numberextends Expression{
double num;
public String toString(){
//return the number but to 2nd place
}
public evaluateMe(){
return num.evaluateMe();
}
}
Ok My problem is in Number.java I am having trouble applying the toString() and evaluateMe() here.
I am having trouble formatting the toString() in Numbers to return value. And I know that the evaluateMe() in Number should return the stored value, but not sure how to make it do that.
Thanks Again

