Java Assignment My codes needs to show The current value is 0.0 above the Menu t
ID: 3906093 • Letter: J
Question
Java Assignment
My codes needs to show The current value is 0.0 above the Menu to start. It already displays current value after each one.
For Case 5 in the Memory.java, I need it to clear the current value and reset it to 0.0. It should also display "The current value is 0.0" after it clears the value.
Here is my code in the two parts.
Calculator.java
import java.util.Scanner;
public class Calculator {
public static double currentValue=0; //Variable to hold result
public Calculator(double currentValue)
{
this.currentValue = currentValue;
}
public static int displayMenu(Scanner choose) // Displaying the user's menu
{
int choice;
System.out.println(" ");
System.out.println("Menu ");
System.out.println("1. Add");
System.out.println("2. Subtract");
System.out.println("3. Multiply");
System.out.println("4. Divide");
System.out.println("5. Clear");
System.out.println("6. Quit");
System.out.println(" ");
System.out.print("What would you like to do? ");
choice = choose.nextInt();
return choice;
}
public static double getOperand(String prompt)
{
Scanner choose = new Scanner(System.in);
System.out.print(prompt);
double option = choose.nextDouble();
return option;
}
public double getCurrentValue()
{
return this.currentValue;
}
public void add(double operand2)
{
this.currentValue += operand2;
}
public void subtract(double operand2)
{
this.currentValue -= operand2;
}
public void multiply(double operand2)
{
this.currentValue *= operand2;
}
public void divide(double operand2)
{
if (operand2 == 0)
{
System.out.println("The current value is NaN. ");
clear();
}
else
{
this.currentValue /= operand2;
}
}
public void clear()
{
this.currentValue = 0;
}
}
Memory.java
import java.util.Scanner;
public class Memory {
public static void main(String[] args) // Main function
{
Calculator c = new Calculator(0);
Scanner choose = new Scanner(System.in);
int choice = 0;
String prompt;
boolean right = false;
double operand2;
do // Loop until user wants to quit
{
if(c.getCurrentValue() > 0)
System.out.println("The current value is " + c.getCurrentValue()); //Displaying current value
choice = c.displayMenu(choose); //Displaying the user's menu
if ((choice <= 6) && (choice > 0)) //Checking for a valid option to be selected
{
switch(choice) //Calling the appropriate function
{
case 1: operand2 = c.getOperand("What is the second number? ");
c.add(operand2);
break;
case 2: operand2 = c.getOperand("What is the second number? ");
c.subtract(operand2);
break;
case 3: operand2 = c.getOperand("What is the second number? ");
c.multiply(operand2);
break;
case 4: operand2 = c.getOperand("What is the second number? ");
c.divide(operand2);
break;
case 5:
case 6: c.currentValue = 0;
System.out.println("Goodbye!");
break;
}
}
else
{
right = true;
System.out.println("I'm sorry, " + choice + " wasn't one of the options. Please try again.");
}
}while (choice != 6);
}
}
Explanation / Answer
Given below is the code for the question.
To indent code in eclipse , select code by pressing ctrl+a and then indent using ctrl+i
Please do rate the answer if it was helpful. Thank you
import java.util.Scanner;
public class Memory {
public static void main(String[] args) // Main function
{
Calculator c = new Calculator(0);
Scanner choose = new Scanner(System.in);
int choice = 0;
double operand2;
do // Loop until user wants to quit
{
System.out.println(" The current value is " + c.getCurrentValue()); // Displaying
// current
// value
choice = c.displayMenu(choose); // Displaying the user's menu
switch (choice) // Calling the appropriate function
{
case 1:
operand2 = c.getOperand("What is the second number? ");
c.add(operand2);
break;
case 2:
operand2 = c.getOperand("What is the second number? ");
c.subtract(operand2);
break;
case 3:
operand2 = c.getOperand("What is the second number? ");
c.multiply(operand2);
break;
case 4:
operand2 = c.getOperand("What is the second number? ");
c.divide(operand2);
break;
case 5:
c.currentValue = 0;
break;
case 6:
c.currentValue = 0;
System.out.println("Goodbye!");
break;
default:
System.out.println("I'm sorry, " + choice + " wasn't one of the options. Please try again.");
}
} while (choice != 6);
}
}
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? 4
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? 4
What is the second number? 2
The current value is 2.0
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? 3
The current value is 3.0
Menu
1. Add
2. Subtract
3. Multiply
4. Divide
5. Clear
6. Quit
What would you like to do? 6
Goodbye!
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.