teaching machines

CS 145 Lab 4 – Logical Operators and Conditional Statements

October 4, 2014 by . Filed under cs145, fall 2014, labs.

First, if you have checkpoints left over from last lab, get them inspected during the first 15 minutes of this lab. No credit will be awarded past these 15 minutes.

Don’t forget to work in pairs! Where possible, please work with someone that you did not work with last week. The exchange of new ideas and perspectives is not an opportunity you want to rob yourself of.

Objective

To make our software respond to the world around us, it must ask questions of data. Questions are asked using relational and logical operators, and the CPU is turned left or right based on the answers using conditional statements. We will look at these operators and conditional statements in this lab.

Checkpoint 1

Person A types.

Declare and assign the following variables, all describing some individual person:

Now, craft expressions using the variables above for the following derived attributes of this person. Do not write if statements; write only boolean expressions. Test them under various values of the person’s variables.

  1. A person is eligible for benefits if he/she works half-time (20 hours) or more on average each week.
    boolean isBenefitsEligible =
  2. A person is a teenager if he/she is between 13 and 19 years old, inclusively.
    boolean isTeenager =
  3. A person is a bachelor if he is an unmarried man.
    boolean isBachelor =
  4. A person is busy if he/she has children or typically works over 45 hours a week on average.
    boolean isBusy =
  5. See the variable name.
    boolean notBornInOctober1990 =
  6. A person goes to Starbucks if he/she earns over $30,000 or drinks coffee.
    boolean goesToStarbucks = 
  7. A person can have children if he is male. A female can also have children if she is less than 45 years old and does not drink coffee.
    boolean canHaveChildren =

Checkpoint 2

Person B types.

Write a method named getOrdinal that takes in an int parameter and returns a String in which the parameter is followed by its correct ordinal suffix: “st”, “nd”, “rd”, or “th”.