0

I'm creating an arithmetic calculator and need to be able to have it answer a question like (5.0+8.1)x(2.0) so far i have create my subclasses according to each mathematical operation i.e. +,-,x,/. I have got it to work for 2 numbers like 2.0 + 5.0 but i do not understand how i can get it to work for the above expression.

class Addition extends ArithmeticExpression{
    Addition(double value1, double value2){
       result = value1 + value2;
       this.value1 = value1;
       this.value2 = value2;
    }

    public double display() {
       System.out.println("Addition Question Is");
       System.out.println(value1 + " + "+ value2);
       return result;
    }

    public double evaluate(){
       System.out.println("Addition Answer Is");
       System.out.println(result);
       return result;
    }


}
Dmitry B.
  • 9,107
  • 3
  • 43
  • 64
user1052425
  • 27
  • 1
  • 4
  • 3
    This looks suspiciously like homework. If it is, please tag it as such. – digitaljoel Nov 17 '11 at 18:55
  • 1
    possible duplicate of [java evaluate string to math expression](http://stackoverflow.com/questions/3422673/java-evaluate-string-to-math-expression) – Ted Hopp Nov 17 '11 at 18:55
  • Good approach Dmitry! May not be homework... although it looks a lot like it is. :-) – Costis Aivalis Nov 17 '11 at 19:14
  • its not homework its just something ive wanted to try – user1052425 Nov 17 '11 at 19:21
  • You're at least the second poster this week to be working on the identical task. The previous questioner's problem was type conversion to int, but it's the same task: write a program in java to evaluate expressions. Coincidental, no? – CPerkins Nov 17 '11 at 19:46

5 Answers5

2

This is not as trivial as you might think it is. Forget about the mathematical operations. This is the easy part.

The hard part is to tokenize your input. I suggest reading :

http://www.antlr.org/wiki/display/ANTLR3/Quick+Starter+on+Parser+Grammars+-+No+Past+Experience+Required

http://javadude.com/articles/antlrtut/

FailedDev
  • 26,680
  • 9
  • 53
  • 73
1

You can find a solution here. This is an article from 1997, so the author uses a Vector. It will give you a deprecation warning when you try to run it. Ignore it first and fix it later.

Here you have a well explained nice C/C++ approach from Siberia. Stick to the description of the solution.

Good luck!

Costis Aivalis
  • 13,680
  • 3
  • 46
  • 47
0

Create another class for multiplication and then do

 double result = new Multiplication(new Addition(5.0,8.1).evaluate(), 2.0).evaluate();

Is that what you mean?

Willem Mulder
  • 12,974
  • 3
  • 37
  • 62
0

Learn more about Abstract Syntax Trees (AST), Parsing, Name binding.

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
0

I'm not going to give code because it's homework, but it might help if you think of it more that every operation operates on two expressions, so in your example (5.0+8.1) your addition operation operates on one constant expression that evaluates to 5.0, and another that has a constant evaluation of 8.1. From there, you can think of your multiplication operation as operating on the (5.0+8.1) expression, and another constant expression that evaluates to 2.0.

digitaljoel
  • 26,265
  • 15
  • 89
  • 115