Question: Java Assignment Calculator with methods. My code appears below what I need to correct. What I need to correct is if the user attempts to
Java Assignment
Calculator with methods. My code appears below what I need to correct.
What I need to correct is
if the user attempts to divide a number by 0, the divide() method is supposed to return Double.NaN, but your divide() method doesn't do this. Instead it does this:
public static double divide(double operand1, double operand2) { return operand1 / operand2; }
The random method is supposed to return a double within a lower limit and an upper limit, not an int type cast to a double.
public static double random(double x, double y) { Random generator = new Random(); return generator.nextInt((int)(y - x) + 1) + x; }
Code Starts Here.
import java.util.Random;
import java.util.Scanner;
public class CalculatorWithMethods {
static Scanner input = new Scanner(System.in);
boolean mainloop = true;
public static void main(String[] args) {
double res;
int count = 0;
while(true) {
int choice = (int) getMenuOption();
switch (choice) {
case 1:
{
//Method for getting 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:
{
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:
{
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:
{
double operand1 = getOperand("What is the first number?");
double operand2 = getOperand("What is the second number?");
if (operand2 == 0) {
System.out.println("Double.NaN");
} else {
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;
}
default: {
count++;
System.out.println("** Invalid Choice **");
}
}
if(count == 3){
break;
}
}
}
//Addition.
public static double add(double operand1, double operand2) {
return operand1 + operand2;
}
//Subtraction.
public static double subtract(double operand1, double operand2) {
return operand1 - operand2;
}
//Multiplication
public static double multiply(double operand1, double operand2) {
return operand1 * operand2;
}
//Division
public static double divide(double operand1, double operand2) {
return operand1 / operand2;
}
//Random number
public static double random(double x, double y) {
Random generator = new Random();
return generator.nextInt((int)(y - x) + 1) + x;
}
//Getting the operands.
public static double getOperand(String str) {
System.out.print(str);
double num = input.nextDouble();
return num;
}
//Display menu
public static double getMenuOption() {
double choice;
System.out.println(" Menu 1. Add 2. Subtract 3. Multiply 4. Divide 5. Generate Random Number 6. Quit");
System.out.println("What would you like to do?");
choice = input.nextInt();
return choice;
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
