Question

In: Computer Science

Summary In this lab, you use a counter-controlled while loop in a Java program provided for...

Summary

In this lab, you use a counter-controlled while loop in a Java program provided for you. When completed, the program should print the numbers 0 through 10, along with their values multiplied by 2 and by 10. The data file contains the necessary variable declarations and some output statements.

Instructions

  1. Ensure the file named Multiply.java is open.

  2. Write a counter-controlled while loop that uses the loop control variable to take on the values 0 through 10. Remember to initialize the loop control variable before the program enters the loop.

  3. In the body of the loop, multiply the value of the loop control variable by 2 and by 10. Remember to change the value of the loop control variable in the body of the loop.

  4. Execute the program by clicking Run. Record the output of this program.

  5. // Multiply.java - This program prints the numbers 0 through 10 along
    // with these values multiplied by 2 and by 10.
    // Input: None.
    // Output: Prints the numbers 0 through 10 along with their values multiplied by 2 and by 10.


    public class Multiply
    {
       public static void main(String args[])
       {
           String head1 = "Number: ";
           String head2 = "Multiplied by 2: ";
           String head3 = "Multiplied by 10: ";              
           int numberCounter;    // Numbers 0 through 10.
           int byTen;       // Stores the number multiplied by 10.
           int byTwo;    // Stores the number multiplied by 2.
           final int NUM_LOOPS = 10; // Constant used to control loop.

           // This is the work done in the housekeeping() method
           System.out.println("0 through 10 multiplied by 2 and by 10" + "\n");

           // This is the work done in the detailLoop() method
           // Initialize loop control variable.
           // Write your counter controlled while loop here
               // Multiply by 2
               // Multiply by 10
               System.out.println(head1 + numberCounter);
               System.out.println(head2 + byTwo);
               System.out.println(head3 + byTen);
               // Next number.
          
           // This is the work done in the endOfJob() method
    System.exit(0);
       } // End of main() method.

    } // End of Multiply class.

Solutions

Expert Solution

public class Multiply
{
    public static void main(String args[])
    {

        String head1 = "Number: ";
        String head2 = "Multiplied by 2: ";
        String head3 = "Multiplied by 10: ";
        int numberCounter; // Numbers 0 through 10.
        int byTen; // Stores the number multiplied by 10.
        int byTwo; // Stores the number multiplied by 2.
        final int NUM_LOOPS = 10; // Constant used to control loop.

// This is the work done in the housekeeping() method
        System.out.println("0 through 10 multiplied by 2 and by 10" + "\n");

// This is the work done in the detailLoop() method
// Initialize loop control variable.
// Write your counter controlled while loop here
        numberCounter = 0;
        while (numberCounter <= 10)
        {
            // Multiply by 2
            byTwo = numberCounter * 2;
            // Multiply by 10
            byTen = numberCounter * 10;
            System.out.println(head1 + numberCounter);
            System.out.println(head2 + byTwo);
            System.out.println(head3 + byTen);
            numberCounter++;
        }
// This is the work done in the endOfJob() method
        System.exit(0);
    } // End of main() method.

} // End of NewMultiply class.


Related Solutions

Sentinel While Loop Lab Convert Lab 11 from a counter controlled WHILE loop to a sentinel...
Sentinel While Loop Lab Convert Lab 11 from a counter controlled WHILE loop to a sentinel WHILE loop. Do the following: Prompts the user to enter a grade or a -1 to quit. IF the user entered a -1 THEN Display a message that the User is done entering grades ELSE Count each grade as it is entered. Compute a running total of the grades entered. END IF After the user enters the sentinel of -1, calculate the average of...
Summary In this lab, you write a while loop that uses a sentinel value to control...
Summary In this lab, you write a while loop that uses a sentinel value to control a loop in a C++ program that has been provided. You also write the statements that make up the body of the loop. The source code file already contains the necessary variable declarations and output statements. Each theater patron enters a value from 0 to 4 indicating the number of stars the patron awards to the Guide’s featured movie of the week. The program...
Summary In this lab, you will make additions to a C++ program that is provided. The...
Summary In this lab, you will make additions to a C++ program that is provided. The program is a guessing game. A random number between 1 and 10 is generated in the program. The user enters a number between 1 and 10, trying to guess the correct number. If the user guesses correctly, the program congratulates the user, and then the loop that controls guessing numbers exits; otherwise, the program asks the user if he or she wants to guess...
*Java program* Use while loop 1.) Write a program that reads an integer, and then prints...
*Java program* Use while loop 1.) Write a program that reads an integer, and then prints the sum of the even and odd integers. 2.) Write program to calculate the sum of the following series where in is input by user. (1/1 + 1/2 + 1/3 +..... 1/n)
Java Program. Sentinel While Loop Lab Do the following: Prompts the user to enter a grade...
Java Program. Sentinel While Loop Lab Do the following: Prompts the user to enter a grade or a -1 to quit. IF the user entered a -1 THEN Display a message that the User is done entering grades ELSE Count each grade as it is entered. Compute a running total of the grades entered. END IF After the user enters the sentinel of -1, calculate the average of the grades entered. When computing the average, make sure that there is...
8.40 Lab 8e: BankBalance Introduction For this lab, you will use a do-while loop to complete...
8.40 Lab 8e: BankBalance Introduction For this lab, you will use a do-while loop to complete the task. A do-while loop has this basic structure: /* variable initializations */ do{ /* statements to be performed multiple times */ /* make sure the variable that the stop condition relies on is changed inside the loop. */ } while (/*stop condition*/); Despite the structure of the do-while loop being different than that of a for loop and a while loop, the concept...
Write a program in java that deliberately contains an endless or infinite while loop. The loop...
Write a program in java that deliberately contains an endless or infinite while loop. The loop should generate multiplication questions with single-digit random integers. Users can answer the questions and get immediate feedback. After each question, the user should be able to stop the questions and get an overall result. See Example Output. Example Output What is 7 * 6 ? 42 Correct. Nice work! Want more questions y or n ? y What is 8 * 5 ? 40...
Modify the previous program to use the Do-While Loop instead of the While Loop. This version...
Modify the previous program to use the Do-While Loop instead of the While Loop. This version of the program will ask the user if they wish to enter another name and accept a Y or N answer. Remove the "exit" requirement from before. Output: Enter the full name of a person that can serve as a reference: [user types: Bob Smith] Bob Smith is reference #1 Would you like to enter another name (Y or N)? [user types: y] Enter...
Understanding if-elseStatements Summary In this lab, you will complete a prewritten Java program that computes the...
Understanding if-elseStatements Summary In this lab, you will complete a prewritten Java program that computes the largest and smallest of three integer values. The three values are –50, 53, and 78. Instructions Two variables named largest and smallest are declared for you. Use these variables to store the largest and smallest of the three integer values. You must decide what other variables you will need and initialize them if appropriate. Write the rest of the program using assignment statements, if...
Do the following lab by while or Do-while loop. question: Write a c++ program that asks...
Do the following lab by while or Do-while loop. question: Write a c++ program that asks students to enter 3 valid grades and then calculate the average and print it. if the user enters the invalid grade you should print the error message and get the new grade. Hint1: each time your program ask the following question: "Do you want to calculate another average?" and if the answer to this question is "Y" or "y" then you should continue. Hint2:...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT