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

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.

Warmup

Your first task is to introduce yourself to your lab partner. Take turns answering the following question:
Imagine you were the host of a talk show. Who would you want your first guest to be and why?

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

Background

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)
TTTTF
TFFTF
FTFTT
FFFFT
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.pde 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. Complete the stub methods to perform the indicated function. Verify your code by running the program. Add additional test cases as needed.


// Your names here!

// setup() is at the bottom, which will call each of the methods you are completing in this lab

// the methods are stubs with a default return value
//
// replace the code in between the curlies {} for each method
//
// don't change the method 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
// don't use the predefined Math.abs() function!
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 < x2 and y1 < y2
// Hint: use the AND (&&) operators
public static boolean inRectangle(double x, double y, double x1, double y1, double x2, double y2) {
  return false;
}


// returns true if (x,y) is inside the ellipse with center (xc,yc) 
// and x width 2a and y height 2b
// (x-xc)/a squared plus (y-yc)/b squared is less than or equal to 1
public static boolean inEllipse(double x, double y, double xc, double yc, double a, double b) {
  return false;
}


// returns a String based on the hour (0-23) as follows
// morning  lasts from 6 AM to noon (6 to 11)
// afternoon extends from noon to 5 PM (12 to 17)
// evening extends from 6 PM to 10 PM (18 to 22)
// night starts at 11 PM (23) and continues through 5 AM (5)
// return "morning", "afternoon", "evening", or "night"
// Hint: use the OR (||)
public static String dayTime(int hour) {
  return "";
}


// return the northern hemisphere season
// March 21 - 1st day of spring
// June 21 - 1st day of summer
// September 21 - 1st day of fall
// December 21 - 1st day of winter
// return "spring", "summer", "fall", or "winter"
// Hint: use the OR (||) and the AND (&&) operators
public static String seasonOfTheYear(int month, int day) {
  return "";
}


// returns true if the year is a leap year:
// when a year is divisible by 4 (true), unless
// the year is divisible by 100 (false), unless
// the year is divisible by 400 (true)
// Leap years: 2000, 2016, 2020, etc
// Non-leap years: 2017, 2018, 1900, 2100, etc
public static boolean isLeapYear(int year) {
  return false;
}


// returns the number of days in a given month
// 1 (January) >>>> 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
void setup() {
  System.out.println("Lab 3");

  System.out.println();
  System.out.printf("ABS(%f) = %f should be 1\n", -1D, (float)ABS(-1F));
  System.out.printf("ABS(%f) = %f should be 0\n", 0D, ABS(0D)); 
  System.out.printf("ABS(%f) = %f should be 1\n", 1D, ABS(1));

  System.out.println();
  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.println();
  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.println();
  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.println();
  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.println();
  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.println();
  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.println();
  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.println();
  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.println();
  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.println();
  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.println();
  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.println();
  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.println();
  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.println();
  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 © 2020, David A. Reimann. All rights reserved.