Hi! I am new in java. I had a homework with memory calculator. I'll try to write my 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. I have no idea at now. Here my code
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;
}
}
Get Free Quote!
276 Experts Online