Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Java calculator implementation

  1. #1
    Junior Member
    Join Date
    Mar 2012
    Posts
    21
    My Mood
    Confused
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Java calculator implementation

    am basically attempting to create a calculator using a class hierarchy - the basic function will be:

    Convert the String representation into a list of Tokens
    Convert the List of Tokens into Polish Notation
    Calculate the result on the basis of Polish Notation
    I also need to represent objects -

    numbers operators and brackets.

    Following this i need to convert to polish notation - using a shunting yard algorithm and then compute the answer.

    I am ok with understanding the shunting yard but am unsure as to the class hierarchy part.

    The precedence is - Addition and Subtraction have a precedence of 2, and Multiplication and Division have a precedence of 3.

    I am assuming i need a filereader class and also classes to handle the different objects.

    I also need help in creating my own stacks and queues.

    Thanks


  2. #2
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: Java calculator implementation

    There are two phases: Conversion from an Infix String to an RPN expression, and evaluation of the RPN expression. There is no theoretical reason why you couldn't do it all-in-one, but by having the intermediate RPN expression available, it would probably lead to more systematic debugging.



    For starters:

    For conversion to RPN, I would probably use a Stack<Character> object for the operator stack and a StringBuilder object for the output expression. Operator characters and operand numbers will be converted to Strings when you append them to the output expression.

    The conversion would use Stack methods push(), pop() and peek() and empty() and the StringBuilder append() method.


    For evaluation I would probably use a Stack<Double> object for the operand stack. Split the StringBuilder output expression into an array of Strings for processing, then step through the array. You will need Stack methods push(), pop() and empty(). You can index through the RPN array and see if the next item is a String corresponding to one of the operands.

    As you get into implementation details (on paper, before writing code) you may decide that certain functions or data structures can help keep it clean. A hashmap for operators' precedence might be handy (and cleaner) instead of hard-coding procedural precedence comparisons in the RPN conversion process, for example.


    Cheers!

    Z
    Last edited by Zaphod_b; November 4th, 2012 at 01:32 PM.

  3. #3
    Junior Member
    Join Date
    Mar 2012
    Posts
    21
    My Mood
    Confused
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Java calculator implementation

    Hi thanks for the help.

    How would I implement my own stack without using the java utils?

    also what sort of hierarchy should i use? I was thinking maybe a filereader class and then one to deal with the objects and then a main to tie it all together?

    thanks again

  4. #4
    Junior Member
    Join Date
    Mar 2012
    Posts
    21
    My Mood
    Confused
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Java calculator implementation

    so my thinking now is to have the following classes:

    stack class
    main - filereading, dealing with the numbers+operands
    shunting yard class

    Is this an acceptable sort of hierarchy?

    thanks

  5. #5
    Junior Member
    Join Date
    Mar 2012
    Posts
    21
    My Mood
    Confused
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Java calculator implementation

    Quote Originally Posted by why_always_me View Post
    so my thinking now is to have the following classes:

    stack class
    main - filereading, dealing with the numbers+operands
    shunting yard class

    Is this an acceptable sort of hierarchy?

    thanks
    Hi Guys my new thinking is :

    List class
    Node class
    Shunting yard class
    Main class
    Main includes the reverse Polich notation, filereader and tokenizer.

    My question now is how would i link main to the shunting yard? I was told I should create a list of objects which will be numbers, operands and brackets. Thanks again

  6. #6
    Junior Member
    Join Date
    Nov 2010
    Posts
    7
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: Java calculator implementation

    you can create instances of you class in your main. then you can use those instances to perform whatever you need to.
    i.e.

    ...main()
    {
    ShuntingYard var1;
    .....
    }

Similar Threads

  1. Need Help with this Java Calculator
    By jo112205 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 13th, 2011, 06:03 AM
  2. Java Security Implementation for Plugin Supported Architecture
    By bgroenks96 in forum Java Theory & Questions
    Replies: 19
    Last Post: November 22nd, 2011, 04:13 PM
  3. help with deque implementation using arrays in java
    By twi in forum Java Theory & Questions
    Replies: 1
    Last Post: June 14th, 2011, 09:55 PM
  4. Java Calculator
    By helloworld922 in forum Algorithms & Recursion
    Replies: 7
    Last Post: January 10th, 2011, 06:01 AM
  5. java newbie..simple mail server implementation
    By saurabh4dudes in forum Java Networking
    Replies: 0
    Last Post: March 12th, 2010, 08:53 AM

Tags for this Thread