Question

In: Computer Science

For this assignment we're going to make another calculator. This one will be a simple four...

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!

Solutions

Expert Solution

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


Related Solutions

Hangman We're going to write a game of hangman. Don't worry, this assignment is not nearly...
Hangman We're going to write a game of hangman. Don't worry, this assignment is not nearly as difficult as it may appear. The way hangman works (for this assignment - we are doing a simplified game) is as follows: the computer will choose a word. (For this version, a word is selected from a static list encoded into the program -- so the words are pretty limited). Let's say the word is "cocoa". the computer shows the user how many...
This week, we're going to be discussing Amazon and it's ascent to become one of the...
This week, we're going to be discussing Amazon and it's ascent to become one of the largest global online retailers. Get ready for another great set of conversations! https://www.youtube.com/watch?v=YlgkfOr_GLY Why do you think has Amazon succeeded when so many other companies have failed? How did their First Mover's Advantage factor into their ubiquity? How long did it take for Amazon to become profitable? What’s next for Amazon... Cloud computing? Deliveries by drone? Travel? Retail? You should research the company and...
4) We're going to test the same hypothesis four ways. Assume the people in the dataset...
4) We're going to test the same hypothesis four ways. Assume the people in the dataset in armspanSpring2020.csv are a random sample of all adults. For each test, report the test statistic and the p-value. With a 5% significance level, give the conclusion of each test. a) Test the hypothesis that the mean difference between armspan and height it not equal to 0, using the data in armspanSpring2020.csv. Do this by creating a new variable named diff = (armspan -...
For this assignment you will develop pseudocode and write a C++ program for a simple calculator....
For this assignment you will develop pseudocode and write a C++ program for a simple calculator. You will create both files in Codio. Put your pseudocode and C++ code in the files below. PSEUDOCODE FILE NAME: Calculator.txt C++ SOURCE CODE FILE NAME : Calculator.cpp DESCRIPTION: Write a menu-driven program to perform arithmetic operations and computations on a list of integer input values. Present the user with the following menu. The user will choose a menu option. The program will prompt...
Decision Structures Calorie Calculator Assignment Overview This assignment will give you practice with numerical calculations, simple...
Decision Structures Calorie Calculator Assignment Overview This assignment will give you practice with numerical calculations, simple input/output, and if-else statements. Program Objective: In this exercise, you will write a program the calculates the Daily Caloric needs for weight lose diet. According to everydayhealth.com they recommend this daily caloric formula for a weight loss diet: BMR + Activity Level - 500 calories. Calories Calculator The Harris-Benedict equation estimates the number of calories your body needs to maintain your weight if you...
PYTHON: Im writing a program to make a simple calculator that can add, subtract, multiply, divide...
PYTHON: Im writing a program to make a simple calculator that can add, subtract, multiply, divide using functions. It needs to ask for the two numbers from the user and will ask the user for their choice of arithmetic operation 1- subtract 2- add 3- divide 4- multiply
Do it in python please This lesson's Group Activities are: We're going to take the Group...
Do it in python please This lesson's Group Activities are: We're going to take the Group Activity from last week and tweak it. Instead of storing the random numbers in a list, we're going to store them in a file. Write a program using functions and mainline logic which prompts the user to enter a number, then generates that number of random integers and stores them in a file. It should then display the following data to back to the...
Hello, In C#, we are creating a simple calculator that takes two operands and one operator...
Hello, In C#, we are creating a simple calculator that takes two operands and one operator and calculates a result. We need to include a private method to calculate the result as well as try-catch statements for exceptions. My code that i have so far is below, it executes and calculates, but there is an issue with the way i have done the method. Any help or info about my method errors would be great, Thank you! ********************************** using System;...
n this problem, we're going get a rough estimate the amount of uranium fuel it would...
n this problem, we're going get a rough estimate the amount of uranium fuel it would take if the US recieved all its electrical power from nuclear power plants. The size of a power plant in normally given as the about of electrical power it can produce when running a full capacity. This electrical power produced can be very different than the mechanical or thermal power that was required to produce this electricity. For example, power plant might have a...
n the instructions for this Task C, we're going to assume that you have completed your...
n the instructions for this Task C, we're going to assume that you have completed your script for Task B above; i.e. that you have a working dictionary-based version of the functions (originally named addToRecord(), etc. in Task A) now named addToHistogram(), etc. Let's assume that your dictionary-based function (from Task B above) that creates a histogram is named makeHistogram(). You are going to use your makeHistogram() in the following sub-tasks. In fact, you're welcome to include any function you...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT