CS 163/164, Fall 2017
Lab 5 - Booleans, Comparisons, Conditionals
Thursday, Sep. 7th or
Friday, Sep. 8th
Objectives of this Lab
- Do the Eclipse setup for the next programming assignment,
- learn about Java boolean data type,
- practice with comparison operators, and
- write a conditional and switch statements.
Your TA will help you setup Eclipse for the P3 programming assignment,
Create a new Java Project named R5, and make a class named R5.
Your TA will explain boolean variables, comparison operators, logical operators, if statements,
and switch statements. After that you will code. See the sample output below to see exactly what
should be printed. Following the instructions below to explore boolean variables.
- Declare a variable named boolean0 of type boolean, with an initial value of true.
- Declare a variable named boolean1 of type boolean, with an initial value of false.
- Write two System.out.println statements that print boolean0 and boolean1, as shown below.
- NOTE: Do not put quotes around boolean values, they're not strings!
The instructions below introduce comparison operators, which are used to compare
integer and floating-point values and other primitive types such as characters.
- Declare a variable named equals of type boolean, and set it's value to the comparison (11 == 33).
- Declare a variable named notEquals of type boolean, and set it's value to the comparison (22 !=44).
- Declare a variable named greaterThan of type boolean, and set it's value to the comparison (15 > 25).
- Declare a variable named lessThan of type boolean, and set it's value to the comparison (-11 < 13).
- Write System.out.println statement to print each variable above, as shown below.
The instructions below introduce conditional statements, which use if, if/else, and if/else if/else
to conditionally handle different values of integers.
- Declare a Scanner and initialize it to read from the keyboard.
- Declare integer variables named integer0 and integer1.
- Prompt the user as follows: "Enter first integer: " and read integer0 from the keyboard.
- Prompt the user as follows: "Enter second integer: " and read integer1 from the keyboard.
- Write an if statement that prints "First integer is larger than the second." when integer0 > integer1.
- Add an else if statement that prints "Second integer is larger than the first." when integer1 > integer0.
- Finish with an else statement that prints "Both integers are equal." when integer0 == integer1.
The program segment that you will write in this section introduces switch statements,
which are really just a more efficient way of writing conditionals. You can write switch
statements for integer values, including characters, or String values. Here are the
- Declare a String variable named myString.
- Use the Scanner from the previous section to read it, using the prompt: "Enter a state: ".
- NOTE: You must use the next() method in Scanner, not nextLine().
- Write a switch statement that does the following:
- Prints "Southern State" if myString is "Alabama" or "Florida".
- Prints "Western State" if myString is "Colorado" or "Utah".
- Prints "Northern State" if myString is "Michigan" or "Wisconsin".
- Prints "Eastern State" if myString is "Delaware" or "Maine".
- Prints "Not sure where that is, must be in the Midwest!" for any other value of myString.
boolean0 is true.
boolean1 is false.
(11 == 33) is false
(22 != 44) is true
(15 > 25) is false
(-11 < 13) is true
Enter first integer: 123
Enter second integer: 234
Second integer is larger than the first.
Enter a state: Colorado
- Make sure your R5.java compiles and produces 11 lines of output.
- Have your TA review your output for credit.