i have built a Simple calculator that calculates arithmetic expressions, it is working but it has to evaluate more than 2 numbers eg (5.0 + 8.1) * (2.0) i am not sure as to how to get it to do this.....not for want of trying (i have to say!:-)

There is obviously something i am not getting, any help will be greatly appreciated, i have a super class Arithmetic Expression and the 4 basic calculations as child classes (as per specification)and then CalcTest as a test class. All functions work as they should but in the CalcTest class i have to evaluate an expression like the one above ^

**Please dont be put off by the amount of code it is a very simple calculator (just had to use abstract classes and inheritance)It only has 4 basic operations add, subtract, divide and multiply**

Thankyou

please help if you can?

package calculator; import java.lang.String; /* * @author */ public class CalcTest { public static void main(String[] args) { Addition add = new Addition(); double add1 = add.getAdd(); double add2 = add.getAdd(); add1 = (5.0); add2 = (8.1); double result = add1 + add2; System.out.println("(5.0) + (8.1) = " + result); Subtraction sub = new Subtraction(); double sub1 = sub.getSub(); double sub2 = sub.getSub(); double sub3 = sub.getSub(); double sub4 = sub.getSub(); sub1 = (34.90); sub2 = (23.89); double result2 = sub1 - sub2; System.out.println("(34.90) - (23.89 )= " + result2); Multiplication mul = new Multiplication(); double mul1 = mul.getMul(); double mul2 = mul.getMul(); mul1 = (278.0); mul2 = 2.0; double result3= mul1 * mul2; System.out.println("(2.0) * 278.0 = " + result3); Division div = new Division(); double div1 = div.getDiv(); double div2 = div.getDiv(); div1 = (345); div2 = (890); double result4= div1 / div2; System.out.println("345 / 890 = " + result4 ); System.out.println( "(5.0 + 8.1) * (2.0) = " ); //Not sure as to how to //calculate more than one number? } private CalcTest() { } }

package calculator; /** * * @author */ public abstract class ArithmeticExpression { private double num1; private double num2; private double result; public abstract double evaluate(); public abstract void display(); /** * @return the num1 */ public double getNum1() { return num1; } /** * @param num1 the num1 to set */ public void setNum1(double num1) { this.num1 = num1; } /** * @return the num2 */ public double getNum2() { return num2; } /** * @param num2 the num2 to set */ public void setNum2(double num2) { this.num2 = num2; } /** * @return the result */ public double getResult() { return result; } /** * @param result * @param result the result to set */ public void setResult(double result) { this.result = result; } public double evaluate(double num1, double num2) { return getResult(); } }

package calculator; /** * * @author */ public class Addition extends ArithmeticExpression { private double add; /** * @return the add */ public double getAdd() { return add; } /** * @param add the add to set */ public void setAdd(double add) { this.add = add; } @Override public double evaluate (double num1, double num2) { this.setResult(num1 + num2); return getResult(); } @Override public void display() { System.out.println( "("+ getNum1()+ "+ "+ getNum2() + ")" +"=" + getResult()); } /** * * @return */ @Override public double evaluate() { throw new UnsupportedOperationException("Not supported yet."); } }

package calculator; /** * * @author */ public class Subtraction extends ArithmeticExpression { private double sub; public double getSub() { return sub; } /** * @param sub */ public void setSub(double sub) { this.sub = sub; } /** * * @param num1 * @param num2 * @return */ @Override public double evaluate(double num1, double num2) { this.setResult1D(num1 + num2); return getResult1D(); } @Override public void display() { } @Override public double evaluate() { throw new UnsupportedOperationException("Not supported yet."); } private void setResult1D(double d) { throw new UnsupportedOperationException("Not yet implemented"); } private double getResult1D() { throw new UnsupportedOperationException("Not yet implemented"); } }

package calculator; /** * * @author */ public class Multiplication extends ArithmeticExpression { private double Multiply; /** * @return the multiply */ public double getMul() { return Multiply; } /** * @param Mul */ public void setMul(double Mul) { this.Multiply = Mul; } @Override public double evaluate(double num1, double num2) { this.setResult1D(num1 + num2); return getResult1D(); } @Override public void display() { } @Override public double evaluate() { throw new UnsupportedOperationException("Not supported yet."); } private void setResult1D(double d) { throw new UnsupportedOperationException("Not yet implemented"); } private double getResult1D() { throw new UnsupportedOperationException("Not yet implemented"); } }

package calculator; /** * * @author */ public class Division extends ArithmeticExpression { private double Division; /** * @return the division */ public double getDiv() { return Division; } /** * @param Div */ public void setDiv(double Div) { this.Division = Div; } @Override public double evaluate(double num1, double num2) { this.setResult(num1 + num2); return getResult(); } @Override public void display() { System.out.println(); } @Override public double evaluate() { throw new UnsupportedOperationException("Not supported yet."); }

This post has been edited by **Tara200**: 29 November 2010 - 04:19 PM