CSU Banner

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

  1. Configure zyBooks with your eid
  2. Do the Eclipse setup for the next programming assignment,
  3. learn about Java boolean data type,
  4. practice with comparison operators, and
  5. 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.
  1. Declare a variable named boolean0 of type boolean, with an initial value of true.
  2. Declare a variable named boolean1 of type boolean, with an initial value of false.
  3. Write two System.out.println statements that print boolean0 and boolean1, as shown below.
  4. 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.
  1. Declare a variable named equals of type boolean, and set it's value to the comparison (11 == 33).
  2. Declare a variable named notEquals of type boolean, and set it's value to the comparison (22 !=44).
  3. Declare a variable named greaterThan of type boolean, and set it's value to the comparison (15 > 25).
  4. Declare a variable named lessThan of type boolean, and set it's value to the comparison (-11 < 13).
  5. 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.
  1. Declare a Scanner and initialize it to read from the keyboard.
  2. Declare integer variables named integer0 and integer1.
  3. Prompt the user as follows: "Enter first integer: " and read integer0 from the keyboard.
  4. Prompt the user as follows: "Enter second integer: " and read integer1 from the keyboard.
  5. Write an if statement that prints "First integer is larger than the second." when integer0 > integer1.
  6. Add an else if statement that prints "Second integer is larger than the first." when integer1 > integer0.
  7. 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:
  1. Declare a String variable named myString.
  2. Use the Scanner from the previous section to read it, using the prompt: "Enter a state: ".
  3. NOTE: You must use the next() method in Scanner, not nextLine().
  4. Write a switch statement that does the following:

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



CS Banner
CS Building