CSE 110 - Lab 4
What this Lab Is About:
• Getting familiar with LOOPS and SWITCH CASE statements
• Performing arithmetic operations inside the loop
Problem Description: Displaying menu options and performing arithmetic operations based
on user choice
For this Lab you have to perform two different arithmetic operations based on user choice.
You have to prompt menu option until user selects “Quit”
You have to ask user to enter choice option from menu
menu should be in the following format
Please choose your choice from following menu
1) Calculate the sum of 1 to m integers
2) Calculate the factorial of given number
3) Quit
Step 1: Getting Started Create a class called Lab3. Be sure to name your file Lab3.java.
Lab 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
Examining the problem, we need to ensure that the menu option is seen until the user choose to quit
For this we need to declare a variable choice to store the user choice using Scanner class object
int choice;
initialize do-while loop
Inside the loop print the menu using System.out.println Statements
do{
Please choose your choice 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 a given number
4) Quit
} while(/* termination condition*/)
Remember every loop should have a termination condition.Here the loop should run until user
choose option 4. So the termination condition would be (choice!=4)
Step 3: Performing arithmetic operations
Based on the choice num entered by the user.We need to do arithmetic operations.
Inside the do-while loop initialize switch-case statement to perform the operations.
Switch -case is performed on the choice /*choice entered by the user*/
The outline will be in following way.
switch(choice):{
case 1:
break;
case 2:
break;
default:
break;
}
For CASE 1 CASE 2 description follow the below mentioned steps
Step4: CASE 1 arithmetic operation /* Code to be done inside CASE 1 statement */
The user chooses option 1 in order to perform sum of 1st m integers.
For calculating sum ,we need to have two variables – m/* integer entered by the user*/
sum /* stores the value of 1st m integers */
int m,sum=0; /* ensure that initial value of sum is 0*/
Ask the user to enter a number using System.out.println
Store the value entered by the user in m variable by using scanner object
To perform the operation we need an intermediate variable that take the value from 1 to m in a loop
and calculates the sum
For that initialize a variable i
int i;
begin a for loop for i taking values from 1 to m and inside the loop update the value of sum
in the following manner
for (i=1,i<=m;i++)
{
sum=sum+i;
}/* at the end of each iteration sum will have the value of sum till ith integer*/
Print the value of sum so that user can see it using System.out.println
Step5: CASE 2 arithmetic operation /* Code to be done inside CASE 2 statement */
The user chooses option 2 in order to perform the factorial of given integer
For calculating factorial ,we need to have two variables – n/* integer entered by the user*/
fact /* stores the value of 1st n integers */
int n,fact=1; /* ensure that initial value of fact is 1.*/
Ask the user to enter a number using System.out.println
Store the value entered by the user in n variable using scanner object
To perform the operation we need an intermediate variable that take the value from 1 to n in a loop
and calculates the factorial
For that initialize a variable i
int i;
begin a for loop for i taking values from 1 to n and inside the loop update the value of fact
in the following manner
for (i=1,i<=n;i++)
{
fact=fact*i;
}/* at the end of each iteration fact will have the product of 1st i numbers*/
Print the value of fact using System.out.Println statement
Step6: Display the output
You should display the answer of the CASE conditions as mentioned.
Sample Output
Below is an example of what your output should roughly look like when this lab is completed.
All text in bold 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.Quit
1
Enter the number
12
The sum of first 12 numbers is 78
Please choose from following menu
1. Calculate the sum of 1 to m integers
2.Calculate the factorial of given number
3.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.Quit
4
Last Step: Submit your lab by following the instructions below:
*******************************************************************************
Submit your Lab4.java file to the Submission Server. Go to the Submission Server site,
https://courses.eas.asu.edu/cse110/login , then click on Lab Submissions in the left frame. The
dropdown box will start in Lab1, so you don’t have to change it (you will for future labs). Click on
the browse button and find where you saved your Lab1.java file (and not the Lab1.class file) on
your computer. Upload the file to the site and then click on the Submit button.
Your file will be submitted and a screen will show up displaying if your program compiled and
what your output is when run on some sample input (in this case nothing).
You should then check to make sure that the actual file submitted properly and is readable to the
grader. To do so click on Grades in the frame on the left of the page and then click on the 0
underneath Lab3. You will again see that your program compiled and the sample output, but you
should scroll down to the bottom of the screen and make sure your file is readable as well.
Challenging Questions (In case if you were able to complete the lab early)
Note: the below questions are not evaluated as lab
1) Print the following format using loops
*
**
***
****
*****
2) Calculate the 1st integer from left for the given integer
example if the given number is 12345
Output should be 1
Powered by TCPDF (www.tcpdf.org)