[SOLVED] CSE110 -Principles of Programming with Java  - Lab 4

30.00 $

Category:

Description

Rate this product

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​

 

 

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. finalint​ ​ 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

//–>

 

//     Use a while loop to calculate the sum of numbers

from 1 to n

//     Add the while-statement with the condition that

<count> variable is less than <num>

while​(count​ ​?​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 – ‘ <num> ‘ is ‘ <sum>

//–>

 

//     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 <num>

//–>

//     Compute the factorial of <num>

// Declare an long (integer) variable <fact> and

initialize it to 1

//–>

//     Use a for loop to calculate the factorial of n

//     Write a for loop with an integer variable <i> and

initialize it to <num>,

//  condition that <i> greater than 1, increment <i> by 1

for​(​int​ ​i​= ​num​; ​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

<num>

// Hint: The loop should terminate after iterating upto

<num>

// 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)

// Print the numbers with two spaces

 

case​ QUIT:

//–>

//     Print ‘Your choice was <QUIT>, Quitting the program, Have a good day!’

//–>

//     exit from the switch with a break statement//–>

//     default: default​:

//      Print ‘Incorrect choice, ‘ <choice> ‘ Please choose

again’

//–>

//     Close the switch statement

//–>

//Close the do-while loop with a condition <choice> is not equal to <QUIT>

}

}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 <QUIT>, Quitting the program, Have a good day!