Question

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

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

This was my first one

   import java.util.Scanner;

   import java.util.Random;

public class BasicCalcutlator {


   public static void main(String[] args) {

  

   System.out.print("Menu\n1. Add\n2. Subtract\n3. Multiply\n4. Divide\n5. Generate Random Number\nWhat would you like to do?");

   Scanner keyboard = new Scanner(System.in);

   int option = keyboard.nextInt();

   if(option < 1 || option > 5) // invalid

   System.out.printf("I'm sorry, %d wasn't one of the options", option);

else

   {

if(option == 5) //User input

       {System.out.print("\nWhat is the lower limit? ");
  
       double lower = keyboard.nextInt();
      
  
       System.out.print("What is the upper limit? ");
  
       double upper = keyboard.nextInt();
      
  
       Random r = new Random();
  
       System.out.print("Answer: "+(lower + (upper - lower) * r.nextDouble()));}
      

else

   {
  
   System.out.print("\nWhat is the first number? ");

   double one = keyboard.nextInt();
  

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

   double two = keyboard.nextInt();
  

   if(option == 1) // prints output

   System.out.print("Answer: "+(one+two));

   else if(option == 2)

   System.out.print("Answer: "+(one-two));

   else if(option == 3)

   System.out.print("Answer: "+(one*two));

   else if(option == 4)

       {if(two == 0) // Invalid if 0

           System.out.print("I'm sorry, you can't divide by zero.");

           else

           System.out.print("Answer: "+(one/two));}
  
   }
   }
   }

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

Answer:-

import java.util.Random;

import java.util.Scanner;

class calculator

{

public static int getMenuOption(){

int option;

while(true)

{

System.out.println("Menu\n1. Add\n2. Subtract\n3. Multiply\n4. Divide\n5.Generate random number\n6. Quit");

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

Scanner sc=new Scanner(System.in);

option=sc.nextInt();

if(option>0&&option<7)

return option;

else

System.out.println("I'm sorry, "+option+" wasn't one of the options");

}

}

public static double getOperand(String prompt){

System.out.println(prompt);

Scanner sc=new Scanner(System.in);

return sc.nextDouble();

}

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

return operand1+operand2;

}

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

return operand1-operand2;

}

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

return operand1*operand2;

}

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

return operand1/operand2;

}

public static double random(double lowerLimit, double upperLimit){

Random r = new Random();

return (double)r.nextInt((int)(upperLimit-lowerLimit)) + lowerLimit;

}

public static void main(String[] args)

{

int option=getMenuOption();

double lowerLimit,upperLimit,operand1,operand2;

switch(option)

{

case 1:

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

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

System.out.println("Answer: "+add(operand1,operand2));

break;

case 2:

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

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

System.out.println("Answer: "+subtract(operand1,operand2));

break;

case 3:

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

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

System.out.println("Answer: "+multiply(operand1,operand2));

break;

case 4:

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

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

if(operand2==0){

System.out.println("I'm sorry, you can't divide by zero.");

System.out.println("Answer: NaN");

return;

} else {

System.out.println("Answer: "+divide(operand1,operand2));

}

break;

case 5:

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

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

System.out.println("Answer: "+random(lowerLimit,upperLimit));

break;

case 6:

System.out.println("Goodbye!");

break;

}

}

}

If you find any difficulty with the code, please let know know I will try for any modification in the code. Hope this answer will helps you. If you have even any small doubt, please let me know by comments. I am there to help you. Please give Thumbs Up,Thank You!! All the best

Add a comment
Know the answer?
Add Answer to:
The main method of your calculator program has started to get a little messy. In this...
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
  • 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...

  • I have provided a main method. Please add your fraction class. You need constructors, add, subtract,...

    I have provided a main method. Please add your fraction class. You need constructors, add, subtract, multiply, and divide methods, and a toString method. Your toString method needs to return the numerator followed by / followed by the denominator - NO spaces. DO NOT make any attempt to reduce the fractions (we shall do that later). Please add comments throughout. import java.util.Scanner; public class H4 { public static class Fraction { } public static void main(String[] args) { Fraction f=...

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

  • (Java with Netbeans) Programming. Please show modified programming code for already given Main class, and programming...

    (Java with Netbeans) Programming. Please show modified programming code for already given Main class, and programming for code for the new GameChanger class. // the code for Main class for step number 6 has already been given, please show modified progrraming for Main class and GameCHanger class, thank you. package .games; import java.util.Random; import java.util.Scanner; public class Main { public static void main(String args[]) { System.out.println("Welcome to the Number Guessing Game"); System.out.println(); Scanner sc = new Scanner(System.in); // Get upper...

  • Write another program called calculator that inherits from class average and also has methods that has:...

    Write another program called calculator that inherits from class average and also has methods that has: two methods multiply() to calculate the product of the numbers. One method will have two parameters which are both ints. The second method has three parameters: two ints and a Double. Another method “Power” that finds the power of a number using methods. For example, if the user types in: 5 and 3, the program should print out 125. 3. And another method “Factorial”...

  • Write another program called calculator that inherits from class average and also has methods that has:...

    Write another program called calculator that inherits from class average and also has methods that has: two methods multiply() to calculate the product of the numbers. One method will have two parameters which are both ints. The second method has three parameters: two ints and a Double. Another method “Power” that finds the power of a number using methods. For example, if the user types in: 5 and 3, the program should print out 125. 3. And another method “Factorial”...

  • Creating a Calculator Program

    Design a calculator program that will add, subtract, multiply, or divide two numbers input by a user.Your program should contain the following:-The main menu of your program is to continue to prompt the user for an arithmetic choice until the user enters a sentinel value to quit the calculatorprogram.-When the user chooses an arithmetic operation (i.e. addition) the operation is to continue to be performed (i.e. prompting the user for each number, displayingthe result, prompting the user to add two...

  • (How do I remove the STATIC ArrayList from the public class Accounts, and move it to...

    (How do I remove the STATIC ArrayList from the public class Accounts, and move it to the MAIN?) import java.util.ArrayList; import java.util.Scanner; public class Accounts { static ArrayList<String> accounts = new ArrayList<>(); static Scanner scanner = new Scanner(System.in);    public static void main(String[] args) { Scanner scanner = new Scanner(System.in);    int option = 0; do { System.out.println("0->quit\n1->add\n2->overwirte\n3->remove\n4->display"); System.out.println("Enter your option"); option = scanner.nextInt(); if (option == 0) { break; } else if (option == 1) { add(); } else...

  • CE – Return and Overload in C++ You are going to create a rudimentary calculator. The...

    CE – Return and Overload in C++ You are going to create a rudimentary calculator. The program should call a function to display a menu of three options: 1 – Integer Math 2 – Double Math 3 – Exit Program The program must test that the user enters in a valid menu option. If they do not, the program must display an error message and allow the user to reenter the selection. Once valid, the function must return the option...

  • this is for java class Define a utility class for displaying values of type double. Call...

    this is for java class Define a utility class for displaying values of type double. Call the class DoubleOut. Include all the methods from the class DollarFormat in Listing 6.14, all the methods from the class OutputFormat of Self-Test Question 30, and a method called scienceWrite that displays a value of type double using e notation, such as 2.13e–12. (This e notation is also called scientific notation, which explains the method name.) When displayed in e notation, the number should...

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