Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I can't seem to get this one right even after looking over what other people have posted on the same questions. What am I doing

I can't seem to get this one right even after looking over what other people have posted on the same questions. What am I doing wrong?

image text in transcribed

This is the error I keep getting:

Exception in thread "main" java.util.InputMismatchException at java.util.Scanner.throwFor(Scanner.java:864) at java.util.Scanner.next(Scanner.java:1485) at java.util.Scanner.nextInt(Scanner.java:2117) at java.util.Scanner.nextInt(Scanner.java:2076) at StackCalculatorApp.main(StackCalculatorApp.java:27) C:\Users\k9lov\AppData\Local\NetBeans\Cache\8.2\executor-snippets un.xml:53: Java returned: 1 BUILD FAILED (total time: 8 seconds)

This is what I tried:

import static java.lang.System.exit; import java.util.*;

// Create Stack Using Linked list class StackCalculator {

// A linked list node private class Node { double data; // data Node link; // reference variavle Node type } Node top; // Constructor StackCalculator(){ this.top = null; } public void push(double x){ Node temp = new Node();

// check if stack (heap) is full. Then inserting an // element would lead to stack overflow if (temp == null) { System.out.print(" Heap Overflow"); return; } temp.data = x; temp.link = top; // update top reference top = temp; }

// Utility function to check if the stack is empty or not public boolean isEmpty(){ return top == null; }

// function to return top element in a stack public double peek() { // check for empty stack if (!isEmpty()) { return top.data; } else { System.out.println("Stack is empty"); return -1; } }

// function to pop top element from the stack public double pop(){ // check for stack underflow if (top == null) { System.out.print(" Stack Underflow"); return; } // update the top pointer to point to the next node top = (top).link; }

public void display(){ if (top == null) { System.out.printf(" Stack Underflow"); exit(1); } else { Node temp = top; while (temp != null) { // print node data System.out.println(temp.data + "->");

// assign temp link to temp temp = temp.link; } } } }

Chapter 12 Project: Stack Calculator Create an application that simulates an old-school stack calculator that uses Reverse Polish Notation (RPN). Console Welcome to the Stack Calculator. Commands: push n, add, sub, mult, div, clear, or quit. stack> push 4 4.0 stack> push 3 3.0 4.0 stack> push 2 2.0 3.0 4.0 stack> mult 6.0 4.0 stack> add 10.0 stack> clear empty stack> quit Thanks for using the Stack Calculator. Specifications The calculator should be implemented as a separate class named StackCalculator. This class should have the following methods: Method Explanation public void push (double x) Pushes x onto the top of the stack. public double pop () Removes the value from the top of the stack. public double add() Removes two values from the stack, adds them, and pushes the result back onto the stack. public double subtract() Same as add() but subtracts the values. public double multiply) Same as add() but multiplies the values. public double divide () Same as add() but divides the values. public void clear() Removes all entries from the stack. public double[] getValues() Returns all of the values from the stack in an array without removing them from the stack. public int size() Gets the number of values in the stack. The StackCalculator class should use a linked list to maintain the stack data

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Databases Organizing Information Digital And Information Literacy

Authors: Greg Roza

1st Edition

1448805929, 978-1448805921

More Books

Students also viewed these Databases questions

Question

What is the net force acting on a 1-kg ball in free fall?

Answered: 1 week ago

Question

What is the function of HTTP response header line for referer ?

Answered: 1 week ago