Assignment #10
Implement a stack calculator
Due Date: Monday of Week 5
20 points
This project uses a linked list to store the values the user enters and the results of the calculations that are performed in a stack. This project uses nested if statements that require the use of the break and continue statements. This project will be tedious, but it's manageable.
Console
Operation
- This application implements a stack calculator that does arithmetic with doubles. It accepts commands in any of the following formats:
push double-value add sub mult div clear quit
Specifications
- The calculator itself should be implemented as a separate class named StackCalculator. This class should have the following methods:
- 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.
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 them, 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 first value into the second one, 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 |
Hint
- You can use the toArray method of the LinkedList class to implement the getValues method. Although this method is shown in figure 11-9 in the book, that figure doesn't indicate that to use it with a typed collection, you must specify an array of the correct type as a parameter. See the online documentation for more information. (It's also possible to implement this method without using the LinkedList's toArray method.)
Enhancements
- Provide alternate forms for the commands that invoke operations. For example, allow + for add, - for sub, * for mult, and / for div.
- Add additional commands to the calculator. For example, sqrt could calculate the square root of the number on top of the stack, and pow could pop the top two values off the stack, raise the second value to the power indicated by the first value, and push the result back on the stack.
- Eliminate the need to use the word "push" to push data onto the stack. Instead, the calculator should treat any command that consists of just a number as a request to push the number onto the stack.
- Name of program
- Programmer's name
- Current Date
- Computer system and compiler you are using
- Brief description of the program
Compile and run the Java program.
When you are ready, upload the java program by clicking the link below:I cannot grade if I don't get the files and receive your email.