In: Computer Science
For this assignment we're going to make another calculator. This
one will be a simple
four function (add, subtract, multiply, and divide) calculator, but
it will have state.
Specifically, the calculator will keep track of the result of the
most recent operation and
use that value as the first operand for the next operation. Take a
look at the sample
output below if this doesn't quite make sense. Your new calculator
class 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 these
options: add, subtract,
multiply, divide, clear, and quit. If the user has entered an
invalid option, it should reprompt them. Once a valid option has
been entered, the method should return it. The
getOperand method displays the prompt to the user, reads in a
double value, and
returns it. The getCurrentValue method just returns the number
stored in the
currentValue field. The add, subtract, multiply, and divide methods
now only need to
take one parameter because the first operand will be the
currentValue field. Also, these
methods do not need to return the result. Instead, they should
store it in the
currentValue field. The clear method should reset the currentValue
to zero. DO NOT
make the currentValue field static. With currentValue as an
instance field, we can
eventually write a program that allows users to have multiple
calculators running, each
computing different things. Note that because currentValue is an
instance field, the
methods that change it, such as add, subtract, multiply, and
divide, must be instance
methods. Methods such as displayMenu and getOperand can remain
static because
they do not need to access the state of the calculator. Once you
have all of the methods
written, write a main method that creates an instance of your
calculator 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 the
user 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!
IF YOU HAVE ANY DOUBTS COMMENT BELOW I WILL BE THERE TO HELP YOU
ANSWER:
CODE:
import java.util.Scanner; public class Calculator { static Scanner keybd = new Scanner(System.in); private double currentValue = 0; public static int displayMenu() { System.out.print( "Menu 1. Add " + "2. Subtract " + "3. Multiply " + "4. Divide " + "5. Clear " + "6. Quit " + "What would you like to do? "); return Integer.parseInt(keybd.nextLine()); } public static double getOperand(String prompt) { System.out.print(prompt + " "); return Double.parseDouble(keybd.nextLine()); } public double getCurrentValue() { return currentValue; } public void add(double operand2) { currentValue += operand2; } public void subtract(double operand2) { currentValue -= operand2; } public void multiply(double operand2) { currentValue *= operand2; } public void divide(double operand2) { if (operand2 == 0) { currentValue = Double.NaN; return; } currentValue /= operand2; } public void clear() { currentValue = 0; } public static void main(String[] args) { Calculator c = new Calculator(); System.out.println("The current value is " + c.getCurrentValue()); int choice = displayMenu(); while (choice != 6) { switch (choice) { case 1: c.add(getOperand("What is the second number?")); System.out.println("The current value is " + c.getCurrentValue()); break; case 2: c.subtract(getOperand("What is the second number?")); System.out.println("The current value is " + c.getCurrentValue()); break; case 3: c.multiply(getOperand("What is the second number?")); System.out.println("The current value is " + c.getCurrentValue()); break; case 4: c.divide(getOperand("What is the second number?")); System.out.println("The current value is " + c.getCurrentValue()); break; case 5: c.clear(); System.out.println("The current value is " + c.getCurrentValue()); break; default: System.out.println("Invalid Choice!"); break; } System.out.println(); choice = displayMenu(); } System.out.println("Good Bye!"); } }
HOPE IT HELPS YOU
RATE THUMBSUP PLEASE