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

In JAVA need help! Implement a stack calculator Console Welcome to the Stack Cal

ID: 3567775 • Letter: I

Question

In JAVA need help!

Implement a stack calculator Console Welcome to the Stack Calculator. Commands: push n, +, -, *, /, clear, or quit. ? push 4 4.0 ? push 3 3.0 4.0 ? push 2 2.0 3.0 4.0 ? * 6.0 4.0 ? + 10.0 ? clear empty ? quit Thanks for using the Stack Calculator. Operation This application implements a stack calculator that does arithmetic with doubles. It accepts commands in any of the following formats: push double-value + - * / clear quit Specifications The calculator itself should be implemented as a separate class named StackCalculator. This class should have the following methods: Method Explanation void push(double x) Pushes x onto the stack. double x pop() Pops the top value from the stack. double add() Pops two values off the stack, adds them, and pushes the result back onto the stack. double subtract() Pops two values off the stack, subtracts the first value from the second value, and pushes the result back onto the stack. double multiply() Pops two values off the stack, multiplies them, and pushes the result back onto the stack. double divide() Pops two values off the stack, divides the second value by the first value, and pushes the result back onto the stack. void clear() Removes all entries from the stack. double[] getValues() Returns all of the values from the stack in array, without removing them from the stack. int size() Gets the number of values in the stack The StackCalculator class should use a linked list to maintain the stack data. The class that implements the user interface for the stack calculator should use a series of nested if statements in its main method to interpret the commands entered by the user.

Explanation / Answer

import java.util.Arrays;
import java.util.Scanner;

class StackCalculator implements myStackInterface {
    /**
     * Construct the myStackInterface.
     */
    public StackCalculator( ) {
        topOfStack = null;       
    }
   
    /**
     * Test if the myStackInterface is logically empty.
     * @return true if empty, false otherwise.
     */
    public boolean isEmpty( ) {
        return topOfStack == null;
    }
   
    /**
     * Make the myStackInterface logically empty.
     */
    public void makeEmpty( ) {
        topOfStack = null;
    }
   
    /**
     * Insert a new item into the myStackInterface.
     * @param x the item to insert.
     */
    public void push( double x ) {
    System.out.println(" pushing "+x);
        topOfStack = new Node( x, topOfStack );
    }
   
    /**
     * Remove the most recently inserted item from the myStackInterface.
     * @throws UnderflowException if the myStackInterface is empty.
     */
    public double pop( ) {
    Node temp = topOfStack;
        if( isEmpty( ) )
            System.out.println(" Stack is Empty");
        double p = temp.element;
       temp = temp.next;
        return p;
    }
   
    /**
     * Get the most recently inserted item in the myStackInterface.
     * Does not alter the myStackInterface.
     * @return the most recently inserted item in the myStackInterface.
     * @throws UnderflowException if the myStackInterface is empty.
     */
    public double top( ) {
        if( isEmpty( ) )
        System.out.println(" Stack is Empty");
        return topOfStack.element;
    }
   
    public double[] getValues()
    {
    System.out.println(" print called");
    double arr[] = new double [20];
    int i = 0;
    Node temp;
    temp = topOfStack;
    while(!isEmpty())
      {
      double p = topOfStack.element;
      topOfStack = topOfStack.next;
      arr[i] = p;
      i++;
      }
    return arr;       
    }
     
    private Node topOfStack;
   
}



interface myStackInterface {
    /**
     * Insert a new item into the myStackInterface.
     * @param x the item to insert.
     */
    void    push( double x );
   
    /**
     * Remove the most recently inserted item from the myStackInterface.
     *
     */
    double pop( );
   
    /**
     * Get the most recently inserted item in the myStackInterface.
     * Does not alter the myStackInterface.
     * @return the most recently inserted item in the myStackInterface.
     *
     */
    double top( );
   
   
    /**
     * Return and remove the most recently inserted item
     * from the myStackInterface.
     * @return the most recently inserted item in the myStackInterface.
     * @exception UnderflowException if the myStackInterface is empty.
     */
  
    boolean isEmpty( );
   
    /**
     * Make the myStackInterface logically empty.
     */
    void    makeEmpty( );
   
    double[] getValues();
}

// Basic node stored in a linked list
// Note that this class is not accessible outside
// of package weiss.nonstandard

class Node {
    // Constructors
    public Node( double theElement ) {
        this( theElement, null );
    }
   
    public Node( double theElement, Node n ) {
        element = theElement;
        next    = n;
    }
   
    public double element;
    public Node next;
}

public class StackCalculator_Main
{
public static void main(String ar[]){
  System.out.println("Welcome to the Stack Calculator");
  Scanner sc = new Scanner(System.in);
  myStackInterface st = new StackCalculator();
  String str;
    
  int i;
  double p;
  double[] ele;
  
  while(true)
  {
   System.out.println(" Enter the Command");
   str = sc.nextLine();
   if(str.contains("quit"))
   {
    System.out.println("Thanks for using the Stack Calculator");
    System.exit(0);
   }
    
   else if(str.contains("push"))
   {
    i = 1;    
    String arr[] = str.split("\s+");    
    while(i < arr.length)
    {     
     p = Double.parseDouble(arr[i]);
     st.push(p);
     i++;
    }     
   }
   else if(str.contains("+"))
   {
    double a = st.pop();
    double b = st.pop();
    st.push(a+b);
   }
   else if(str.contains("-"))
   {
    double a = st.pop();
    double b = st.pop();
    st.push(a-b);
   }
   else if(str.contains("/"))
   {
    double a = st.pop();
    double b = st.pop();
    st.push(a/b);
   }
   else if(str.contains("*"))
   {
    double a = st.pop();
    double b = st.pop();
    st.push(a*b);
   }
   else if(str.contains("clear"))
   {
    st.makeEmpty();
   }
   else if(str.contains("empty"))
   {
    if(st.isEmpty())
     System.out.println(" Stack is Empty");
    else
     System.out.println(" Stack is not Empty");
   }
   else if(str.contains("print"))
   {    
    ele = st.getValues();
    System.out.println(Arrays.toString(ele));
   }
   else
   {
    
   }  
  }
}

}

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