Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The main method of your calculator program has started to get a little messy. In

ID: 3885640 • Letter: T

Question

The main method of your calculator program has started to get a little messy. In this assignment, you will clean it up some by moving some of your code into new methods. Methods allow you to organize your code, avoid repetition, and make aspects of your code easier to modify. While the calculator program is very simple, this assignment attempts to show you how larger, real world programs are structured. As a new programmer in a job, you will likely not be developing new programs by yourself, completely from scratch. Instead, it is likely that you will be asked to modify or write a new method within an existing program. It will help to ease your transition from school to work if you have been exposed to realistic program organization. The new methods you will need to implement are listed below. While programming is often a very creative exercise, there are also times when you will need to be able to code to requirements such that what you write will integrate seamlessly with what other developers working on the same project will write. Because of this, for this assignment you will need to implement these methods with the exact signatures shown here. public static int getMenuOption() public static double getOperand(String prompt) public static double add(double operand1, double operand2) public static double subtract(double operand1, double operand2) public static double multiply(double operand1, double operand2) public static double divide(double operand1, double operand2) public static double random(double lowerLimit, double upperLimit) The getMenuOption method should display the menu to the user and read in their option. If the option is invalid, the method should inform the user and re-prompt them. This should continue until the user enters a valid option. Once the user has entered a valid choice, the getMenuOption method should return that choice to the calling method. The getOperand method should display the prompt to the user and then read in a double value from the user. This value should be returned to the calling method. The intent is that you will be able to use this method to gather operands for both the standard arithmetic functions (add, subtract, multiply, and divide) and for the random number generation. For instance, in the case of subtract, you would do something like this: double operand1 = getOperand(“What is the first number? “); double operand2 = getOperand(“What is the second number? “); // call your subtract method and pass it these inputs Copyright © 2017, 2018 Sinclair Community College. All Rights Reserved. For the case of random number generation, you would do something like this: double lowerLimit = getOperand(“What is the lower limit? “); double upperLimit = getOperand(“What is the upper limit? “); // all your random number generation method and pass it these inputs The add, subtract, multiply, divide, and random methods are pretty straightforward. For the divide method, if the second operand is zero, return the special value Double.NaN. This stands for “not a number.” We will discuss this more in later chapters. Once you have written these new methods, rewrite the main method of your calculator program to use these methods whenever possible. The output (other than the special case of dividing by zero) should be identical to the output for last week. Because you are reorganizing the program rather than adding any new functionality, your calculator should be able to pass the same tests that were included in the previous assignment (Third Assignment – Menu-Driven Calculator). You will be graded according to the following rubric (each item is worth one point): The getMenuOption method displays the options to the user, gets their choice, and returns it The getOperand method prints out the prompt that was passed in to the method, reads the double value the user enters in response, and returns it The add, subtract, multiply, and divide methods perform the corresponding mathematical operation. Division by zero causes the divide method to return Double.NaN The random method generates a random number between the lower and upper bounds passed to it and returns that number The main method uses the new methods you have developed whenever possible You program produces the correct output for all menu options Your program compiles Your program runs You follow standard coding conventions (e.g. variable names, indentation, comments, etc.)

___________________________________________________________________________________________________________________________________________

This is my current code, I am having trouble stopping it from dividing by zero as well...

import java.util.*;

import java.util.Scanner;

import java.util.Random;

public class MenuDrivenCalculator {

public static void main(String[] args)

{

int num1, num2, choice,count=0; //variable declaration

Scanner input = new Scanner (System.in); //scanner object creation

while(true) { //user interaction until quit performed.

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

System.out.println("What would you like to do?"); //ask user choice

choice = input.nextInt();

switch (choice) {

case 1: //choice is add

System.out.println("What is the first number?");

num1 = input.nextInt(); //input first number

System.out.println("What is the second number?");

num2 = input.nextInt(); //input second number

System.out.println(add( num1,num2)); //add function calling and print result

count=0;

break;

case 2:System.out.println("What is the first number?"); //if choice is subtraction

num1 = input.nextInt(); //input first number

System.out.println("What is the second number?");

num2 = input.nextInt(); //input second number

System.out.println(sub( num1,num2)); //subtraction function calling and print result

count=0;

break;

case 3:

System.out.println("What is the first number?");

num1 = input.nextInt();

System.out.println("What is the second number?"); //multiplication case

num2 = input.nextInt();

System.out.println(mult( num1,num2));

count=0;

break;

case 4:

System.out.println("What is the first number?");

num1 = input.nextInt();

System.out.println("What is the second number?");

//division case

num2 = input.nextInt();

System.out.println(div( num1,num2));

count=0;

break;

case 5:

System.out.println("What is the first number?");

num1 = input.nextInt();

System.out.println("What is the second number?"); //random number case

num2 = input.nextInt();

System.out.println(rand( num1,num2));

count=0;

break;

case 6:

System.exit(0); //if user enters quit then exit the program

default:

count++; //counting for 3 invalid reasons

System.out.println("Sorry that isn't a valid option.");

}

if(count==3) System.exit(0); } //checking for 3 invalid reasons in a row

}

public static int add(int x, int y)

{

int result = x + y;

return result;

}

public static int sub(int x, int y)

{

int result = x-y;

return result;

}

public static int mult(int x, int y)

{

int result = x*y;

return result;

}

public static double div(int x, int y)

{

double result = x/y;

return result;

}

public static double rand(int x, int y)

{

Random generator = new Random();

double result = generator.nextInt((y - x) + 1) + x;

return result;

}

}

Explanation / Answer

MenuDrivenCalculator.java

import java.util.Random;
import java.util.Scanner;

public class MenuDrivenCalculator {

// scanner object creation
static Scanner input = new Scanner(System.in);

public static void main(String[] args) {

//Declaring variables
double operand1, operand2, res;
String str1 = "What is the first number?";
String str2 = "What is the second number?";

//calling the method
int choice = getMenuOption();


//Based on the user choice the corresponding case will executed
switch (choice) {
case 1:
{
//calling the method to get the operands
operand1 = getOperand(str1);
operand2 = getOperand(str2);

//calling the method to perform the appropriate operation
res = add(operand1, operand2);
System.out.println(operand1 + " + " + operand2 + " = " + res);
break;
}
case 2:
{
//calling the method to get the operands
operand1 = getOperand(str1);
operand2 = getOperand(str2);

//calling the method to perform the appropriate operation
res = subtract(operand1, operand2);
System.out.println(operand1 + " - " + operand2 + " = " + res);
break;
}
case 3:
{
//calling the method to get the operands
operand1 = getOperand(str1);
operand2 = getOperand(str2);

//calling the method to perform the appropriate operation
res = multiply(operand1, operand2);
System.out.println(operand1 + " * " + operand2 + " = " + res);
break;
}
case 4:
{
//calling the method to get the operands
operand1 = getOperand(str1);
operand2 = getOperand(str2);
if (operand2 == 0) {
System.out.println("The Second Number is NAN .");
} else {
//calling the method to get the operands
operand1 = getOperand(str1);
operand2 = getOperand(str2);

//calling the method to perform the appropriate operation
res = divide(operand1, operand2);
System.out.println(operand1 + " / " + operand2 + " = " + res);
}
break;
}
case 5:
{
operand1 = getOperand("What is the upper limit ?");
operand2 = getOperand("What is the lower limit ?");

//calling the method to perform the appropriate operation
res = random(operand1, operand2);
System.out.println("The Random Number is :" + res);
break;
}

}

}

//This method returns the random number
private static double random(double x, double y) {
Random generator = new Random();

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

//This method will perform the division operation
private static double divide(double operand1, double operand2) {

return operand1 / operand2;
}

//This method will perform the multiply operation
private static double multiply(double operand1, double operand2) {
return operand1 * operand2;
}


//This method will perform the add operation
private static double add(double operand1, double operand2) {

return operand1 + operand2;
}

//This method will perform the subtract operation
private static double subtract(double operand1, double operand2) {

return operand1 - operand2;
}

//This method will get the operands entered by the user
private static double getOperand(String str) {

System.out.print(str);
double num = input.nextDouble();
return num;
}

//This method will display the menu
public static int getMenuOption() {

int 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;
}
}


}
}

______________________

Output:


Menu
1. Add
2. Subtract
3. Multiply
4. Divide
5. Generate Random Number
6.Quit
What would you like to do?
7
** Invalid Choice **

Menu
1. Add
2. Subtract
3. Multiply
4. Divide
5. Generate Random Number
6.Quit
What would you like to do?
1
What is the first number?23
What is the second number?34
23.0 + 34.0 = 57.0

_______________

Output#2:


Menu
1. Add
2. Subtract
3. Multiply
4. Divide
5. Generate Random Number
6.Quit
What would you like to do?
5
What is the upper limit ?10
What is the lower limit ?100
The Random Number is :80.0


_____________Could you rate me well.Plz .Thank You