Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Problem Description: Displaying menu options and performing arithmetic operations based on user choice For this Lab your program must perform three different arithmetic operations based
Problem Description: Displaying menu options and performing arithmetic operations based on user choice For this Lab your program must perform three different arithmetic operations based on a user's input. To do this, you must show the user a menu, ask the user for their choice, perform the appropriate computation, and then repeat this process until the user chooses to exit. The menu is as follows Please choose from following menu 1) Calculate the sum of the integers from m to n. 2) Calculate the factorial of given number. 3) Display the first digit for a given number. 4) Quit. Step 1: Getting Started Create a class called Lab4. Use the same setup for setting up your class and main method as you did in previous labs and assignments. Be sure to name your file Lab4.java Assignment Documentation: At the beginning of each programming assignment you must have a comment block with the following information // AUTHOR: your name /FILENAME: title of the source file /SPECIFICATION: description of the program // FOR: CSE 110- Lab #4 // TIME SPENT: how long it took you to complete the assignment Step 2: Beginning the loop and declaring variables The first part of the problem is to show the user the menu, collect the user's menu choice as an input, and repeat it until the user selects the exit option. For this, we need to declare a variable named choice to store the user choice. int choice -0 Next, we must select the appropriate loop for the main body of the program. In this case, since we know that the program should execute at least once, a do-while loop is most appropriate. Inside the loop, print the menu using System.out.printin statements and then request the menu option from the user. A skeleton do-while loop follows. Remember to add the Scanner statement that requests the choice from the user. dot //Please choose from following menu: //1) Calculate the sum of integers m to n. 12) Calculate the factorial of given number //3) Find the first digit of a given number. //4) Quit. // Use scanner to collect user input 3 while(/*termination condition/ Remember every loop should have a termination condition. Here the loop should run until user chooses option 4. So the termination condition would be (choice!4) Step 3: Performing arithmetic operations Based on the choice entered by the user, we need to do some arithmetic operations. Inside the do-while loop, but after the menu, initialize a switch-case statement to perform the operations. The switch-case statement hinges on the choice variable, which should now have an integer value. An example switch-case statement follows switch(choice) l case 1: break; case 2 break; case 3 sto do> break default:
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started