Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Java Question. Here's hoping it's someone different. The previous two answers to this have been in cursive handwriting and then scanned in and I couldn't

Java Question. Here's hoping it's someone different. The previous two answers to this have been in cursive handwriting and then scanned in and I couldn't read it.

The problem I am having is that I can't get the menu to return after each user selection. The only time it does that is when a user makes an invalid choice. That part works correctly. It also needs to close if the person makes three incorrect selections in a row.

import java.util.Random;

import java.util.Scanner;

public class CalculatorWithMethods {

// scanner object creation

static Scanner input = new Scanner(System.in);

boolean mainloop = true;

public static void main(String[] args) {

//Declaring variables

double res;

int choice = (int) getMenuOption();

switch (choice) {

case 1:

{

//calling the method to get the operands

double operand1 = getOperand("What is the first number?");

double operand2 = getOperand("What is the second number?");

res = add(operand1, operand2);

System.out.println(operand1 + " + " + operand2 + " = " + res);

break;

}

case 2:

{

//calling the method to get the operands

double operand1 = getOperand("What is the first number?");

double operand2 = getOperand("What is the second number?");

res = subtract(operand1, operand2);

System.out.println(operand1 + " - " + operand2 + " = " + res);

break;

}

case 3:

{

//calling the method to get the operands

double operand1 = getOperand("What is the first number?");

double operand2 = getOperand("What is the second number?");

res = multiply(operand1, operand2);

System.out.println(operand1 + " * " + operand2 + " = " + res);

break;

}

case 4:

{

//calling the method to get the operands

double operand1 = getOperand("What is the first number?");

double operand2 = getOperand("What is the second number?");

if (operand2 == 0) {

System.out.println("NAN.");

} else {

//calling the method to get the operands

operand1 = getOperand("What is the first number?");

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

res = divide(operand1, operand2);

System.out.println(operand1 + " / " + operand2 + " = " + res);

}

break;

}

case 5:

{

double operand1 = getOperand("What is the lower limit ?");

double operand2 = getOperand("What is the upper limit ?");

res = random(operand1, operand2);

System.out.println("The Random Number is :" + res);

break;

}

}

}

//This method will perform the add operation

public static double add(double operand1, double operand2) {

return operand1 + operand2;

}

//This method will perform the subtract operation

public static double subtract(double operand1, double operand2) {

return operand1 - operand2;

}

//This method will perform the multiply operation

public static double multiply(double operand1, double operand2) {

return operand1 * operand2;

}

//This method will perform the division operation

public static double divide(double operand1, double operand2) {

return operand1 / operand2;

}

//This method returns the random number

public static double random(double x, double y) {

Random generator = new Random();

return generator.nextInt((int)(y - x) + 1) + x;

}

//This method will get the operands entered by the user

public static double getOperand(String str) {

System.out.print(str);

double num = input.nextDouble();

return num;

}

//This method will display the menu

public static double getMenuOption() {

double choice;

// user interaction until quit performed.

while (true) {

// displaying menu

System.out.println(" Menu 1. Add 2. Subtract 3. Multiply 4. Divide 5. Generate Random Number 6. Quit");

// ask user choice

System.out.println("What would you like to do?");

choice = input.nextInt();

if (choice < 1 || choice > 5) {

System.out.println("** Invalid Choice **");

continue;

} else {

return choice;

}

}

}

}

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

Step: 3

blur-text-image

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

Seven NoSQL Databases In A Week Get Up And Running With The Fundamentals And Functionalities Of Seven Of The Most Popular NoSQL Databases

Authors: Aaron Ploetz ,Devram Kandhare ,Sudarshan Kadambi ,Xun Wu

1st Edition

1787288862, 978-1787288867

More Books

Students also viewed these Databases questions

Question

Describe the general characteristics of carbohydrates.

Answered: 1 week ago