Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I need this question answered and I cannot used Linked List. For this project, I

ID: 3804533 • Letter: I

Question

I need this question answered and I cannot used Linked List.

For this project, I need to develop a Java program that will act as an RPN (reverse polish notation) calculator. To do this, I have to use the stack class (stack of integers), together with a driver program which can handle the operations:

+ 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

Explanation / Answer

import java.util.Scanner;

public class Calculator {

   public static UserStack stack = new UserStack();
  
   public int add() throws Exception {
       if (stack.size() < 2) {
           throw new Exception("Error: Too many operators");
        }
        int second=stack.pop();        
        int first=stack.pop();
        stack.push(first+second);
        return stack.peek();      
   }

   public int substract() throws Exception {
       if (stack.size() < 2) {
           throw new Exception("Error: Too many operators");
       }
       int second = stack.pop();
       int first = stack.pop();
       stack.push(first - second);
       return stack.peek();
   }
  
   public int multiply() throws Exception {
       if (stack.size() < 2) {
           throw new Exception("Error: Too many operators");
       }
       int second = stack.pop();
       int first = stack.pop();
       stack.push(first * second);
       return stack.peek();
   }
  
   public int divide() throws Exception {
       if (stack.size() < 2) {
           throw new Exception("Error: Too many operators");
       }
       int second = stack.pop();
       int first = stack.pop();
       if (second == 0) {
           throw new Exception("Error: Divide by zero");          
       }
       stack.push(first / second);
       return stack.peek();
   }
  
   public int modulus() throws Exception {
       if (stack.size() < 2) {
           throw new Exception("Error: Too many operators");
       }
       int second = stack.pop();
       int first = stack.pop();
       if (second == 0) {
           throw new Exception("Error: Divide by zero");          
       }
       stack.push(first % second);
       return stack.peek();
   }
  
   public int unaryMinus() throws Exception {
       if (stack.size() < 1) {
           throw new Exception("Error: Too many operators");
       }
       int top = stack.pop();
       stack.push(-top);
       return stack.peek();
   }

   public void exchangeTopTwo() throws Exception {
       if (stack.size() < 2) {
           throw new Exception("Error: Too many operators");
       }
       int second = stack.pop();
       int first = stack.pop();
       stack.push(second);
       stack.push(first);
   }
  
   public void duplicateTopItem() throws Exception {
       if (stack.size() < 1) {
           throw new Exception("Error: Too many operators");
       }
       stack.push(stack.peek());
   }
  
   public void print() throws Exception {
       if (stack.size() < 1) {
           throw new Exception("Error: Too many operators");
       }
       System.out.println(stack.peek());
   }
  
   public void printAndRemove() throws Exception {
       if (stack.size() < 1) {
           throw new Exception("Error: Too many operators");
       }
       System.out.println(stack.pop());
   }
  
   public void printAll() {
       stack.printAll();
   }
  
   public void clearStack() {
       stack.clearStack();
   }
  
   public void Quit() {
       System.exit(0);
   }
  
   public void help() {
       System.out.println("Help Message!!!");
   }
  
   public static void main(String args[]) throws Exception {
       stack.push(1);
       stack.push(2);
       stack.push(3);
       stack.push(4);
       stack.push(5);
       stack.push(6);
       Calculator cal = new Calculator();
      
       Scanner sc = new Scanner(System.in);
       while (true) {
           printMenu();
           System.out.print(" Enter you choice : ");
           String choice = sc.next().toLowerCase();
          
           switch(choice) {
               case "+" :
                   cal.add();
                   break;
               case "-" :
                   cal.substract();
                   break;
               case "*" :
                   cal.multiply();
                   break;
               case "/" :
                   cal.divide();
                   break;
               case "%" :
                   cal.modulus();
                   break;
               case "m" :
                   cal.unaryMinus();
                   break;
               case "r" :
                   cal.exchangeTopTwo();
                   break;
               case "d" :
                   cal.duplicateTopItem();
                   break;
               case "p" :
                   cal.print();
                   break;                  
               case "n" :
                   cal.printAndRemove();
                   break;                  
               case "f" :
                   cal.printAll();
                   break;  
               case "c" :
                   cal.clearStack();
                   break;  
               case "q" :
                   sc.close();
                   cal.Quit();
                   break;
               case "h" :
                   break;                  
               default :
                   System.out.println("Wrong Option ");
           }
       }
   }
  
   public static void printMenu() {
       System.out.println(" ");
       System.out.println("+ : add");
       System.out.println("* : multiply");
       System.out.println("/ : division");
       System.out.println("- : substract");
       System.out.println("% : modulus");
       System.out.println("m : unaryMinus");
       System.out.println("r : exchange top two");
       System.out.println("d : duplicate top item");
       System.out.println("p : print top item");
       System.out.println("n : print and remove top item");
       System.out.println("f : print all contents");
       System.out.println("c : clear the stack");
       System.out.println("q : quit");
       System.out.println("h : help");
       System.out.println(" ");
   }

}

class UserStack {
  
   int top = -1, MAX = 1000;
   public int array[] = new int[1000];
  
   public void push(int value) {
       if (isFull()) {
           return;
       }
       top++;
       array[top] = value;
   }

   public int pop() throws Exception {
       if (isEmpty()) {
           throw new Exception("Stack Empty");
       }
       return array[top--];
   }
  
   public int peek() throws Exception {
       if (isEmpty()) {
           throw new Exception("Stack Empty");
       }
       return array[top];
   }
  
   public boolean isEmpty() {
       if(top == -1) {
           return true;
       }
       return false;
   }
  
   public int size() {
       return top+1;
   }
  
   public boolean isFull() {
       if (top == MAX-1) {
           return true;
       }
       return false;
   }
  
   public void printAll() {
       int size = size();
       for (int i=0; i<size; i++) {
           System.out.print(array[i]+ " ");
       }
   }
  
   public void clearStack() {
       array = new int[1000];
       top = -1;
   }
  
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote