In: Computer Science
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
Ensure the file named Multiply.java is open.
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.
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.
Execute the program by clicking Run. Record the output of this program.
// 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.
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.