CS 145 Lab 4 – Logical Operators and Conditional Statements
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.
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.
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.
- A person is eligible for benefits if he/she works half-time (20 hours) or more on average each week.
boolean isBenefitsEligible =
- A person is a teenager if he/she is between 13 and 19 years old, inclusively.
boolean isTeenager =
- A person is a bachelor if he is an unmarried man.
boolean isBachelor =
- A person is busy if he/she has children or typically works over 45 hours a week on average.
boolean isBusy =
- See the variable name.
boolean notBornInOctober1990 =
- A person goes to Starbucks if he/she earns over $30,000 or drinks coffee.
boolean goesToStarbucks =
- 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 =
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”.