Question

// Group Names: // Date: // Program Description: //   Import required packages //--> //   Declare class...

// 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 '<display the incorrect number you entered> ' Please choose again'
                   //-->
                   System.out.println("Incorrect choice " + choice + " Please choose again\n");

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

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Program:

// Group Names:

// Date:

// Program Description:

// Import required packages

import java.util.Scanner;

// Declare class (SwitchDoLab)

public class SwitchDoLab

{

// Declare the main method

public static void main(String[] args)

{

// Declare Constant integers SUM = 1, FACTORIAL = 2, QUIT = 3.

//-->

final int SUM = 1;

final int FACTORIAL = 2;

final int QUIT = 3;

// Create an integer variable named choice to store user's option.

//-->

int choice;

// Create a Scanner object

//-->

Scanner scan = new Scanner(System.in);

// Create a do-while loop that exits only when the user chooses quit (choice = QUIT)

//-->

do

{

// Print the following options:

// "This program does the following:"

//-->

System.out.println("This program does the following:");

// "1. Sum of numbers from 1 to n"

//-->

System.out.println("1. Sum of numbers from 1 to n");

// "2. Factorial of n"

//-->

System.out.println("2. Factorial of n");

// "3. Quit"

//-->

System.out.println("3. Quit");

// "Please choose an option "

//-->

System.out.print("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

switch(choice)

{

//add up all the numbers from 1 to the number you entered

case SUM:

// Ask the user to enter a number,

System.out.print(" 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

//-->

//-->

int summation = 0;

int current = 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

while(current < value)

{

// increment the current number;

current++;

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

//-->

System.out.print(" Enter a number: ");

// Take user input and put it into the variable

//-->

int value2;

value2 = scan.nextInt();

// Declare an long variable to store current product and initialize it to 1

//-->

long value3 = 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; i > 1 ; i--)

{

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:

//-->

case QUIT:

// Print 'Your choice was Quit, Quitting the program, '

//-->

System.out.println(" Your choice was Quit, Quitting the program");

// exit from the switch with a break statement

//-->

break;

// default:

default:

// Print 'Incorrect choice '<display the incorrect number you entered> ' Please choose again'

//-->

System.out.println(" Incorrect choice " + choice + " Please choose again ");

// 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(choice != QUIT);

// Close the main method

//-->

}

//Close the Class

//-->

}

Output:

Program Screenshot:

Add a comment
Know the answer?
Add Answer to:
// Group Names: // Date: // Program Description: //   Import required packages //--> //   Declare class...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • import java.util.*; /** Description: This program determines the average of a list of (nonnegative) exam scores....

    import java.util.*; /** Description: This program determines the average of a list of (nonnegative) exam scores. Repeats for more exams until the user says to stop. Input: Numbers, sum, answer Output: Displays program description Displays instruction for user Displays average Algorithm: 1. START 2. Declare variables sum, numberOf Students, nextNumber, answer 3. Display welcome message and program description 4. DOWHILE user wants to continue 5. Display instructions on how to use the program 6. Inititalize sum and number of student...

  • Answer using programming in C only. 6 pts Use a while loop to complete the following:...

    Answer using programming in C only. 6 pts Use a while loop to complete the following: Prompt the user to enter integers one by one, until the user enters a negative number to stop Calculate the sum and the average of all the numbers entered by the user Print the sum and the average onto the screen after the loop. Declare and initialize all variables. Be sure to keep a count of the numbers entered for the average calculation

  • Please use python to write the simple program. Exercise 2 - Summation Write a program to...

    Please use python to write the simple program. Exercise 2 - Summation Write a program to accept integer inputs until the user inputs a non-integer. Then, the program prints the summation of the inputted numbers. Hint: Use the function input() to get the user input. Use a variable total for calculating the summation. Need to use while loop for the repeated inputs. Use if statement with isdigit() function to check whether the user input is an integer or not. if...

  • Evaluate Exponent: Declare a double variable called result and initialize it to 1.0; Ask the user...

    Evaluate Exponent: Declare a double variable called result and initialize it to 1.0; Ask the user for two values - one for the base, and the other for the exponent. If the exponent parameter is 0, give the user an error. You do not need to handle a negative exponent value. Otherwise, declare an integer variable called loop and initialize it to 0 Write a do while loop as follows: Inside the loop: Multiply the results times the number Increment...

  • This is for a java program public class Calculation {    public static void main(String[] args)...

    This is for a java program public class Calculation {    public static void main(String[] args) { int num; // the number to calculate the sum of squares double x; // the variable of height double v; // the variable of velocity double t; // the variable of time System.out.println("**************************"); System.out.println(" Task 1: Sum of Squares"); System.out.println("**************************"); //Step 1: Create a Scanner object    //Task 1. Write your code here //Print the prompt and ask the user to enter an...

  • This is the contents of Lab11.java import java.util.Scanner; import java.io.*; public class Lab11 { public static...

    This is the contents of Lab11.java import java.util.Scanner; import java.io.*; public class Lab11 { public static void main(String args[]) throws IOException { Scanner inFile = new Scanner(new File(args[0])); Scanner keyboard = new Scanner(System.in);    TwoDArray array = new TwoDArray(inFile); inFile.close(); int numRows = array.getNumRows(); int numCols = array.getNumCols(); int choice;    do { System.out.println(); System.out.println("\t1. Find the number of rows in the 2D array"); System.out.println("\t2. Find the number of columns in the 2D array"); System.out.println("\t3. Find the sum of elements...

  • Declare and initialize 4 Constants for the course category weights: The weight of Homework will be...

    Declare and initialize 4 Constants for the course category weights: The weight of Homework will be 15% The weight of Tests will be 35% The weight of the Mid term will be 20% The weight of the Fin al will be 30% Remember to name your Constants according to Java standards. Declare a variable to store the input for the number of homework scores and use a Scanner method to read the value from the Console. Declare two variables: one...

  • Make one program using Dev c++, and explain how program works Final Program Requirements: Declare an...

    Make one program using Dev c++, and explain how program works Final Program Requirements: Declare an int variable and int array with four elements in the main program. Initial values for the intvariable and int array are hardcoded in the program. Declare an int pointer in the main program.Program shows the values of the integer variable and integer arrayelements. Program shows the addresses of the variable and each integer array element. Store addresses inpointers temporarily prior to displaying. The addresses...

  • In this lab, you complete a partially written Java program that includes two methods that require...

    In this lab, you complete a partially written Java program that includes two methods that require a single parameter. The program continuously prompts the user for an integer until the user enters 0. The program then passes the value to a method that computes the sum of all the whole numbers from 1 up to and including the entered number. Next, the program passes the value to another method that computes the product of all the whole numbers up to...

  • In this lab, you complete a partially written Java program that includes two methods that require...

    In this lab, you complete a partially written Java program that includes two methods that require a single parameter. The program continuously prompts the user for an integer until the user enters 0. The program then passes the value to a method that computes the sum of all the whole numbers from 1 up to and including the entered number. Next, the program passes the value to another method that computes the product of all the whole numbers up to...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT