teaching machines

CS1: Lecture 14 – Relational Operators

Dear students,

We have seen the Computer as Calculator, a lover of number crunching. We have seen the Computer as Chef, organizing its tasks into self-contained recipes. Now we see the Computer as Philosopher, seeking truth from our data.

Truth is communicated in our programs through the boolean data type. Values of type boolean can only be true or false. We’ve already seen some ways to generate such values: String.startsWith, String.endsWith, String.contains, String.isEmpty, Random.nextBoolean, and so on. There are also a number of operators that yield boolean truths, and these are our focus for today.

Relational Operators

To begin, let’s meet the relational operators: <, <=, >, >=, ==, and !=. == and != are sometimes distinguished as equality operators. We’ll introduce these by writing some methods that serve as predicates, answering yes/no questions like these:

  • Social Security?
    Is person X eligible for Social Security?
  • Walk?
    Should the batter walk to first base?
  • Polygamous?
    Is person X polygamous?
  • Drive legally?
    Is person X (who has been drinking) able to drive legally?
  • Wrong number?
    Has person X guessed the wrong number?
  • Is tattooable?
    Is your loved one’s name tattooable on your knuckles, one letter per?
  • Is blank?
    Is the given string empty?
  • Is logout?
    Is the given string "logout"?
  • Same side?
    Are two numbers on the same side of 0?
  • Is integer?
    Is a given String able to be converted to an int?

TODO

Here’s your TODO list to complete before our next lecture:

  • Homework 2 is due soon. Be sure you are running the SpecChecker on your local machine. If you don’t see “High five!”, then your code doesn’t work. If you don’t properly commit and push, then your code doesn’t work. Do not wait until Monday to finish it. I cannot accommodate your last-minute questions.
  • Read sections 5.3 through 5.4 in your book. On a quarter sheet, express 3 compound truths of life. A compound truth is a statement that combines simpler truths using the logical operators &&, ||, and !. For example, here’s one that may or may not apply to you:
    boolean isEdible = isCooked || !isMeat;
    

See you next class!

Sincerely,

P.S. It’s time for a haiku!

i <3 u
ur at least a third of me
better half, likely

P.P.S. Here’s the code we wrote together in class…

Certainly.java

package lecture1004.cs145;

public class Certainly {
  public static void samesies(int expected, int actual) {
    System.out.printf("%d == %d%n", expected, actual);
  }

  public static void samesies(boolean expected, boolean actual) {
    System.out.printf("%b == %b%n", expected, actual);
  }
}

Booleandrews.java

package lecture1004.cs145;

public class Booleandrews {
  public static void main(String[] args) {
    Certainly.samesies(false, isSocialSecurityEligible(14));
    Certainly.samesies(true, isSocialSecurityEligible(74));
    Certainly.samesies(true, isSocialSecurityEligible(62));
    Certainly.samesies(false, isSocialSecurityEligible(61));

    Certainly.samesies(false, isWalk(0, false));
    Certainly.samesies(true, isWalk(4, false));
    Certainly.samesies(false, isWalk(3, false));

    Certainly.samesies(false, isPolygamous(0));
    Certainly.samesies(false, isPolygamous(1));
    Certainly.samesies(true, isPolygamous(2));
    Certainly.samesies(true, isPolygamous(17));

    Certainly.samesies(true, isLegalToDrive(0));
    Certainly.samesies(true, isLegalToDrive(0.0799999));
    Certainly.samesies(false, isLegalToDrive(0.08));
    Certainly.samesies(false, isLegalToDrive(0.09));
    Certainly.samesies(false, isLegalToDrive(2));

    Certainly.samesies(true, isWrongNumber(11, 7));
    Certainly.samesies(false, isWrongNumber(11, 11));
    Certainly.samesies(false, isWrongNumber(0.001, 0.001));
    Certainly.samesies(false, isWrongNumber(0.001, 0.0005 + 0.0005));
    Certainly.samesies(true, isWrongNumber(0.001, 0.0004 + 0.0005));
    Certainly.samesies(false, isWrongNumber(0.001, 0.0010));

    Certainly.samesies(true, isNameTattooable(10, "Chris"));
    Certainly.samesies(false, isNameTattooable(10, "Christopher"));
    Certainly.samesies(false, isNameTattooable(10, "Demosthenes"));
    Certainly.samesies(false, isNameTattooable(0, "Mom"));
    Certainly.samesies(true, isNameTattooable(0, ""));
  }

  public static boolean isSocialSecurityEligible(int age) {
    return age >= 62;
  }

  public static boolean isWalk(int numberOfBalls, boolean isHitByPitch) {
    return numberOfBalls == 4;
  }

  public static boolean isPolygamous(int nSpouses) {
    return nSpouses > 1;
  }

  public static boolean isLegalToDrive(double bac) {
    return bac < 0.08;
  }

  public static boolean isWrongNumber(double rightNumber, double guess) {
//    return rightNumber != guess;
    return Math.abs(rightNumber - guess) > 0.001;
  }

  public static boolean isNameTattooable(int nKnuckles, String name) {
    return nKnuckles >= name.length();
  }
}

Quarter.java

package lecture1004.cs148;

public class Quarter {
  public static boolean hasJustOne(String haystack, char needle) {
    int count = haystack.length() - haystack.replaceAll("" + needle, "").length();
    return count == 1;
  }

  public static boolean hasJustOneJustin(String haystack, char needle) {
    return haystack.indexOf(needle) == haystack.lastIndexOf(needle);
  }

  public static boolean hasJustOneVictor(String haystack, char needle) {
    int count = 0;
    for (int i = 0; i < haystack.length(); ++i) {
      if (haystack.charAt(i) == needle) {
        return true;
      }
    }

    return count == 1;
  }
}

Certainly.java

package lecture1004.cs148;

public class Certainly {
  public static void samesies(int expected, int actual, String message) {
    if (expected != actual) {
      throw new RuntimeException(String.format("%s%nExpected: %d%n  Actual: %d%n", message, expected, actual));
    }
  }

  public static void samesies(boolean expected, boolean actual, String message) {
    if (expected != actual) {
      throw new RuntimeException(String.format("%s%nExpected: %b%n  Actual: %b%n", message, expected, actual));
    }
  }
}

Booleanning.java

package lecture1004.cs148;

public class Booleanning {
  public static void main(String[] args) {
    Certainly.samesies(false, isSocialSecurityEligible(25), "");
    Certainly.samesies(true, isSocialSecurityEligible(65), "");
    Certainly.samesies(false, isSocialSecurityEligible(18), "");
    Certainly.samesies(true, isSocialSecurityEligible(300), "");
    Certainly.samesies(true, isSocialSecurityEligible(62), "");
    Certainly.samesies(false, isSocialSecurityEligible(61), "");

    Certainly.samesies(false, isWalk(0), "walk 0");
    Certainly.samesies(false, isWalk(1), "walk 1");
    Certainly.samesies(false, isWalk(2), "walk 2");
    Certainly.samesies(false, isWalk(3), "walk 3");
    Certainly.samesies(true, isWalk(4), "walk 4");

    Certainly.samesies(false, isPolygamous(0), "poly 0");
    Certainly.samesies(false, isPolygamous(1), "poly 1");
    Certainly.samesies(true, isPolygamous(2), "poly 2");
    Certainly.samesies(true, isPolygamous(17), "poly 17");

    Certainly.samesies(true, isLegalToDrive(0), "bac 0");
    Certainly.samesies(true, isLegalToDrive(0.079999), "bac 0.079999");
    Certainly.samesies(false, isLegalToDrive(0.08), "bac 0.08");
    Certainly.samesies(false, isLegalToDrive(0.09), "bac 0.09");

    Certainly.samesies(true, isTattooable(10, "Sam"), "sam");
    Certainly.samesies(false, isTattooable(10, "Bartholomew"), "Barthomolew");
    Certainly.samesies(false, isTattooable(10, "Demosthenes"), "Demosthenes");
  }

  public static boolean isSocialSecurityEligible(int age) {
    return age >= 62;
  }

  public static boolean isWalk(int nBalls) {
    return nBalls == 4;
  }

  public static boolean isPolygamous(int nPartners) {
    return nPartners >= 2;
  }

  public static boolean isLegalToDrive(double bac) {
    return bac < 0.08;
  }

  public static boolean isTattooable(int nKnuckles, String name) {
    int nCharacters = name.length();
    return nKnuckles >= nCharacters;
  }
}

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *