Section12.13Unit 2b Subgoal Labeled Puzzles for Toggle Code
These are copies of the Loops Mixed Up Code Practice puzzles with subgoal labels added. The Loops Toggle Code Practice page uses them as the Get Help puzzles for its write code problems. Each loop header and its opening brace are one block, so one label sits on each loop.
The following program segment should print out all the values from 20 to 30 (20, 21, 22, … 30). But, the blocks have been mixed up and include extra blocks that are not needed in a correct solution. Drag the blocks from the left and put them in the correct order on the right. Click the Check button to check your solution.
// Start the counter at 20
int x = 20;
---
// Keep looping while the counter is at most 30
while (x <= 30)
{
---
// Keep looping while the counter is at most 30
while (x < 30)
{ #paired
---
// Keep looping while the counter is at most 30
while (x <= 30);
{ #paired
---
// Print the counter's current value
System.out.println(x);
---
// Add one to the counter so it moves toward 30
x++;
---
} // end while loop
The following program segment should print a countdown from 15 to 0 (15, 14, 13, … 0). But the blocks have been mixed up and include extra blocks that are not needed in a correct solution. Drag the needed blocks from the left and put them in the correct order on the right. Click the Check button to check your solution.
public class Test1
{
---
public static void main(String[] args)
{
---
// Use a for loop counting down from 15 to 0 by ones
for (int i = 15; i >= 0; i--)
{
---
// Use a for loop counting down from 15 to 0 by ones
for (int i = 15; i > 0; i--)
{ #paired
---
// Use a for loop counting down from 15 to 0 by ones
for (int i = 15; i <= 0; i--)
{ #paired
---
// Print the counter's current value
System.out.println(i);
} // end for loop
---
} // end main method
} // end of class
The main method in the following class should print up from 0 to 50 by 5 (0, 5, 10, 15 … 50). But, the blocks have been mixed up and include extra blocks that are not needed in a correct solution. Drag the needed blocks from the left and put them in the correct order on the right. Click the Check button to check your solution.
public class Test1
{
---
public static void main(String[] args)
{
---
// Start the counter at 0
int x = 0;
---
// Keep looping while the counter is at most 50
while (x <= 50)
{
---
// Keep looping while the counter is at most 50
while (x < 50)
{ #paired
---
// Keep looping while the counter is at most 50
while (x <= 50);
{ #paired
---
// Print the counter's current value
System.out.println(x);
---
// Add 5 to the counter so it moves toward 50
x = x + 5;
---
// Add 5 to the counter so it moves toward 50
x + 5; #paired
---
} // end while loop
---
} // end main method
} // end of class
The main method in the following class should print out the values from 0 to 100 by 20’s (0, 20, 40, .. 100). But, the blocks have been mixed up and include extra blocks that are not needed in a correct solution. Drag the needed blocks from the left and put them in the correct order on the right. Click the Check button to check your solution.
public class Test1
{
---
public static void main(String[] args)
{
---
// Use a for loop counting up from 0 to 100 by 20s
for (int i = 0; i <= 100; i+=20)
{
---
// Use a for loop counting up from 0 to 100 by 20s
for (int i = 0; i <= 100; i + 20)
{ #paired
---
// Use a for loop counting up from 0 to 100 by 20s
for (int i = 0; i < 100; i+=20)
{ #paired
---
// Print the counter's current value
System.out.println(i);
} // end for loop
---
} // end main method
} // end of class
The main method in the following class should print out the values from 100 to 0 by 10’s (100, 90, 80, … 0). But, the blocks have been mixed up and include extra blocks that are not needed in a correct solution. Drag the needed blocks from the left and put them in the correct order on the right. Click the Check button to check your solution.
public class Test1
{
---
public static void main(String[] args)
{
---
// Use a for loop counting down from 100 to 0 by 10s
for (int i = 100; i >= 0; i -= 10)
{
---
// Use a for loop counting down from 100 to 0 by 10s
for (int i = 100; i > 0; i -= 10)
{ #paired
---
// Use a for loop counting down from 100 to 0 by 10s
for (int i = 100; i >= 0; i - 10)
{ #paired
---
// Use a for loop counting down from 100 to 0 by 10s
for (int i = 100; i <= 0; i -= 10)
{ #paired
---
// Print the counter's current value
System.out.println(i);
} // end for loop
---
} // end main method
} // end of class
The main method in the following class should print 3 rows with 6 *’s in each row. But, the blocks have been mixed up and include extra blocks that are not needed in a correct solution. Drag the needed blocks from the left and put them in the correct order on the right. Click the Check button to check your solution.
public class Test1
{
---
public static void main(String[] args)
{
---
// Use an outer for loop that runs 3 times, once per row
for (int x = 3; x > 0; x--)
{
---
// Use an outer for loop that runs 3 times, once per row
for (int x = 0; x <= 3; x++)
{ #paired
---
// Use an inner for loop that runs 6 times for each row
for (int y = 6; y > 0; y--)
{
---
// Use an inner for loop that runs 6 times for each row
for (int y = 0; y <= 6; y++)
{ #paired
---
// Use an inner for loop that runs 6 times for each row
for (int y = 6; y > 0; y--);
{ #paired
---
// Print a star without moving to a new line
System.out.print("*");
---
} // end inner for loop
---
// Move to a new line after the row is done
System.out.println();
} // end outer for loop
---
} // end main method
} // end of class
The main method in the following class should print 1 (followed by a newline), then 22 (followed by a newline), and then 333 (followed by a newline). But, the blocks have been mixed up and include extra blocks that are not needed in a correct solution. Drag the needed blocks from the left and put them in the correct order on the right. Click the Check button to check your solution.
public class Test1
{
---
public static void main(String[] args)
{
---
// Use an outer for loop counting rows up from one to three
for (int x = 1; x <= 3; x++)
{
---
// Use an outer for loop counting rows up from one to three
for (int x = 0; x < 3; x++)
{ #paired
---
// Use an inner for loop running as many times as the row number
for (int y = 0; y < x; y++)
{
---
// Use an inner for loop running as many times as the row number
for (int y = 0; y <= x; y++)
{ #paired
---
// Print the row number without moving to a new line
System.out.print(x);
---
// Print the row number without moving to a new line
System.out.print(y); #paired
---
} // end inner for loop
---
// Move to a new line after the row is done
System.out.println();
} // end outer for loop
---
} // end main method
} // end of class
The main method in the following class should print 11111, 22222, 33333, 44444, and 55555. But, the blocks have been mixed up and include extra blocks that are not needed in a correct solution. Drag the blocks from the left and put them in the correct order on the right. Click the Check button to check your solution.
public class Test1
{
---
public static void main(String[] args)
{
---
// Use an outer for loop counting rows up from one to five
for (int x = 1; x <= 5; x++)
{
---
// Use an outer for loop counting rows up from one to five
for (int x = 1; x < 5; x++)
{ #paired
---
// Use an inner for loop that runs five times for each row
for (int y = 0; y < 5; y++)
{
---
// Print the row number without moving to a new line
System.out.print(x);
---
// Print the row number without moving to a new line
System.out.print(y); #paired
---
} // end inner for loop
---
// Move to a new line after the row is done
System.out.println();
} // end outer for loop
---
} // end main method
} // end of class
The main method in the following class should print 11111, 2222, 333, 44, 5. But, the blocks have been mixed up and include extra blocks that are not needed in a correct solution. Drag the needed blocks from the left and put them in the correct order on the right. Click the Check button to check your solution.
public class Test1
{
---
public static void main(String[] args)
{
---
// Use an outer for loop counting rows up from one to five
for (int x = 1; x <= 5; x++)
{
---
// Use an inner for loop counting down from five to the row number
for (int y = 5; y >= x; y--)
{
---
// Use an inner for loop counting down from five to the row number
for (int y = 5; y > x; y--)
{ #paired
---
// Print the row number without moving to a new line
System.out.print(x);
---
// Print the row number without moving to a new line
System.out.print(y); #paired
---
} // end inner for loop
---
// Move to a new line after the row is done
System.out.println();
} // end outer for loop
---
} // end main method
} // end of class
The even_div(s, n) method in the NumWorker class should return the number of digits in s (a string representation of a number) that are evenly divisible by n. For example, even_div("6892",3) should return 2 since both 6 and 9 are evenly divisible by 3. The blocks have been mixed up and include extra blocks that are not needed in a correct solution.
public class NumWorker
{
---
public static int even_div(String s, int n)
{
---
// Start the count of divisible digits at zero
int count = 0;
---
// Visit each digit in the string in order
for (int i = 0; i < s.length(); i++)
{
---
// Visit each digit in the string in order
for (int i = 0; i <= s.length(); i++)
{ #paired
---
// Get the digit at this position in the string
String c_digit = s.substring(i, i+1);
---
// Convert the digit to an integer so you can divide
int c_num = Integer.parseInt(c_digit);
---
// Check for an evenly divisible digit and count it
if (c_num % n == 0)
{
count += 1;
} // end if
---
// Check for an evenly divisible digit and count it
if (c_num / n == 0)
{
count += 1;
} // end if #paired
---
} // end for loop
---
// Return the count of divisible digits after checking every digit
return count;
---
} // end method even_div
---
} // end of class