Project #56222 - Java HW

Reverse Polish Calculator!

 

Objective:

 

Write a class called ReversePolishCalculator (which by using the driver) will take a string, parse it, and then calculate the value in post-fix (reverse polish) notation.  To understand this mostly every expression you’ve seen in math is called in-fix notation.

 

In-fix Example:

3 + 2

 

However in post-fix notation the operator is at the end of the expression.

 

Post-fix Example:

3 2 +

 

A stack is commonly used to calculate values in post-fix notation.  Numbers are pushed onto the stack, and when an operator is reached it pops off the last two numbers and then pushes the resulting value back on the stack. See these attached PP Example slides  for further information.

 

The class ReversePolishCalculator needs to have the following:

  • Make sure to import java.util.*;
  • Instance Variables
    • valueStack: A stack of type Stack<Integer> which holds on to the running calculation
  • Constructor
    • A default constructor that initializes the valueStack.  HINT!!!   To set up the valueStack use the following
      valueStack = new Stack<Integer>();
  • Methods
    • calculate: This method which takes in a string as a parameter and returns an integer value is tasked with calculating the expression in reverse polish notation.  First the string must be parsed one token at a time.  Each token is denoted by white space.  If it’s an operator (+,-,*,/) then two items off the stack are popped off, then evaluated with the operator, and finally the resulting value is pushed back onto the stack.  Make sure to check if there’s at least two items on the stack before performing this or else it’s not a properly formatted post-fix expression.  Now if the token is a number then push that number on to the stack.  If it’s not a number or a denoted operator then the string has an error and needs to quit immediately.  Finally if the program reaches the end of the string and it has only one value left on the stack, then that value is popped off and returned.  Otherwise it was not properly formatted and should return 0.
      HINTS!:  First when you’re processing the inputted string you can use a Scanner and call the method next() to get each token.  From there you can evaluate if that token is an operator, numeric value, or an error.  If it’s a numeric value then you may parse that string usingInteger.parseInt() to get the integer value.  You may also call Integer.parseInt() in a try-catch block.  If the token is not an integer an exception will be raised and should be immediately halted.

 

Example Dialog:

 

Enter a reverse polish expression or "quit" to quit

3 2 +

5

Enter a reverse polish expression or "quit" to quit

5 1 2 + 4 * + 3 -

14

Enter a reverse polish expression or "quit" to quit

2 3 4 +

This was not properly formatted

0

Enter a reverse polish expression or "quit" to quit

quit

Goodbye

Finally:

Upload the .java file

 

Driver:

import java.util.Scanner;
public class ReversePolishCalculatorFrontEnd {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner keyboard = new Scanner(System.in);
		ReversePolishCalculator rpCal = new ReversePolishCalculator();
		boolean quit = false;
		while(quit == false)
		{
			System.out.println("Enter a reverse polish expression or \"quit\" to quit");
			String input = keyboard.nextLine();
			if(input.equalsIgnoreCase("quit"))
			{
				System.out.println("Goodbye");
				break;
			}
			System.out.println(rpCal.calculate(input));
		}
			
	}

}

 

Subject Computer
Due By (Pacific Time) 02/04/2015 01:30 pm
Report DMCA
TutorRating
pallavi

Chat Now!

out of 1971 reviews
More..
amosmm

Chat Now!

out of 766 reviews
More..
PhyzKyd

Chat Now!

out of 1164 reviews
More..
rajdeep77

Chat Now!

out of 721 reviews
More..
sctys

Chat Now!

out of 1600 reviews
More..
sharadgreen

Chat Now!

out of 770 reviews
More..
topnotcher

Chat Now!

out of 766 reviews
More..
XXXIAO

Chat Now!

out of 680 reviews
More..
All Rights Reserved. Copyright by AceMyHW.com - Copyright Policy