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

[3787 byte] By [AdiVa] at [2007-11-27 10:44:09]
# 1

> I am having trouble formatting the toString() in Numbers to return value.

See java.text.DecimalFormat

> And I know that the evaluateMe() in Number should return the stored value, but not sure how to make it do that.

I thought evaluateMe's return type was double. That should be easy!

BigDaddyLoveHandlesa at 2007-7-28 20:05:09 > top of Java-index,Java Essentials,New To Java...
# 2

For decimal Format do you mean ROUND_HALF_UP? I was not too sure, and for evaluateMe() do you mean to have just

public double evaluateMe(){

return num;

}

Message was edited by:

AdiV

AdiVa at 2007-7-28 20:05:09 > top of Java-index,Java Essentials,New To Java...
# 3

> For decimal Format do you mean ROUND_HALF_UP?

I meant the class DecmalFormat.

> and for evaluateMe() do you mean to

> have just

>

>

> public double evaluateMe(){

> return num;

> }

Does this work when you test it?

BigDaddyLoveHandlesa at 2007-7-28 20:05:09 > top of Java-index,Java Essentials,New To Java...
# 4

I can't run it right now, since there the Number class is not done. Mainly the toString()

AdiVa at 2007-7-28 20:05:09 > top of Java-index,Java Essentials,New To Java...
# 5

wierd the last 3 replies got lost or something

AdiVa at 2007-7-28 20:05:09 > top of Java-index,Java Essentials,New To Java...
# 6

> I can't run it right now, since there the Number class is not done. Mainly the toString()

quick and dirty, to get it up-and-running:

public String toString() {

return String.valueOf(num);

}

BigDaddyLoveHandlesa at 2007-7-28 20:05:09 > top of Java-index,Java Essentials,New To Java...
# 7

Well that helped clear the Number class, but there is now an error in Expressions.java

In Sum s1 = new Sum....An error says "The constructor Sum(Number, Number) is undefined. and a similiar one on same line "The constructor Number(double) is undefined.

I placed Expression underneath, any ideas on how to fix it?

public abstract class Expression {

public abstract String toString();

public abstract double evaluateMe();

public static void main(String args[]){

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());

}

}

AdiVa at 2007-7-28 20:05:09 > top of Java-index,Java Essentials,New To Java...
# 8

> In Sum s1 = new Sum....An error says "The constructor Sum(Number,

> Number) is undefined. and a similiar one on same line "The constructor

> Number(double) is undefined.

Sounds like you forgot to write constructors for Sum.

BigDaddyLoveHandlesa at 2007-7-28 20:05:09 > top of Java-index,Java Essentials,New To Java...
# 9

Yep, ok I will try to come up with something in there.

AdiVa at 2007-7-28 20:05:09 > top of Java-index,Java Essentials,New To Java...