For this assignment we're going to make another calculator. Thisone will be a simple
four function (add, subtract, multiply, and divide) calculator, butit will have state.
Specifically, the calculator will keep track of the result of themost recent operation and
use that value as the first operand for the next operation. Take alook at the sample
output below if this doesn't quite make sense. Your new calculatorclass should have
the following fields and methods:
fields:
private double currentValue
methods:
public static int displayMenu()
public static double getOperand(String prompt)
public double getCurrentValue()
public void add(double operand2)
public void subtract(double operand2)
public void multiply(double operand2)
public void divide(double operand2)
public void clear()
The displayMenu option should allow the user to pick from theseoptions: add, subtract,
multiply, divide, clear, and quit. If the user has entered aninvalid option, it should reprompt them. Once a valid option hasbeen entered, the method should return it. The
getOperand method displays the prompt to the user, reads in adouble value, and
returns it. The getCurrentValue method just returns the numberstored in the
currentValue field. The add, subtract, multiply, and divide methodsnow only need to
take one parameter because the first operand will be thecurrentValue field. Also, these
methods do not need to return the result. Instead, they shouldstore it in the
currentValue field. The clear method should reset the currentValueto zero. DO NOT
make the currentValue field static. With currentValue as aninstance field, we can
eventually write a program that allows users to have multiplecalculators running, each
computing different things. Note that because currentValue is aninstance field, the
methods that change it, such as add, subtract, multiply, anddivide, must be instance
methods. Methods such as displayMenu and getOperand can remainstatic because
they do not need to access the state of the calculator. Once youhave all of the methods
written, write a main method that creates an instance of yourcalculator class. Write a
loop that displays the current value (initially it will be zero),asks the user what operation
they want to perform, computes the result, and repeats until theuser chooses to quit.
Sample output:
The current value is 0.0
Menu
1. Add
2. Subtract
3. Multiply
4. Divide
5. Clear
6. Quit
What would you like to do? 1
What is the second number? 6
The current value is 6.0
Menu
1. Add
2. Subtract
3. Multiply
4. Divide
5. Clear
6. Quit
What would you like to do? 2
What is the second number? 2
The current value is 4.0
Menu
1. Add
2. Subtract
3. Multiply
4. Divide
5. Clear
6. Quit
What would you like to do? 3
What is the second number? 12
The current value is 48.0
Menu
1. Add
2. Subtract
3. Multiply
4. Divide
5. Clear
6. Quit
What would you like to do? 4
What is the second number? 0
The current value is NaN
Menu
1. Add
2. Subtract
3. Multiply
4. Divide
5. Clear
6. Quit
What would you like to do? 5
The current value is 0.0
Menu
1. Add
2. Subtract
3. Multiply
4. Divide
5. Clear
6. Quit
What would you like to do? 1
What is the second number? 48
The current value is 48.0
Menu
1. Add
2. Subtract
3. Multiply
4. Divide
5. Clear
6. Quit
What would you like to do? 4
What is the second number? 12
The current value is 4.0
Menu
1. Add
2. Subtract
3. Multiply
4. Divide
5. Clear
6. Quit
What would you like to do? 6
Goodbye!