Question: NEEDED IN JAVA 3. Write output statements that display the following menu of options: Choose one of the following options: 1. Perform an arithmetic operation
NEEDED IN JAVA
3. Write output statements that display the following menu of options:
Choose one of the following options:
1. Perform an arithmetic operation
2. Apply a function
3. Calculate a factorial
4. Exit the program
4. Write an input statement that reads an integer from the user representing the option the user chose and store it in a variable.
5. Write a switch statement that uses this variable as a controlling expression and has cases for options 1-4 along with a default case.
For the cases for 1-3, display a string indicating the users choice and use a break statement to exit the switch statement.
For case 4, display a thank you message with your name and use System.exit to exit the program.
For the default case, display a message indicating the users choice was invalid.
6. Run your program to see if it works. Here are some examples of that the output should look like:
Sample output 1:
Choose one of the following options:
1. Perform an arithmetic operation
2. Apply a function
3. Calculate a factorial
4. Exit the program
1
Perform an arithmetic operation
Sample output 2:
Choose one of the following options:
1. Perform an arithmetic operation
2. Apply a function
3. Calculate a factorial
4. Exit the program
4
Thank you for using John Doe's calculator!
Sample output 3:
Choose one of the following options:
1. Perform an arithmetic operation
2. Apply a function
3. Calculate a factorial
4. Exit the program
5
Invalid Choice
7. Put the code to display the menu, read the users choice, and the switch statement inside the body of a do-while loop. The loop should continue as long as the user didnt choose 4.
8. Run your program to see if it works. Here is an example of what the output should look like:
Choose one of the following options:
1. Perform an arithmetic operation
2. Apply a function
3. Calculate a factorial
4. Exit the program
0
Invalid Choice
Choose one of the following options:
1. Perform an arithmetic operation
2. Apply a function
3. Calculate a factorial
4. Exit the program
2
Apply a function
Choose one of the following options:
1. Perform an arithmetic operation
2. Apply a function
3. Calculate a factorial
4. Exit the program
4
Thank you for using John Doe's calculator!
9. Replace the output statement in case 1 with code that does the following:
(a) Prompt the user to enter an expression of the form NUM OP NUM.
(b) Read an int representing the first number, a String representing the operation, and an int representing the second number.
(c) Use the charAt method of the String class to extract the character as position 0 in the string and store it in a variable of type char.
(d) Use a switch statement with the char variable as the controlling expression to determine the operator the user entered.
(e) If the operator is one of +, -, *, /, %, perform the operation on the numbers the user entered and display the result.
(f) For the default case, display an error indicating the user entered an unknown operator.
10. Run your program to see if it works. Here is an example of what the output should look like:
Choose one of the following options:
1. Perform an arithmetic operation
2. Apply a function
3. Calculate a factorial
4. Exit the program
1
Enter an expression of the form NUM OP NUM:
7 ^ 2
Unknown operator: ^
Choose one of the following options:
1. Perform an arithmetic operation
2. Apply a function
3. Calculate a factorial
4. Exit the program
1
Enter an expression of the form NUM OP NUM:
8 / 2
Result: 4
Choose one of the following options:
1. Perform an arithmetic operation
2. Apply a function
3. Calculate a factorial
4. Exit the program
4
Thank you for using John Doe's calculator
11. Replace the output statement in case 2 with code that does the following:
(a) Prompt the user to enter an expression of the form FUNC ARG.
(b) Read a String representing the function name, and a double representing the argument.
(c) Use a switch statement with the String variable as the controlling expression to determine the function the user entered.
(d) If the function is one of log, ln, or sqrt apply the function to the argument using the Math.log10, Math.log, or Math.sqrt methods respectively, and display the result.
(e) For the default case, display an error indicating the user entered an unknown function.
12. Run your program to see if it works. Here is an example of what the output should look like:
Choose one of the following options:
1. Perform an arithmetic operation
2. Apply a function
3. Calculate a factorial
4. Exit the program
2
Enter an expression of the form FUNC ARG:
log .001
Result: -3.0
Choose one of the following options:
1. Perform an arithmetic operation
2. Apply a function
3. Calculate a factorial
4. Exit the program
2
Enter an expression of the form FUNC ARG:
sin 0
Unrecognized function: sin
Choose one of the following options:
1. Perform an arithmetic operation
2. Apply a function
3. Calculate a factorial
4. Exit the program
4
Thank you for using John Doe's calculator
13. Replace the output statement in case 3 with code that does the following:
(a) Prompt the user to enter a number.
(b) Read an int.
(c) Create a variable to store the result and initialize it to 1.
(d) Use a while loop to multiply the result by every number between 1 and the number the user entered.
(e) Display the result to the user.
14. Run your program to see if it works. Here is an example of what the output should look like:
Choose one of the following options:
1. Perform an arithmetic operation
2. Apply a function
3. Calculate a factorial
4. Exit the program
3
Enter a number:
4
Result: 24
Choose one of the following options:
1. Perform an arithmetic operation
2. Apply a function
3. Calculate a factorial
4. Exit the program
3
Enter a number:
-4
Result: 1
Choose one of the following options:
1. Perform an arithmetic operation
2. Apply a function
3. Calculate a factorial
4. Exit the program
4
Thank you for using John Doe's calculator
15. Run your program again to make sure it works when each of the options is chosen.
16. Make sure your code is indented properly and comments are added to each line
17. Upload the file YournameLab5.java to the drop box folder labeled Lab Assignment 5.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
