CS 163/164, Spring 2018
Lab 5 - Booleans, Comparisons, Conditionals
Thursday, Feb. 1st or
Friday, Feb. 2nd
Join the help queue right away at the start of lab if you need a Lab 4 regrade.
The assistant TA will come by while the lead starts the presentation.
Objectives of this Lab
- Configure zyBooks with your eid
- 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.
Configure zyBooks
In order for zyBooks to talk to Canvas, your eid must be in zyBooks. This is
entered in the "Student ID" field when registering. This must be your eid and not
your CSUID. Your eid is the username you typed to log on to the CS linux systems
and to log into Canvas. Your CSUID is a 9-digit number that is on the front of your
student ID card. Treat your username with the same privacy you would treat your e-mail
address. Treat your CSUID with the same privacy as a Social Security Number (SSN).
Calculator Setup
Your TA will help you setup Eclipse for the P3 programming assignment,
linked here.
Getting Started
Create a new Java Project named R5, and make a class named R5.
Boolean Variables
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!
Comparison Operators
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.
Conditional Statements
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.
Switch Statements
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
instructions:
- 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.
sample Output
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
Western State
- Make sure your R5.java compiles and produces 11 lines of output.
- Have your TA review your output for credit.
- You must also show an activated zyBooks with your eid in the Student ID field.