CS 171 & 171L Introduction to Computer Science I & Lab Fall 2017 

Lab 3 - Conditionals and Booleans

The purpose of this lab is to get you experimenting with and using Java conditionals and boolean expressions. Whatever you do not complete in lab today, you should try and complete on your own or with your lab partner in the next few days.

Rules For The Lab

You will play two different roles in the lab. You will either be the driver or navigator.
  • driver: The person typing at the keyboard.
  • navigator: The person watching for mistakes, making suggestions, and thinking ahead.
Important: The navigator must not touch the keyboard or the mouse. If the navigator either types or moves the mouse, the navigator will get a zero for the lab.

You have been assigned identifiers s1 and s2 when you entered the lab. At the start of the lab, s1 will be the driver and s2 will be the navigator. Your roles will switch during the lab.


Your first task is to introduce yourself to your lab partner. Tell your lab partner your name, how your semster is going so far, and if you did anything fun over the weekend.

The driver must start jGRASP now. After every step, switch roles between driver and navigator.


Java (and Processing) and many other languages have variables that can store a Boolean (TRUE or FALSE) value. In Java, variables of type boolean can represent a Boolean value:
boolean b1 = true;
boolean b2 = false;

The result of a comparison (such as <, >, <=, >=, ==, !=) is also a booolean. Variables of type boolean can be combined with booleans to yield new booleans based on standard Boolean logic, defined as follows
XY&& (AND)|| (OR)!X (NOT X)
Using boolean variables, one can create complex expressions that reresent many situations.

We have seen several ways to conditionally perform computations that rely on boolean logic:

  • the ?: operator
  • the if statement
  • the if-else statement
There is also a switch statement that we will see soon, but on another day.

Download the lab

Download the source file Lab3.java and save it on your computer. It contains the stubs of several static methods. Save it directly to your folder with your other lab files and open it in jGRASP.

// Your names here!
// Save as Lab3.java
// run the main method (at the bottom) to test your functions
//    using the red running person icon on the speedbar
// You can also test using: Lab3.methodName(actualParameters)

public class Lab3 {

// these methods are stubs with a default return value
// replace the code in between the curlies {} for each method
// don't change the function signatures:
//    (name, return type, number or parameters, types of parameters)

// returns the absolute value of x:
// x, if x is positive or 0
// -x, if x is negative
   public static double abs(double x) {
      return 0.0;

// returns true when n is even
// returns false when n is odd
   public static boolean isEven(int n) {
      return false;

// returns true when n is odd
// returns false when n is even
   public static boolean isOdd(int n) {
      return false;

// returns true if c is a vowel (a, e, i, o, or u) 
// This should work for both upper or lower case
// remeber single quotes specify a literal char value
// thus 'a' is the first letter of the alphabet
   public static boolean isVowel(char c) {
      return false;

// returns true if n is a perfect square
// n=a*a for some a
// hint: use (int) Math.sqrt(n)
   public static boolean isSquare(int n) {
      return false;

// returns v when v is a valid color index from 0 to 255 inclusive
// returns 0 when v is negative
// returns 255 when v is larger than 255
   public static int clip(int v) {
      return 0;

// returns hours*rate when hours are less than or equal to 40
// returns the base rate plus any hours above 40 at 1.5 times base rate
// Michigan minimum wage is $8.90/hour
   public static double wages(double hours, double rate) {
      return 0.0;

// returns true if (x,y) is inside the rectangle with corners (x1,y1) and (x2,y2)
// Assume x1>>> 31
// 2 (February) >>>> 28 (normally) or 29 (leap year)
// 3 (March) >>>> 31
// 4 (April) >>>> 30
// 5 (May) >>>> 31
// 6 (June) >>>> 30
// 7 (July) >>>> 31
// 8 (August) >>>> 31
// 9 (September) >>>> 30
// 10 (October) >>>> 31
// 11 (November) >>>> 30
// 12 (December) >>>> 31
// Hint: Use the || operator and your isLeapYear() method
   public static int daysInMonth(int month, int year) {
      return 0;

// determine if a household is in poverty depending on the 
// following cutoff values:
// 12,082 for 1 person
// 15,391 for 2 people
// 18,871 for 3 people
// 24,257 for 4 people
// 28,741 for 5 people
// 32,542 for 6 people
// 36,998 for 7 people
// 41,029 for 8 people
// 49,177 for 9 people
// https://www.census.gov/newsroom/press-releases/2016/cb16-158.html
   public static boolean inPoverty(double wages, int people) {
      return false;

// single taxes based on income
// https://www.irs.com/articles/projected-us-tax-rates-2016
// income : tax rate
//$0$9,275 : 10%
//$9,276$37,650 : $927.50 plus 15% of the amount over $9,275
//$37,651$91,150 : $5,183.75 plus 25% of the amount over $37,650
//$91,151$190,150 : $18,558.75 plus 28% of the amount over $91,150
//$190,151$ 413,350 : $46,278.75 plus 33% of the amount over $190,150
//$413,351$415,050 : $119,934.75 plus 35% of the amount over $413,350
//$415,051 or more : $120,529.75 plus 39.6% of the amount over $415,050
   public static double taxes(double income) {
      return 0.0;
   // don't change the main method below except to add additional test cases
   public static void main(String args[]) {
      System.out.println("Lab 3");
      System.out.printf("abs(%f) = %f should be 1\n", -1D, abs(-1));
      System.out.printf("abs(%f) = %f should be 0\n", 0D, abs(0)); 
      System.out.printf("abs(%f) = %f should be 1\n", 1D, abs(1));
      System.out.printf("isEven(%d) = %b should be true\n", -2, isEven(-2));
      System.out.printf("isEven(%d) = %b should be true\n", 0, isEven(0)); 
      System.out.printf("isEven(%d) = %b should be false\n", 1, isEven(1));
      System.out.printf("isOdd(%d) = %b should be false\n", -2, isOdd(-2));
      System.out.printf("isOdd(%d) = %b should be false\n", 0, isOdd(0)); 
      System.out.printf("isOdd(%d) = %b should be true\n", 1, isOdd(1));
      System.out.printf("isVowel(%c) = %b should be true\n", 'A', isVowel('A'));
      System.out.printf("isVowel(%c) = %b should be true\n", 'a', isVowel('a'));
      System.out.printf("isVowel(%c) = %b should be true\n", 'e', isVowel('e'));
      System.out.printf("isVowel(%c) = %b should be true\n", 'i', isVowel('i'));
      System.out.printf("isVowel(%c) = %b should be true\n", 'o', isVowel('o'));
      System.out.printf("isVowel(%c) = %b should be true\n", 'u', isVowel('u'));
      System.out.printf("isVowel(%c) = %b should be false\n", 'b', isVowel('b'));
      System.out.printf("isSquare(%d) = %b should be true\n", 0, isSquare(0));
      System.out.printf("isSquare(%d) = %b should be true\n", 1, isSquare(1));
      System.out.printf("isSquare(%d) = %b should be false\n", 2, isSquare(2));
      System.out.printf("isSquare(%d) = %b should be false\n", 3, isSquare(3));
      System.out.printf("isSquare(%d) = %b should be true\n", 4, isSquare(4));
      System.out.printf("isSquare(%d) = %b should be false\n", 65535, isSquare(65535));
      System.out.printf("clip(%d) = %d should be 0\n", -1, clip(-1));
      System.out.printf("clip(%d) = %d should be 0\n", 0, clip(0));
      System.out.printf("clip(%d) = %d should be 128\n", 128, clip(128));
      System.out.printf("clip(%d) = %d should be 255\n", 255, clip(255));
      System.out.printf("clip(%d) = %d should be 255\n", 256, clip(256));
      System.out.printf("wages(10,8.90) = %.2f should be 89.00\n", wages(10,8.90));
      System.out.printf("wages(40,8.90) = %.2f should be 356.00\n", wages(40,8.90));
      System.out.printf("wages(50,8.90) = %.2f should be 489.50\n", wages(50,8.90));
      System.out.printf("inRectangle(0,0,0,0,10,10) = %b should be true\n", inRectangle(0,0,0,0,10,10));
      System.out.printf("inRectangle(0,0,5,5,10,10) = %b should be false\n", inRectangle(0,0,5,5,10,10));
      System.out.printf("inRectangle(100,10,50,15,150,150) = %b should be false\n", inRectangle(100,10,50,15,150,150));
      System.out.printf("inRectangle(100,10,50,5,150,150) = %b should be true\n", inRectangle(100,10,50,5,150,150));
      System.out.printf("inEllipse(100,100,100,100,20,30) = %b should be true\n", inEllipse(100,100,100,100,20,30));
      System.out.printf("inEllipse(120,130,100,100,20,30) = %b should be false\n", inEllipse(120,130,100,100,20,30));
      System.out.printf("dayTime(1) = \"%s\" should be \"night\"\n", dayTime(1));
      System.out.printf("dayTime(5) = \"%s\" should be \"night\"\n", dayTime(5));
      System.out.printf("dayTime(23) = \"%s\" should be \"night\"\n", dayTime(23));
      System.out.printf("dayTime(8) = \"%s\" should be \"morning\"\n", dayTime(8));
      System.out.printf("dayTime(13) = \"%s\" should be \"afternoon\"\n", dayTime(13));
      System.out.printf("dayTime(20) = \"%s\" should be \"evening\"\n", dayTime(20));
      System.out.printf("seasonOfTheYear(1,1) = \"%s\" should be \"winter\"\n", seasonOfTheYear(1,1));
      System.out.printf("seasonOfTheYear(3,1) = \"%s\" should be \"winter\"\n", seasonOfTheYear(3,1));
      System.out.printf("seasonOfTheYear(3,31) = \"%s\" should be \"spring\"\n", seasonOfTheYear(3,31));
      System.out.printf("seasonOfTheYear(6,30) = \"%s\" should be \"summer\"\n", seasonOfTheYear(6,30));
      System.out.printf("seasonOfTheYear(9,30) = \"%s\" should be \"fall\"\n", seasonOfTheYear(9,30));
      System.out.printf("seasonOfTheYear(12,30) = \"%s\" should be \"winter\"\n", seasonOfTheYear(12,30));
      System.out.printf("isLeapYear(%d) = %b should be true\n", 2000, isLeapYear(2000));
      System.out.printf("isLeapYear(%d) = %b should be true\n", 2016, isLeapYear(2016));
      System.out.printf("isLeapYear(%d) = %b should be true\n", 2020, isLeapYear(2020));
      System.out.printf("isLeapYear(%d) = %b should be false\n", 2017, isLeapYear(2017));
      System.out.printf("isLeapYear(%d) = %b should be false\n", 2018, isLeapYear(2018));
      System.out.printf("isLeapYear(%d) = %b should be false\n", 1900, isLeapYear(1900));
      System.out.printf("isLeapYear(%d) = %b should be false\n", 2100, isLeapYear(2100));
      System.out.printf("daysInMonth(1,2017) = %d should be 31\n", daysInMonth(1,2017));
      System.out.printf("daysInMonth(2,2017) = %d should be 28\n", daysInMonth(1,2017));
      System.out.printf("daysInMonth(1,2017) = %d should be 29\n", daysInMonth(1,2020));
      System.out.printf("daysInMonth(1,2017) = %d should be 30\n", daysInMonth(9,2017));
   // The poverty rate for families and the number of families in poverty were 
   // 10.4 percent and 8.6 million in 2015, 
   // a decrease from 11.6 percent and 9.5 million families in 2014.
   // https://www.census.gov/newsroom/press-releases/2016/cb16-158.html
   // 40% of the residents of Flint and Detroit live in poverty. 
   // More than half of this population are children.
   // https://poverty.umich.edu/about/poverty-facts/
      System.out.printf("inPoverty(10000,1) = %b should be true\n", inPoverty(10000,1));
      System.out.printf("inPoverty(20000,1) = %b should be false\n", inPoverty(20000,1));
      System.out.printf("inPoverty(15391,2) = %b should be true\n", inPoverty(15391,2));
      System.out.printf("inPoverty(49177,9) = %b should be true\n", inPoverty(49177,9));
      System.out.printf("taxes(8000) = %.2f should be 800.00\n", taxes(8000));
      System.out.printf("taxes(12082) = %.2f should be 955.57\n", taxes(12082));
      System.out.printf("taxes(500000) = %.2f should be 150262.25\n", taxes(500000));

When you are finished, email your lab files to your lab partner and instructor.

Copyright © 2017, David A. Reimann. All rights reserved.