# CSE 110 – Lab 4 solution

\$14.99

Original Work
Category:

## Description

5/5 - (1 vote)

This lab is for practicing the switch statement, do-while, while and for loops. Use the following Coding Guidelines: • ​When declaring a variable, you usually want to initialize it. • ​Use white space to make your program more readable. • ​Use comments after the ending brace of classes, methods, and blocks to identify to which block it belongs.
Assignments Documentation: At the beginning of each programming assignment you must have a comment block with the following information: /*————————————————————————— // AUTHOR: (Put your name here) // FILENAME: Lab4.java // SPECIFICATION: This program is for practicing the switch statement, do-while, while and for loops. // Using a while loop calculate the sum of integers from 1 to n. // Using a for loop find the factorial of n. // LAB LETTER: (Put your Lab Letter here) //————————————————————————-*/ Getting Started Create a class called ​Lab4​. Use the same setup for setting up your class and main method as you did for the previous assignments. Be sure to name your file ​Lab4.java​. Hints Please replace //–> with the correct program to finish the task according to the corresponding comment.
Please replace ??? with the correct program to enable the program to run as required.
// Import required java utility Scanner package import​ java.util.Scanner; // Declare class (Lab4) public​ ​class​ Lab4 { // Write the main method public​ ​static​ ​void​ main(String[] ​args​) { // Declare Constant integers SUM = 1, FACTORIAL = 2, FACTORS = 3, QUIT = 4. final​ ​int​ ​SUM​= 1; final​ ​int​ ​FACTORIAL​ = 2; //–>
// Create an integer variable named choice. //–>
// Create a Scanner object (you may need to import the class)

//–>
// Create a do-while loop that exits only when the user chooses quit (choice = QUIT) // Have the do-statement here ???​{ // Print the following options: // “This ​program​ does the following:” //–> // “1. Sum of numbers from 1 to n” //–> // “2. Factorial of n” // // “3. Factors of n” //–> // “4. Quit” //–> // “Please choose an option ” //–>
// Read the value the user enters and store it in an integer variable //–>
// Create a switch statement with as input for the 3 cases switch​(​???​) { // case SUM: case​ ​SUM​: // Ask the user to enter a number, System.​out ​ .print(​”\nPlease enter a number n: “​); // Take user input and put it into the variable //–> // Define 2 integer variables and and initialize them to 0 //–>
// Use a while loop to calculate the sum of numbers
from 1 to n
// Add the while-statement with the condition that variable is less than while​(​count​?​num​){ // increment variable; count​++; // Calculate the by adding to sum​ = ​sum​ + ​count​; } // Print the answer saying, ‘Sum of numbers from 1 – ‘ ‘ is ‘ //–>
// exit from the switch with a break statement, what happens if you don’t use one? break​;

// case FACTORIAL: //–> case​ ​???​: // Ask the user to enter a number, System.​out ​ .print(​”\nPlease enter a number n: “​); // Take user input and put it into the variable //–> // Compute the factorial of // Declare an long (integer) variable and
initialize it to 1
//–> // Use a for loop to calculate the factorial of n // Write a for loop with an integer variable and
initialize it to ,
// condition that greater than 1, increment by 1 for​(​int​ ​i​= ​num​; ​i​ > 1 ; ​i​–){ // write the expression = * //–>
} //Print the answer saying,’Factorial of’ ‘is’ //–> // exit from the switch with a break statement. //—> case​ FACTORS: // Ask the user to enter a number, System.​out ​ .print(​”\nPlease enter a number n: “​); // Take user input and put it into the variable //–> // Write a loop to find which numbers are the factors of

// Hint: The loop should terminate after iterating upto

// Print the a statement saying,’Factors of’ ‘are’ // if(num%i) == 0, then I is a factor (Here we check if the number is divisible by i) // Print the numbers with two spaces case​ QUIT: //–> // Print ‘Your choice was , Quitting the program, Have a good day!’ //–> // exit from the switch with a break statement //–> // default: default​: // Print ‘Incorrect choice, ‘ ‘ Please choose again’ //–> // Close the switch statement //–>

//Close the do-while loop with a condition is not equal to

} }​while​(choice!=QUIT); }
}
SAMPLE INPUT: 1 10 2 20 3 65 8 4
SAMPLE OUTPUT: This program does the following: 1. Sum of numbers from 1 to n 2. Factorial of n 3. Factors of n 4. Quit Please choose an option
Please enter an integer n: Sum of numbers from 1 – 10 is 55
This program does the following: 1. Sum of numbers from 1 to n 2. Factorial of n 3. Factors of n 4. Quit Please choose an option
Please enter an integer n: Factorial of number 20 is 21c3677c82b40000
This program does the following: 1. Sum of numbers from 1 to n 2. Factorial of n 3. Factors of n 4. Quit Please choose an option

Please enter an integer n: Factors of number 65 are 1 5 13 65
This program does the following: 1. Sum of numbers from 1 to n 2. Factorial of n 3. Factors of n 4. Quit Please choose an option
Incorrect choice, 8 Please choose again
This program does the following: 1. Sum of numbers from 1 to n 2. Factorial of n 3. Factors of n 4. Quit Please choose an option
Your choice was , Quitting the program, Have a good day!