Question: 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 users input. To do this, you must show the user the menu, ask the user for their choice, perform the appropriate computation, and then repeat until the user exits. The menu is as follows: Please choose your choice from the following menu: 1) Calculate the sum of integers 1 to m. 2)Calculate the factorial of given number. 3) Display the 1st integer from left 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. Assignments 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: your own 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 and repeat it until the user selects the exit option. For this, we need to declare a variable called choice to store the user choice. int choice; //initialize do-while loop 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.println 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. do{ //Please choose your choice from following menu //1) Calculate the sum of integers 1 to m. //2) Calculate the factorial of given number. //3) Find the first digit of a given number. //4) Quit. // Use scanner to user input } 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 switchcase statement follows. switch(choice):{ case 1: break; case 2: break; case 3: break; default: break; } Step 4: CASE 1 arithmetic operation /* CASE 1 statement */ If the user chooses option one, they wish to compute the sum of integers from one to a target number, m. Thus, we must request this target number from the user and sum up the integers from 1 to that number. First, we declare two new integers, one for the target number and one for the sum. int m,sum=0; It is important to initialize the value of sum as 0 or else the sum variable will start with an incorrect value. Next, ask the user to enter a number using System.out.println and then use a Scanner object to get the value. To perform the operation, we need an intermediate variable that goes from 1 to m in a loop and guides the sum. For that, initialize a variable. (These variable are called index variables and typically start at i and proceed in alphabetical order, i.e., i,j,k). i = 1; Begin a while loop using i and until its value is less than m and inside the loop update the value of the sum in the following manner: while (i<=m) { sum=sum+i; i++; } /*at the end of each iteration sum will have the value of sum till ith integer*/ Print the value of sum variable for the user using System.out.println. Step 5: CASE 2 arithmetic operation /* CASE 2 statement */ The user chooses option 2 in order to compute the factorial of given integer. For calculating the value of the factorial, we need to have two variables: n, the integer entered by the user, and fact, the value of the factorial. int n,fact=1; /* ensure that initial value of fact is 1.*/ Ask the user to enter a number using System.out.println and a Scanner object. To perform the operation, we need an intermediate variable that goes from 1 to m in a loop and guides the factorial. For that, initialize an index variable. i = 1; Begin a while loop using i and until i is less than n and inside the loop update the value of the factorial in the following manner: while(i<=n) { fact=fact*i; i++; }/* at the end of each iteration fact will have the product of 1st i integers*/ Print the value of fact variable using System.out.println. Step 6: CASE 3 arithmetic operation /* CASE 3 statement */ The user chooses option 3 in order to get the first digit of a given number. To calculate this, we need to have two variables: num, the integer entered by the user, and rem, which stores the value of reminder when num is divided by 10. Ask the user to enter a number using System.out.println and store the value entered by the user in the num variable using a Scanner object. The logic is that we will divide the number until the quotient becomes 0 and store the reminder at every iteration. At the end of each iteration, the reminder will have the 1st integer from the right from the previous iteration. We need to use while loop to perform the operation, since we dont know how big the number is ahead of times and the terminating condition would be num = 0. This logic will be explained during lab hours. while(num!=0) { rem=num%10; num=num/10; }/* at every iteration rem will have the 1st digit from left for an integer */ /* num will have 1 digit less than its previous iteration */ Print the value of rem variable at the end of the loop, hence outside the loop. That value should be the 1st integer from right. Use System.out.println Step 7: Display the output You should display the answer of the CASE conditions as mentioned and repeat the menu operations. Have a look at the sample output below. Sample Output Below is an example of what your output should roughly look like when this lab is completed. Text in Orange represents user input. Please choose from following menu 1. Calculate the sum of 1 to m integers 2. Calculate the factorial of given number 3. Generate the 1st Integer from left for given number 4. Quit 1 Enter the number: 4 The sum of first 4 numbers is 10 Please choose from following menu 1. Calculate the sum of 1 to m integers 2.Calculate the factorial of given number 3.Generate the 1st Integer from left for given number 4.Quit 2 Enter the number: 5 The factorial of 5 is 120 Please choose from following menu 1. Calculate the sum of 1 to m integers 2. Calculate the factorial of given number 3. Generate the 1st Integer from left for given number 4. Quit 3 Enter the number: 987654321 The 1st integer of given number from left is 9 Please choose from following menu 1. Calculate the sum of 1 to m integers 2. Calculate the factorial of given number 3. Generate the 1st Integer from left for given number 4. Quit

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!