Question

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 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...
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 (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...

  • 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...

  • Can somebody help me with this assignment. I will highly appreciate. Also, please display the output...

    Can somebody help me with this assignment. I will highly appreciate. Also, please display the output as well. I need to use JAVA to to write the program. This is the sample output provided by my professor. HOME WORK: due 09.17.2019 (Rational Numbers) Create a class called Rational for performing arithmetic with fractions. Write a program to test your class. Use integer variables to represent the private instance variables of the class- the numerator and the denominator. Provide a constructor...

  • 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...

  • I need help please... Intro to Programming in C – small program 8 Project objective: To...

    I need help please... Intro to Programming in C – small program 8 Project objective: To compile, build, and execute an interactive program using character arrays and string.h functions **Submit source code (prog8.c) through Canvas • One source code file (unformatted text) will be submitted • The file name must match the assignment • The code should be tested and run on a Microsoft compiler before it is uploaded onto Canvas • The code must be submitted on time in...

  • Sorting Threads Assignment Overview Write a multithreaded sorting program in Java which uses the ...

    Sorting Threads Assignment Overview Write a multithreaded sorting program in Java which uses the merge sort algorithm. The basic steps of merge sort are: 1) divide a collection of items into two lists of equal size, 2) use merge sort to separately sort each of the two lists, and 3) combine the two sorted lists into one sorted list. Of course, if the collection of items is just asingle item then merge sort doesn’t need to perform the three steps,...

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