Question

For this project, I need help developing a Java program that will act as an RPN (reverse polish notation) calculator. I...

For this project, I need help developing a Java program that will act as an RPN (reverse polish notation) calculator. I need it to be able to do the following

  • + add the top two items

  • * multiply the top two items

  • - subtract the top item from the next item

  • / integer divide the second item by the top item

  • % find the integer remainder when dividing the second item by the top item

  • m unary minus -- negate the top item

  • r exchange the top two items

  • d duplicate top item on stack

  • p print (to the screen) the top item

  • n print and remove the top item

  • f print all the contents of the stack (leaving it intact)

  • c clear the stack

  • q quit

  • h (or ?) print a help message


Below is an example of what the program might look like

INPUT:

Your program will have keyboard input from the user of the program.

Example: (note: @ stands for typing a return)

h @

p print top

n print top and remove

d duplicate top

r exchange top two items

f print contents of stack

c clear stack

+ add

- subtract

* multiply

/ integer divide

% integer remainder

m unary minus

q quit

h,? this help

33 44 p @

44

f @

33 44 #

r f @

44 33 #

r @

n @

44

d @

f @

33 33 #

+ p @

66

21 * p @

1386

11 + p @

1397

17 / p @

82

8 % p @

2

m p @

-2

f @

-2 #

c @

f @

#

q @

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here is code :

import java.util.*;

public class test {
  
   public static void main (String[] args){
       boolean loop = true;
       Stack<Integer> stack = new Stack<Integer>();
           Scanner input = new Scanner(System.in);
           while(loop && input.hasNext()){
               String word = input.next();
               switch (word){
               case "+":
                   stack.push((stack.pop() + stack.pop()));
                   break;
               case "*":
                   stack.push((stack.pop() * stack.pop()));
                   break;
               case "-":
                   int s = stack.pop();
                   stack.push((stack.pop() - s));
                   break;
               case "/":
                   int t = stack.pop();
                   stack.push((stack.pop() / t));
                   break;
               case "%":
                   int z = stack.pop();
                   stack.push((stack.pop() % z));
                   break;
               case "m":
                   stack.push((stack.pop() *(-1)));
                   break;
               case "r":
                   int a = stack.pop();
                   int b = stack.pop();
                   stack.push(a);
                   stack.push(b);
                   break;
               case "d":
                   int d = stack.pop();
                   stack.push(d);
                   stack.push(d);
                   break;
               case "p":
                   System.out.println(stack.peek());
                   break;
               case "n":
                   System.out.println(stack.pop());
                   break;
               case "f":
                   for(int i=0;i<stack.size();i++){
                       System.out.print(stack.get(i));
                       System.out.print(" ");
                   }
                   System.out.println("#");
                   break;
               case "c":
                   while(!stack.empty()){
                       stack.pop();
                   }
                   break;
               case "q":
                   loop = false;
                   input.close();
                   break;
               case "h":
                   System.out.println("p print top");
                   System.out.println("n print top and remove");
                   System.out.println("d duplicate top");
                   System.out.println("r exchange top two items");
                   System.out.println("f print contents of stack");
                   System.out.println("c clear stack");
                   System.out.println("+ add");
                   System.out.println("- subtract");
                   System.out.println("* multiply");
                   System.out.println("/ integer divide");
                   System.out.println("% integer remainder");
                   System.out.println("m unary minus");
                   System.out.println("q quit");
                   System.out.println("h,? this help");
                   break;
               case "?":
                   System.out.println("p print top");
                   System.out.println("n print top and remove");
                   System.out.println("d duplicate top");
                   System.out.println("r exchange top two items");
                   System.out.println("f print contents of stack");
                   System.out.println("c clear stack");
                   System.out.println("+ add");
                   System.out.println("- subtract");
                   System.out.println("* multiply");
                   System.out.println("/ integer divide");
                   System.out.println("% integer remainder");
                   System.out.println("m unary minus");
                   System.out.println("q quit");
                   System.out.println("h,? this help");
                   break;
               default :
                   try {
                           stack.push(Integer.parseInt(word));
                       }
                       catch (NumberFormatException e)
                       {
                           System.out.println("Enter a valid command or number !");
                           System.out.println("Enter h or ? for Help");
                       }
               }
           }
   }
}

Add a comment
Know the answer?
Add Answer to:
For this project, I need help developing a Java program that will act as an RPN (reverse polish notation) calculator. I...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • For this project, I need help developing a Java program that will act as an RPN...

    For this project, I need help developing a Java program that will act as an RPN (reverse polish notation) calculator. I need it to be able to do the following + add the top two items * multiply the top two items - subtract the top item from the next item / integer divide the second item by the top item % find the integer remainder when dividing the second item by the top item m unary minus -- negate...

  • write a C program that evaluate algebraic expressions over real numbers in Reverse Polish Notation (RPN)

    Your program must evaluate algebraic expressions over real numbers in Reverse Polish Notation (RPN). + (addition), - (subtraction), * (multiplication) and / (division) should be treated as arithmetic operations. Depending on the selection, numerical values must be entered and displayed in decimal, hexadecimal or binary form. Example: The expression ((3+4) *(7+1.6-12) -5) / (3-8.7)Will be calculated on your RPN calculator as 3 4 + 7 1.6 + 12 - * 5 - 3 8.7 - / Your RPN computer's user...

  • I have a java project that I need help trying to finish. I have most of it completed but the issue I am running into is adding numbers with different lengths. Project requirements: . Use a Stack ADT w...

    I have a java project that I need help trying to finish. I have most of it completed but the issue I am running into is adding numbers with different lengths. Project requirements: . Use a Stack ADT with the implementation of your choice (Array or Link), it should not make a difference 2.Read two “integer” numbers from the user. Hint: You don’t need to use an int type when you read, it may be easier to parse the input...

  • This program needs to be in JAVA language. Also, I need to create my own LinkedList...

    This program needs to be in JAVA language. Also, I need to create my own LinkedList class, I can not use the Java LinkedList Library. Please read all aspects of the program, it needs to display 200 random numbers that are 5 digits long sorted from smallest to largest. I can not use Collection(s) or packages libraries. Should only need: import java.util.Random; import java.util.Scanner; Please provide output screenshots after the code. Thank you! You are going to create a Linked...

  • In Java. Needing help with this homework assignment. The following is the class I need to...

    In Java. Needing help with this homework assignment. The following is the class I need to write up and implement. two maps. P15.3 Write a class Polynomial that stores a polynomial such as f(x) = 5x10 + 9x7-x-10 as a linked list of terms. A term contains the coefficient and the power of x. For example, you would store p(x) as (5,10),(9,7),(-1,1),(?10,0) Supply methods to add, multiply, and print polynomials. Supply a constructor that makes a polynomial from a single...

  • Below is the code for the class shoppingList, I need to enhance it to accomplish the...

    Below is the code for the class shoppingList, I need to enhance it to accomplish the challenge level as stated in the description above. public class ShoppingList {   private java.util.Scanner scan; private String[] list; private int counter; public ShoppingList() { scan = new java.util.Scanner(System.in); list = new String[10]; counter = 0; } public boolean checkDuplicate(String item) { for(int i = 0; i < counter; i++) { if (list[i].equals(item)) return true; } return false; } public void printList() { System.out.println("Your shopping...

  • Infix Expression Evaluator For this project, write a C program that will evaluate an infix expression. The algorithm REQ...

    Infix Expression Evaluator For this project, write a C program that will evaluate an infix expression. The algorithm REQUIRED for this program will use two stacks, an operator stack and a value stack. Both stacks MUST be implemented using a linked list. For this program, you are to write functions for the linked list stacks with the following names: int isEmpty (stack); void push (stack, data); data top (stack); void pop (stack); // return TRUE if the stack has no...

  • Infix Expression Evaluator For this project, write a C program that will evaluate an infix expression. The algorithm REQ...

    Infix Expression Evaluator For this project, write a C program that will evaluate an infix expression. The algorithm REQUIRED for this program will use two stacks, an operator stack and a value stack. Both stacks MUST be implemented using a linked list. For this program, you are to write functions for the linked list stacks with the following names: int isEmpty (stack); void push (stack, data); data top (stack); void pop (stack); // return TRUE if the stack has no...

  • Note: The question needs to be answered in "C Programming Languange ". And after the question fin...

    Note: The question needs to be answered in "C Programming Languange ". And after the question find 3 pages for needed informations. Spring CE4717 Language Processors Q1. Consider the following LEx program. return R1 return R2 return R3 return R4 return R5; return R6; IA-2a-z)[A-Za-z0-9]- -2 10-91+ 10-9a-EA-FI Ihi] [01] [01] 이삐 t Vtin) int main (void) int tcode; do f tcode -yylex()i printf ("token type td \"%s\"\n", tcode, yytext); ) while (tcode)i return 0; i. Explain the steps needed...

  • C++ Fraction calculator Need help with code, cant use "using namespace std" Objectives Create and use...

    C++ Fraction calculator Need help with code, cant use "using namespace std" Objectives Create and use functions. Use a custom library and namespace. Use reference variables as parameters to return values from functions. Create a robust user interface with input error checking. Use exceptions to indicate errors. Resources kishio.h kishio.cpp Assignment requirements You will need eight methods (including main). One is given to you. Their requirements are specified below: menu: The menu function takes no arguments, but returns a char...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT