Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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!

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions