Question

Java Assignment Calculator with methods. My code appears below what I need to correct. What I...

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("\nMenu\n1. Add\n2. Subtract\n3. Multiply\n4. Divide\n5. Generate Random Number\n6. Quit");

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

choice = input.nextInt();

return choice;

}

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

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

// if operand2 is 0 than return Nan

if(operand2==0)

return Double.NaN;

return operand1 / operand2;

}

// Random number

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

// using random from math class to generate within range

return Math.random() * ((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("\nMenu\n1. Add\n2. Subtract\n3. Multiply\n4. Divide\n5. Generate Random Number\n6. Quit");

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

choice = input.nextInt();

return choice;

}

}

Add a comment
Know the answer?
Add Answer to:
Java Assignment Calculator with methods. My code appears below what I need to correct. What I...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • The main method of your calculator program has started to get a little messy. In this...

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

  • In this assignment, you will write a subclass of the MemoryCalc class from Sixth Assignment – Mem...

    In this assignment, you will write a subclass of the MemoryCalc class from Sixth Assignment – Memory Calculator. This new calculator, called ScientificMemCalc, should be able to do everything that the MemoryCalc could do, plus raise the current value to a power and compute the natural logarithm of the current value. This is a fairly realistic assignment – often when you are working for a company, you will be asked to make minor extensions to existing code. You will need...

  • LANGUAGE IS JAVA CALCULATOR USING SCIENTIFIC/STANDARD MODE ERRORS: ADDITION AND DIVISION NOT WORKING FOR EITHER CALCULATOR...

    LANGUAGE IS JAVA CALCULATOR USING SCIENTIFIC/STANDARD MODE ERRORS: ADDITION AND DIVISION NOT WORKING FOR EITHER CALCULATOR CODE: import java.util.Scanner; public class CalculatorProject { public static void main (String[] args) { Scanner yurr = new Scanner(System.in);    String mode; double i; String choice; String answer = "Y"; while (answer.equals("Y")) { System.out.println("Enter the calculator mode: Standard/Scientific?"); mode = yurr.next(); if(mode.equals("Scientific")) { System.out.print("Enter '+' for addition, '-' for subtractions, '*' for multiplication, '/' for division, 'sin' for sin x, 'cos' for cos x,...

  • Breaking it Down Problem One problem that I find in new Java programmers is a tendency...

    Breaking it Down Problem One problem that I find in new Java programmers is a tendency to put too much code in "main()". It is important to learn how to break a program down into methods and classes. To give you practice at this, I have a program that has too much code in main. Your job will be to restructure the code into appropriate methods to make it more readable. I will provide you with lots of coaching on...

  • Could someone re-write this code so that it first prompts the user to choose an option...

    Could someone re-write this code so that it first prompts the user to choose an option from the calculator (and catches if they enter a string), then prompts user to enter the values, and then shows the answer. Also, could the method for the division be rewritten to catch if the denominator is zero. I have the bulk of the code. I am just having trouble rearranging things. ------ import java.util.*; abstract class CalculatorNumVals { int num1,num2; CalculatorNumVals(int value1,int value2)...

  • Hi. This is a prototype of Java. The following Java program was developed for prototyping a...

    Hi. This is a prototype of Java. The following Java program was developed for prototyping a mini calculator. Run the program and provide your feedback as the user/project sponsor. (Save the code as MiniCalculator.java; compile the file: javac MiniCalculator.java; and then run the program: java MiniCalculator). HINTs: May give feedback to the data type and operations handled by the program, the way the program to get numbers and operators, the way the calculation results are output, the termination of the...

  • In Java. Please use the provided code Homework 5 Code: public class Hw05Source { public static...

    In Java. Please use the provided code Homework 5 Code: public class Hw05Source { public static void main(String[] args) { String[] equations ={"Divide 100.0 50.0", "Add 25.0 92.0", "Subtract 225.0 17.0", "Multiply 11.0 3.0"}; CalculateHelper helper= new CalculateHelper(); for (int i = 0;i { helper.process(equations[i]); helper.calculate(); System.out.println(helper); } } } //========================================== public class MathEquation { double leftValue; double rightValue; double result; char opCode='a'; private MathEquation(){ } public MathEquation(char opCode) { this(); this.opCode = opCode; } public MathEquation(char opCode,double leftVal,double rightValue){...

  • JAVA: amortization table: Hi, I have built this code and in one of my methods: printDetailsToConsole...

    JAVA: amortization table: Hi, I have built this code and in one of my methods: printDetailsToConsole I would like it to print the first 3 payments and the last 3 payments if n is larger than 6 payments. Please help! Thank you!! //start of program import java.util.Scanner; import java.io.FileOutputStream; import java.io.FileNotFoundException; import java.io.PrintWriter; import java.lang.Math; //345678901234567890123456789012345678901234567890123456789012345678901234567890 /** * Write a description of class MortgageCalculator here. * * @author () * @version () */ public class LoanAmortization {    /* *...

  • need help editing or rewriting java code, I have this program running that creates random numbers...

    need help editing or rewriting java code, I have this program running that creates random numbers and finds min, max, median ect. from a group of numbers,array. I need to use a data class and a constructor to run the code instead of how I have it written right now. this is an example of what i'm being asked for. This is my code: import java.util.Random; import java.util.Scanner; public class RandomArray { // method to find the minimum number in...

  • JAVA I need a switch so that users can input one selection then another. These selections...

    JAVA I need a switch so that users can input one selection then another. These selections are for a simple calculator that uses random numbers 1. + 2. - 3. * 4./ 5. RNG. This has to be simple this is a beginners class. Here is what I have import java.util.Scanner; import java.util.Random; import java.util.*; public class You_Michael02Basic_Calculator {    public static void main(String[] args) {        // Generate random numbers        int num1,num2;        num1 =(int)(Math.random()*100+1);...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT