Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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 1. Add 2. Subtract 3. Multiply 4. Divide 5. Generate Random Number What 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(" What 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(" What 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));} } } }

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

DATABASE Administrator Make A Difference

Authors: Mohciine Elmourabit

1st Edition

B0CGM7XG75, 978-1722657802

More Books

Students also viewed these Databases questions