Instructions: Follow the steps given in each section below. For each question, click the radio button next to the most correct answer. When you are finished, type your name and your partner at the end of the document, write down your answers for your records, then press the Submit button. You may submit just one lab response. If you submit more than one, only the first one counts for a grade.
We examined the if and if-else statements in C SC 120. In this lab, we'll examine selection statements in more depth. By the end of the lab, you should be familiar with relational operators, String comparisons, the nested if statement, the dangling-else problem, Boolean operators, and DeMorgan's Laws.
==
!=
<
>
<=
>=
if (amount > 100) { System.out.println("amount is more than 100"); }
// Demonstration of incorrect use of = in if statements public class EqualSigns { public static void main(String[] args) { int firstInt = 1, secondInt = 2; boolean firstBool = false, secondBool = true; // The compiler finds this error if (firstInt = secondInt) { System.out.println("The two int variables are equal " + "(1 equals 2)"); } // The compiler doesn't find this error if (firstBool = secondBool) { System.out.println("The two boolean variables are equal " + "(false equals true)"); } } }
The two int variables are equal (1 equals 2) The two boolean variables are equal (false equals true)
// This program is supposed to let you control the turtle by typing // in either the word move or turn. (Wrong!) import Keyboard; import turtlegraphics.*; public class ControlTurtle { public static void main(String[] args) throws java.io.IOException, TurtleException { Turtle myTurtle = new Turtle(); String response = Keyboard.readString("Should the turtle move or turn? "); if (response == "move") { System.out.println("Moving ..."); myTurtle.move(100); } else { System.out.println("Turning ..."); myTurtle.turnRight(90); } } }
if (response.equals("move")) ...
Because the string literal value "move" is also a string, you can use the equals() method on the string literal value instead of the variable. Therefore you can also check whether the string variable response equals "move" like this
if ("move".equals(response)) ...
Either way is fine.
(The Java language, like most programming languages, says that string1 is "less than" string2 if string1 comes before string2 in a dictionary ordering.)
// Demonstrates use of the compareTo() method. // Replace each ?? with == 0, != 0, > 0, < 0, >= 0, or <= 0 so the // program gives the correct answers. import Keyboard; public class DemonstrateCompareTo { public static void main(String[] args) throws java.io.IOException { String string1 = Keyboard.readString("Enter string1: "); String string2 = Keyboard.readString("Enter string2: "); if (string1.compareTo(string2) ??) { System.out.println(string1 + " is less than " + string2); } if (string1.compareTo(string2) ??) { System.out.println(string1 + " is equal to " + string2); } if (string1.compareTo(string2) ??) { System.out.println(string1 + " is greater than " + string2); } if (string1.compareTo(string2) ??) { System.out.println(string1 + " is not equal to " + string2); } if (string1.compareTo(string2) ??) { System.out.println(string1 + " is less than or equal to " + string2); } if (string1.compareTo(string2) ??) { System.out.println(string1 + " is greater than or equal to " + string2); } } }
// Demonstrates use of a nested if statement import Keyboard; public class DemonstrateNestedIf { public static void main(String[] args) throws java.io.IOException { char response = Keyboard.readChar("Compare two numbers? (y/n): ", "yn"); if (response == 'y' || response == 'Y') { int first = Keyboard.readInt("Enter first number: "); int second = Keyboard.readInt("Enter second number: "); // This is the "nested" if statement if (first < second) { System.out.println("The first number is less"); } else { System.out.println("The first number is not less"); } } else { System.out.println("Ok - but it's your loss!"); } } }
// This program has a dangling-else problem import Keyboard; public class DemonstrateDanglingElse { public static void main(String[] args) throws java.io.IOException { int year = 0; int studentStatus = Keyboard.readInt("Are you a (1) undergrad or (2) grad? ", 1, 2); if (studentStatus == 1) year = Keyboard.readInt("Are you (1) freshman, (2) soph, (3) jr, (4) senior? ", 1, 4); // This nested if has a dangling-else problem if (studentStatus == 1) if (year > 1) System.out.println("You're probably glad to be past your first year!"); else System.out.println("Congratulations on being a graduate student!"); } }
if (studentStatus == 1) if (year > 1) System.out.println("You're probably glad to be past your first year!"); else System.out.println("Congratulations on being a graduate student!");
if (studentStatus == 1) { if (year > 1) System.out.println("You're probably glad to be past your first year!"); } else System.out.println("Congratulations on being a graduate student!");
// Demonstrates Boolean operators. // Replace each ?? with && or || so the program works correctly import Keyboard; public class DemonstrateBooleanOps { public static void main(String[] args) throws java.io.IOException { int number = Keyboard.readInt("Enter a number: "); // Check whether the user typed a number between 0 and 100 if (number >= 0 ?? number <= 100) { System.out.println("You typed a number between 0 and 100"); } else { System.out.println("You typed a number outside the range of 0 and 100"); } // Check whether the user typed a number that is a multiple of // 2 or 3 if (number %2 == 0 ?? number % 3 == 0) { System.out.println("You typed a number that's a multiple of 2 or 3"); } else { System.out.println("You typed a number that's NOT a multiple of 2 or 3"); } } }
if (!(x > y)) System.out.println("yes");
if (x >= y) System.out.println("yes");
if (x < y) System.out.println("yes");
if (x != y) System.out.println("yes");
if (x == y) System.out.println("yes");
if (x <= y) System.out.println("yes");
!(a && b) --> !a || !b !(a || b) --> !a && !b
if (!(firstBool && secondBool)) ...
if (firstBool || secondBool) ...
if (!firstBool && !secondBool) ...
if (!firstBool || !secondBool) ...
if (!firstBool || secondBool) ...
if (firstBool && secondBool) ...
if (!(firstBool || secondBool)) ...
if (!(x < 3 || y <= 2)) ...
if (x > 3 && y <= 2) ...
if (x < 3 && y >= 2) ...
if (x >= 3 && y > 2) ...
if (x <= 3 || y > 2) ...
if (x < 3 || y >= 2) ...
Write down your answers for your records, then press the Submit button below to turn in the lab assignment.