Write code but need to rework with separate class from the


Homework on memory calculator.

Write code but need to rework with separate class from the Test or Driver or Demo class that contains the main method. please help fix it.

import java.util.Scanner;

public class MemoryCalculator {
private double currentValue;
static Scanner input = new Scanner(System.in);
public static void main(String[] args) {
int menu = 0;
double operand2 = 0;

MemoryCalculator calculator = new MemoryCalculator();

do {
System.out.println("The current value is "
+ calculator.currentValue);

menu = MemoryCalculator.displayMenu();

switch(menu){
case 1:
operand2 = getOperand("What is the second number?: ");
calculator.add(operand2);
break;

case 2:
operand2 = getOperand("What is the second number?: ");
calculator.subtract(operand2);
break;

case 3:
operand2 = getOperand("What is the second number?: ");
calculator.multiply(operand2);
break;

case 4:
operand2 = getOperand("What is the second number?: ");
calculator.divide(operand2);
break;

case 5:
calculator.clear();
break;

}
} while (menu != 6);

}


//* display menu
public static int displayMenu() {

System.out.println("nMenu:");
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. Quitn");
System.out.print("What would you like to do? ");
int menuInput = input.nextInt();

if (menuInput 6) {
System.out.println( menuInput
+ " wasn't one of the options.n");
menuInput = 0;
}
else if (menuInput == 6) {
System.out.println("Goodbye!");
}

return menuInput;
}

public static double getOperand(String prompt) {
System.out.print(prompt);
double operand = input.nextDouble();
return operand;
}

public double getCurrentValue() {
return currentValue;
}

public void setCurrentValue(double currentValue) {
this.currentValue = currentValue;
}

// add
public void add(double operand2) {
currentValue += operand2;
}

// subtract
public void subtract(double operand2) {
currentValue -= operand2;
}

// multiply
public void multiply(double operand2) {
currentValue *= operand2;
}

// divide
public void divide(double operand2) {
if (operand2 > 0) {
currentValue /= operand2;

}
else{
currentValue = Double.NaN;
}
}

// clear
public void clear() {
currentValue = 0;
}

}

Solution Preview :

Prepared by a verified Expert
JAVA Programming: Write code but need to rework with separate class from the
Reference No:- TGS02386135

Now Priced at $15 (50% Discount)

Recommended (93%)

Rated (4.5/5)