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()); } }
Short Summary:
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!!!
**************************************************************************************
Get Answers For Free
Most questions answered within 1 hours.