Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Java problem. I have to use public void add, public void subtract, public voide multiply, public void divide and public void clear as methods. They

Java problem. I have to use public void add, public void subtract, public voide multiply, public void divide and public void clear as methods. They cannot be public static void add, etc. Once I change them, I run into problems with my cases. Specifically the add(operand2), subtract(operand2), multiply(operand2), divide(operand2) and case5: case 6: lines.

I also have a problem with the System.out.println("The current value is" + currentValue); line. The correction is saying to make it a static but the instructions from my instructor say not to make the currentValue field static.

Here is my code.

import java.util.Scanner;

public class Memory {

public double currentValue=0; //Variable to hold result

public static void main(String[] args) // Main function

{

Scanner choose = new Scanner(System.in);

int choice = 0;

String prompt;

boolean right = false;

double operand2;

do // Loop until user wants to quit

{

System.out.println("The current value is " + currentValue); //Displaying current value

choice = 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 = getOperand("What is the second number? ");

add(operand2);

break;

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

subtract(operand2);

break;

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

multiply(operand2);

break;

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

divide(operand2);

break;

case 5: case 6: 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);

}

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 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)

{

System.out.println("The current value is NaN. ");

}

else

{

currentValue /= operand2;

}

}

public void clear()

{

currentValue = 0;

}

}

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

Database Systems On GPUs In Databases

Authors: Johns Paul ,Shengliang Lu ,Bingsheng He

1st Edition

1680838482, 978-1680838480

More Books

Students also viewed these Databases questions

Question

Select the OSI Model Layer that represents each device router

Answered: 1 week ago