Starting from:
$30

$24

Lab #5 Methods Solution

Lab Description




In this lab you will create a Java file, MyJavaLab05.java and complete the outlined objectives below. Make sure you read the instructions closely, and fulfill each requirement listed in each objective.







Note: orange text is what you print, blue text is the text the user typed in.







Objective 1




Print your name and student ID to the screen, in the format: YourLastName, YourFirstName, YourStudentID




Objective 2




In the main method, call a method named startProgram.







Objective 3




Create a method named startProgram that welcomes a user to working with numbers, and asks a user to enter two numbers, and then call a method menuOptions, passing the two numbers

Expected output:

Welcome to Working with Numbers! Please enter two numbers:

Number 1:

36.0

Number 2:

4.5







Objective 4




Create a method named menuOptions that has two decimal parameters, prompts the user with a menu using the parameters passed into the method:

Please select a menu option below:

1. Calculate 36.0 + 4.5

2. Calculate 36.0 - 4.5

3. Calculate 36.0 * 4.5

4. Calculate 36.0 / 4.5

5. Calculate 36.0 ^ 4.5

3







Based on the option selected, call the corresponding method to calculate the result:







Option



Method name



1



addNumbers



2



subtractNumbers



3



multiplyNumbers



4



divideNumbers



5



powerNumbers



anything not 1-5



No method needed, just print "Invalid menu option" and exit the method.



Each method should take two decimal parameters, and return a decimal result. With the returned result, print the result inside of the menuOptions method.




Objective 5




Implement each of the methods in the above table.







Example full output of the program




Note: orange text is what you print, blue text is the text the user typed in.







Welcome to Working with Numbers! Please enter two numbers:

Number 1:

12.1

Number 2:

23.4

Please select a menu option below:

1. Calculate 12.1 + 23.4

2. Calculate 12.1 - 23.4

3. Calculate 12.1 * 23.4

4. Calculate 12.1 / 23.4

5. Calculate 12.1 ^ 23.4

1

The result is: 35.5

Would you like to keep Working with Numbers? (y/n)

y

Welcome to Working with Numbers! Please enter two numbers:

Number 1:

23.4

Number 2:

34.5

Please select a menu option below:

1. Calculate 23.4 + 34.5

2. Calculate 23.4 - 34.5

3. Calculate 23.4 * 34.5

4. Calculate 23.4 / 34.5

5. Calculate 23.4 ^ 34.5

2

The result is: -11.1

Would you like to keep Working with Numbers? (y/n)

y

Welcome to Working with Numbers! Please enter two numbers:

Number 1:

34.5

Number 2:

45.6

Please select a menu option below:

1. Calculate 34.5 + 45.6

2. Calculate 34.5 - 45.6

3. Calculate 34.5 * 45.6

4. Calculate 34.5 / 45.6

5. Calculate 34.5 ^ 45.6

3

The result is: 1573.2

Would you like to keep Working with Numbers? (y/n)

y

Welcome to Working with Numbers! Please enter two numbers:

Number 1:

100

Number 2:

10

Please select a menu option below:

1. Calculate 100.0 + 10.0

2. Calculate 100.0 - 10.0

3. Calculate 100.0 * 10.0

4. Calculate 100.0 / 10.0

5. Calculate 100.0 ^ 10.0

4

The result is: 10.0

Would you like to keep Working with Numbers? (y/n)

y

Welcome to Working with Numbers! Please enter two numbers:

Number 1:

3

Number 2:

3

Please select a menu option below:

1. Calculate 3.0 + 3.0

2. Calculate 3.0 - 3.0

3. Calculate 3.0 * 3.0

4. Calculate 3.0 / 3.0

5. Calculate 3.0 ^ 3.0

5

The result is: 27.0

Would you like to keep Working with Numbers? (y/n)

n










Note: the questions/prompts should be phrased exactly as they appear above. You don't need to worry about color formatting your terminal, it's color coded above to distinguish between what you print to the screen and what the user entered.







What do you have to turn in?




A Java file named MyJavaLab05.java inside a zip folder named LastNameFirstName-lab05.zip







Criteria for grading:




● [10 pts] Your lab is submitted according to specifications (proper name of file, proper format)

● [10 pts] Well commented code explaining what you are doing (clear and grammatically correct)

● [5 pts] Objective 1

● [10 pts] Objective 2

● [25 pts] Objective 3

● [25 pts] Objective 4

● [15 pts] Objective 5

More products