Question

For this part, you will write a PostfixCalculator class that has methods for processing each possible...

For this part, you will write a PostfixCalculator class that has methods for processing each possible input. You will write a Tester class that reads a line of input from the user, with each symbol separated by a space, and prints out the numeric value of the top of the stack. If the user specifies an incomplete expression, print out the top of the stack and print out a message saying that the stack contains more than one item. If the user specifies an invalid expression in which an operator does not have enough operands to function, the PostfixCalculator method should throw an exception, an the Tester should print out an error and then prompt for the next input. Exit the program when the user enters a blank line.

public class PostfixCalculator
{
    public void pushNumber(double d);  // these methods update the stack
    public void pushAdd();
    public void pushSubtract();
    public void pushMultiply();
    public void pushDivide();

    public double answer();            // return the top of the stack
    public int depth();                // return the stack depth
    public void clear();               // reset the stack
}

//////////TESTER INTERFACE THIS IS SUPPOSED TO WORK WITH

public class Tester {
    public static void main(String[] args) 
    {
        PostFixCalculator pc = new PostFixCalculator();

        System.out.print("Expecting an exception: ");
        try
        {
            pc.answer();
            System.out.println("you should not ever see this message");
        }
        catch (Exception e)
        {
            System.out.println("expected exception caught");
        }

        pc.pushNumber(10);
        pc.pushNumber(20);
        pc.pushMultiply();

        System.out.println("Expecting depth of 1: " + pc.depth());
        System.out.println("Expecting answer of 200: " + pc.answer());

        pc.clear();
        System.out.println("Expecting depth of 0: " + pc.depth());

        System.out.print("Expecting an exception: ");
        try
        {
            pc.pushAdd();
            System.out.println("you should not ever see this message");
        }
        catch (Exception e)
        {
            System.out.println("Caught expected exception");
        }
        pc.clear();

        pc.pushNumber(5);
        pc.pushNumber(6);
        pc.pushNumber(7);
        pc.pushSubtract();
        System.out.println("expecting answer -1: " + pc.answer());
        System.out.println("expecting depth 2 (incomplete expression): " + pc.depth());
        pc.pushMultiply();
        pc.pushNumber(4);
        pc.pushMultiply();
        pc.pushNumber(2);
        pc.pushDivide();
        System.out.println("expecting answer -10: " + pc.answer());
        System.out.println("expecting depth 1: " + pc.depth());

        pc.clear();
        System.out.println("Expecting depth of 0: " + pc.depth());
    }
}

Homework Answers

Answer #1

Short Summary:

  • Provided the source code and sample output as per the requirements.

Source Code:

PostFixCalculator.java:

import java.util.Stack;

public class PostFixCalculator {

private final Stack<Double> calcStack = new Stack<>();
private double result;

// Push number to the stack
public void pushNumber(double d) {
calcStack.push(d);
}

// addition method
public void pushAdd() {
result += calcStack.pop();

}

// subtraction method
public void pushSubtract() {
result -= calcStack.pop();
}

// multiplication method
public void pushMultiply() {
result *= calcStack.pop();
}

// division method
public void pushDivide() {
result /= calcStack.pop();
}

// returns the result
public double answer() {

return result;
}

// size of the stack
public int depth() {
return calcStack.size();
}

// clears the stack
public void clear() {
calcStack.clear();
}
}

Tester.java:

public class Tester {

public static void main(String[] args) {
PostFixCalculator pc = new PostFixCalculator();

System.out.print("Expecting an exception: ");
try {
pc.answer();
System.out.println("you should not ever see this message");
} catch (Exception e) {
System.out.println("expected exception caught");
}

pc.pushNumber(10);
pc.pushNumber(20);
pc.pushMultiply();

System.out.println("Expecting depth of 1: " + pc.depth());
System.out.println("Expecting answer of 200: " + pc.answer());

pc.clear();
System.out.println("Expecting depth of 0: " + pc.depth());

System.out.print("Expecting an exception: ");
try {
pc.pushAdd();
System.out.println("you should not ever see this message");
} catch (Exception e) {
System.out.println("Caught expected exception");
}
pc.clear();

pc.pushNumber(5);
pc.pushNumber(6);
pc.pushNumber(7);
pc.pushSubtract();
System.out.println("expecting answer -1: " + pc.answer());
System.out.println("expecting depth 2 (incomplete expression): " + pc.depth());
pc.pushMultiply();
pc.pushNumber(4);
pc.pushMultiply();
pc.pushNumber(2);
pc.pushDivide();
System.out.println("expecting answer -10: " + pc.answer());
System.out.println("expecting depth 1: " + pc.depth());

pc.clear();
System.out.println("Expecting depth of 0: " + pc.depth());
}
}

Sample Run:

**************************************************************************************

Feel free to rate the answer and comment your questions, if you have any.

Please upvote the answer and appreciate our time.

Happy Studying!!!

**************************************************************************************

Know the answer?
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
Similar Questions
Consider a generic Stack class implemented using linked list with the following methods: public boolean isEmpty(),...
Consider a generic Stack class implemented using linked list with the following methods: public boolean isEmpty(), which returns true if and only if the stack is empty; public T pop() throws StackUnderflowException, which removes and returns the top element of the stack (if the stack is empty, it throws StackUnderflowException); public T peek() throws StackUnderflowException, which returns the top element of the stack (but does not remove it; it throws exception if stack is empty); public void push(T element), which...
class Car{ private String make; public Car(String make){ this.make = make; } public String toString(){ return...
class Car{ private String make; public Car(String make){ this.make = make; } public String toString(){ return make; } } class Node<T>{ public T data; public Node next; public Node(T data){ this.data = data; this.next = null; } } public class StackLinkedList<T>{ //Single field of type Node    //to represent the front of the stack //operation push() /* The push operation is equivalent to the inserting a node at the head of the list. */ public void push(T data){ } //operation...
Create a class StacktwoPopArr using Arrays, Change the pop function so that two elements get popped...
Create a class StacktwoPopArr using Arrays, Change the pop function so that two elements get popped everytime instead of the one. If there is only one element in the stack, just pop the one element and report that the stack is now empty. If stack is empty, it should just report that stack is empty. All other functions for StackArray remain the same. using java StackArray.java: public class StackArray {       private final int size = 20; //Size of...
(JAVA) Why is my toString method not printing out the last node? It will not print...
(JAVA) Why is my toString method not printing out the last node? It will not print out "A" which is for Alice. Everything else is working just fine except for this. package driver; import exception.StackException; import stack.*; public class StackDriver { public static void main(String[] args) throws Exception { StackInterface<Painting> painting = new LinkedStack <Painting>(); try { System.out.println("Peeking at top of player stack.\n" + painting.peek()); }catch (StackException e) { System.out.println(e); System.out.println("Let's double check if it's empty: " + painting.isEmpty()); }...
4.2.2 Basic while loop with user input. JAVA Write an expression that executes the loop while...
4.2.2 Basic while loop with user input. JAVA Write an expression that executes the loop while the user enters a number greater than or equal to 0. Note: These activities may test code with different test values. This activity will perform three tests, with user input of 9, 5, 2, -1, then with user input of 0, -17, then with user input of 0 1 0 -1. See "How to Use zyBooks". Also note: If the submitted code has an...
JAVA public class Purchase { private String name; private int groupCount; //Part of price, like the...
JAVA public class Purchase { private String name; private int groupCount; //Part of price, like the 2 in 2 for $1.99. private double groupPrice; //Part of price, like the $1.99 in 2 for $1.99. private int numberBought; //Total number being purchased. public Purchase () { name = "no name"; groupCount = 0; groupPrice = 0; numberBought = 0; } public Purchase (String name, int groupCount, double groupPrice, int numberBought) { this.name = name; this.groupCount = groupCount; this.groupPrice = groupPrice; this.numberBought...
Question: Squares. Write a program class named SquareDisplay that asks the user for a positive integer...
Question: Squares. Write a program class named SquareDisplay that asks the user for a positive integer no greater than 15. The program should then display a square on the screen using the character ‘X’. The number entered by the user will be the length of each side of the square. For example, if the user enters 5, the program should display the following:       XXXXX       XXXXX       XXXXX       XXXXX       XXXXX INPUT and PROMPTS. The program prompts for an integer as follows: "Enter...
Code in JAVA The requirements are as follows: The input will be in a text file...
Code in JAVA The requirements are as follows: The input will be in a text file whose name is given by arg[0] of main(). It will contain a fully-parenthesized infix expression containing only: "(", ")", "+", "-" and integers. Need help on the main and fixing the Queue. //Input: ( ( 1 + 2 ) - ( ( 3 - 4 ) + ( 7 - 2 ) ) ) ( ( 1 + 2 ) - ( 3 -...
Use Java: Also: Please include screenshots if possible. Create a class called AbstractStackTest. Write an abstract...
Use Java: Also: Please include screenshots if possible. Create a class called AbstractStackTest. Write an abstract method called makeStack that returns a Stack of Strings. Use the Stack interface as the return type, not a specific implementation! Write a class named NodeStackTest that extends your AbstractStackTest class. Implement the makeStack method to return a NodeStack. Repeat parts 1 and 2 for the Queue interface and the NodeQueue implementation. Write a new stack implementation, ArrayStack. It should be generic and use...
ALL CODE MUST BE IN C++ Before you begin, you must write yourself a LinkedList class...
ALL CODE MUST BE IN C++ Before you begin, you must write yourself a LinkedList class and a Node class (please name your classes exactly ​as I did here). Please follow the below specifications for the two classes. Node.cpp ● This must be a generic class. ● Should contain a generic member variable that holds the nodes value. ● Should contain a next and prev Node* as denoted here. ● All member variables should be private. ● Use public and...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT