Section12.12Unit 2a Subgoal Labeled Puzzles for Toggle Code
These are copies of the Selection Mixed Up Code Practice puzzles with subgoal labels added. The Selection Toggle Code Practice page uses them as the Get Help puzzles for its write code problems. Each branch of an if statement is one block, so one label sits on each branch.
The following program segment should print if your guess is correct, too high, or too low. 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.
// Store the guess and the answer
int guess = 10;
int answer = 5;
---
// Check for a correct guess
if (guess == answer)
{
System.out.println("You are right!");
} // end if
---
// Check for a correct guess
if (guess = answer)
{
System.out.println("You are right!");
} // end if #paired
---
// Check for a guess that is too high
else if (guess > answer)
{
System.out.println("Your guess is too high");
} // end else if
---
// Check for a guess that is too high
else if (guess < answer)
{
System.out.println("Your guess is too high");
} // end else if #paired
---
// Otherwise, report a guess that is too low
else
{
System.out.println("Your guess is too low");
} // end else
The following program segment should print either βYou can go outβ if you donβt have any homework and have cleaned and otherwise should print βYou can not go outβ. 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)
{
---
// Store whether homework is left and cleaning is done
boolean homeworkLeft = false;
boolean cleaned = true;
---
// Check for permission to go out
if (!homeworkLeft && cleaned)
{
System.out.println("You can go out");
} // end if
---
// Check for permission to go out
if (homeworkLeft && cleaned)
{
System.out.println("You can go out");
} // end if #paired
---
// Check for permission to go out
if (!homeworkLeft || cleaned)
{
System.out.println("You can go out");
} // end if #paired
---
// Otherwise, report no permission to go out
else
{
System.out.println("You can not go out");
} // end else
---
} // end main method
} // end of class
The main method in the following class should print if x is in the range of 1 to 10 (inclusive) or not. 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)
{
---
// Store the number to test
int x = 3;
---
// Check for a number in the range
if (x >= 1 && x <= 10)
{
System.out.println("1 <= x <= 10");
} // end if
---
// Check for a number in the range
if (x >= 1 || x <= 10)
{
System.out.println("1 <= x <= 10");
} // end if #paired
---
// Check for a number in the range
if (x > 1 && x < 10)
{
System.out.println("1 <= x <= 10");
} // end if #paired
---
// Otherwise, report a number outside the range
else
{
System.out.println("x is not in range");
} // end else
---
} // end main method
} // end of class
The main method in the following class should print out if a string has the word βringingβ in it or not. 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)
{
---
// Store the message
String message = "Is that the phone ringing?";
---
// Check for a message with the word
if (message.indexOf("ringing") >= 0)
{
System.out.println("Answer the phone!");
} // end if
---
// Check for a message with the word
if (message.indexOf("ringing") > 0)
{
System.out.println("Answer the phone!");
} // end if #paired
---
// Check for a message with the word
if (message.indexof("ringing") >= 0)
{
System.out.println("Answer the phone!");
} // end if #paired
---
// Otherwise, report a message without the word
else
{
System.out.println("I don't hear anything.");
} // end else
---
} // end main method
} // end of class
The main method in the following class should print if your favorite food is junk food (pizza or wings) or not. 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)
{
---
// Store the favorite food
String favFood = "kale";
---
// Compare the favorite food with each junk food
boolean favPizza = favFood.equals("pizza");
boolean favWings = favFood.equals("wings");
---
// Compare the favorite food with each junk food
boolean favPizza = favFood == "pizza";
boolean favWings = favFood == "wings"; #paired
---
// Check for a junk food favorite
if (favPizza || favWings)
{
System.out.println("Your fav is junk food");
} // end if
---
// Check for a junk food favorite
if (favPizza && favWings)
{
System.out.println("Your fav is junk food");
} // end if #paired
---
// Otherwise, report a favorite that is not junk food
else
{
System.out.println("Your fav is not junk");
} // end else
---
} // end main method
} // end of class
The main method in the following class should print your fine if you are speeding. If you are going over 65 but less than 75 the fine is 50. If you are going at least 75 and less than 85 the fine is 100. Over that the fine is 200. It does not print anything if you are not speeding. 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)
{
---
// Store the speed
int speed = 90;
---
// Check for a speed with the smallest fine
if (speed > 65 && speed < 75)
{
System.out.println("50");
} // end if
---
// Check for a speed with the smallest fine
if (speed > 65 || speed < 75)
{
System.out.println("50");
} // end if #paired
---
// Check for a speed with a medium fine
else if (speed >= 75 && speed < 85)
{
System.out.println("100");
} // end else if
---
// Check for a speed with a medium fine
else if (speed >= 75 || speed < 85)
{
System.out.println("100");
} // end else if #paired
---
// Check for a speed with the largest fine
else if (speed >= 85)
{
System.out.println("200");
} // end else if
---
} // end main method
} // end of class
The main method in the following class should print the alarm time. If it is a weekday you should get up at 7:00am and if not get up at 10:00am. 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)
{
---
// Store whether it is the weekend
boolean weekend = false;
---
// Check for a weekday
if (!weekend)
{
System.out.println("7:00am");
} // end if
---
// Check for a weekday
if (weekend)
{
System.out.println("7:00am");
} // end if #paired
---
// Otherwise, report the weekend alarm time
else
{
System.out.println("10:00am");
} // end else
---
} // end main method
} // end of class
The main method in the following class should print if you can text now. You can text if you are not driving and not eating. 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)
{
---
// Store whether you are driving and eating
boolean driving = true;
boolean eating = false;
---
// Check for permission to text
if (!driving && !eating)
{
System.out.println("Can text now");
} // end if
---
// Check for permission to text
if (!driving || !eating)
{
System.out.println("Can text now");
} // end if #paired
---
// Otherwise, report no permission to text
else
{
System.out.println("Can't text now");
} // end else
---
} // end main method
} // end of class
The main method in the following class should print if your name starts with a vowel or not. 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)
{
---
// Store the name and its lowercase first letter
String name = "Julian";
String firstLetter = name.substring(0,1);
String lowerFirst = firstLetter.toLowerCase();
---
// Compare the first letter with each vowel
boolean aF = lowerFirst.equals("a");
boolean eF = lowerFirst.equals("e");
boolean iF = lowerFirst.equals("i");
boolean oF = lowerFirst.equals("o");
boolean uF = lowerFirst.equals("u");
---
// Compare the first letter with each vowel
boolean aF = lowerFirst == "a";
boolean eF = lowerFirst == "e";
boolean iF = lowerFirst == "i";
boolean oF = lowerFirst == "o";
boolean uF = lowerFirst == "u"; #paired
---
// Check for a name starting with a vowel
if (aF || eF || iF || oF || uF)
{
System.out.println("Starts with a vowel");
} // end if
---
// Check for a name starting with a vowel
if (aF && eF && iF && oF && uF)
{
System.out.println("Starts with a vowel");
} // end if #paired
---
// Otherwise, report a name starting with a consonant
else
{
System.out.println("Starts with a consonant");
} // end else
---
} // end main method
} // end of class
The main method in the following class should print your grade for score. 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)
{
---
// Store the score
int score = 73;
---
// Check for an excellent score
if (score >= 90)
{
System.out.println("A");
} // end if
---
// Check for a good score
else if (score >= 80)
{
System.out.println("B");
} // end else if
---
// Check for a good score
if (score >= 80)
{
System.out.println("B");
} // end if #paired
---
// Check for an average score
else if (score >= 70)
{
System.out.println("C");
} // end else if
---
// Check for a poor score
else if (score >= 60)
{
System.out.println("D");
} // end else if
---
// Otherwise, report a failing score
else
{
System.out.println("E");
} // end else
---
} // end main method
} // end of class
The get_mid(str) method in the StringWorker class should return the middle character if the string has an odd number of characters and the middle two characters if the string has an even number of characters. For example, get_mid("way") should return "a" and get_mid("away") should return "wa". The blocks have been mixed up and include extra blocks that are not needed in a correct solution.
public class StringWorker
{
---
public static String get_mid(String str)
{
---
// Set up the length and middle position the method needs
int str_len = str.length();
int mid = str_len / 2;
---
// Check for an odd number of characters
if (str_len % 2 == 1)
{
return str.substring(mid, mid+1);
} // end if
---
// Check for an odd number of characters
if (str_len % 2 == 0)
{
return str.substring(mid, mid+1);
} // end if #paired
---
// Otherwise, return the middle two characters
else
{
return str.substring(mid-1, mid+1);
} // end else
---
// Otherwise, return the middle two characters
else
{
return str.substring(mid, mid+2);
} // end else #paired
---
} // end method get_mid
---
} // end of class