Description
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: // FILENAME: // SPECIFICATION: while and for loops. // from 1 to n. // // LAB LETTER: //-------------------------------------------------------------------------*/
(Put your name here) Lab4.java This program is for practicing the switch statement, do-while,
Using a while loop calculate the sum of integers
Using a for loop find the factorial of n. (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 <choice>
//–>
- // Create a switch statement with <choice> 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 <num> //–>
// Define 2 integer variables <sum> and <count> and
initialize them to 0
from 1 to n
//–>
- // Use a while loop to calculate the sum of numbers
- // Add the while-statement with the condition that
<count> variable is less than <num>
<num> ' is ' <sum>
while(c ount?num){
// increment <count> variable;
count++;
// Calculate the <sum> by adding <count> to <sum> sum = sum + count;
}
- // Print the answer saying, ‘Sum of numbers from 1 – ‘ //–>
- // exit from the switch with a break statement, what
happens if you don't use one?
break;
<num> <num>
// case FACTORIAL: //–>
case ???:
initialize it to 1
initialize it to <num>,
// Ask the user to enter a number,
System.out.print(”\nPlease enter a number n: “) ;
// Take user input and put it into the variable <num> //–>
// Compute the factorial of <num>
// Declare an long (integer) variable <fact> and
//–>
- // Use a for loop to calculate the factorial of n
- // Write a for loop with an integer variable <i> and
// condition that <i> greater than 1, increment <i> by 1
for(int i= n um; i > 1 ; i–){
// write the expression <fact> = <fact> * <i> //–>}
//Print the answer saying,'Factorial of' <num> 'is' <fact> //-->
- // 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 <num> //–>
// 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' <num> 'are'
// if(num%i) == 0, then I is a factor (Here we check if the number <num> is divisible by i)
Have a good day!'
again’
// Print the numbers with two spaces
case QUIT: //–>
- // Print ‘Your choice was <QUIT>, Quitting the program,
//–>
- // exit from the switch with a break statement //–>
// default:
default:
// Print ‘Incorrect choice, ‘ <choice> ‘ Please choose
//–>
// Close the switch statement
//–>
//Close the do-while loop with a condition <choice> is not equal to
<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
} }while(choice!=QUIT);
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 <QUIT>, Quitting the program, Have a good day!