Question
// Group Names: // Date: // Program Description: // Import required packages //--> // Declare class (SwitchDoLab) //--> { // Declare the main method //-->
// Group Names: // Date: // Program Description:
// Import required packages //-->
// Declare class (SwitchDoLab) //-->
{
// Declare the main method //-->
{
// Declare Constant integers SUM = 1, FACTORIAL = 2, QUIT = 3. //--> //--> //-->
// Create an integer variable named choice to store user's option. //-->
// Create a Scanner object
// 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 proram does the following:" //-->
// "1. Sum of numbers from 1 to n" //-->
// "2. Factorial of n" //-->
// "3. Quit" //-->
// "Please choose an option " //-->
// Read the value the user enters and store it in an integer variable //--> choice = scan.nextInt();
// Create a switch statement with the variable that stores the option as input for the 3 cases
{ //add up all the numbers from 1 to the number you entered // case SUM:
// Ask the user to enter a number,
// Take user input and put it into the variable //--> int value = scan.nextInt();
// Define 2 integer variables, one is to store the summation, the other is to store current number; and initialize them to 0 //--> //-->
// Use a while loop to calculate the sum // Add the while-statement with the condition that variable is less than the number you entered
{ // increment the current number;
// Calculate the summation by adding summation = summation + current; } // Print the answer saying: 'Sum of numbers from 1 - ' ' is ' ' //--> System.out.println("Sum of numbers from 1 - " + value + " is " + summation);
// exit from the switch with a break statement, what happens if you don't use one? break; //Calculate the product of all numbers from 1 to the number you entered // case FACTORIAL: //--> case FACTORIAL: // Ask the user to enter a number, (if the number is too large, the factorial result will become negative, why?) //-->
// Take user input and put it into the variable //-->
// Declare an long variable to store current product 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 the number you entered, // condition that greater than 1, decrement by 1 for(int i= value2; > 1 ; ) {
value3 = value3 * i; } // Print the answer saying: 'Factorial of ' ' is ' ' //--> System.out.println("Factorial of " + value2 + " is " + value3);
// exit from the switch with a break statement. //--> break;
// case QUIT: //-->
// Print 'Your choice was Quit, Quitting the program, ' //-->
// exit from the switch with a break statement //-->
// default: default: // Print 'Incorrect choice '
// break statement here is optional, why? //--> break; // Close the switch statement //--> } // Close the do-while loop with a condition that the user's choice is not equal to QUIT }while() // Close the main method //--> } //Close the Class //-->
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started