teaching machines

CS 145 Lecture 13 – Relational Operators

October 5, 2016 by . Filed under cs145, fall 2016, lectures.

Dear students,

Based on your quarter sheets on the frankenstring problem, I think we need to take a moment to recommend a thinking strategy when someone asks you to write a method. Here’s how to tackle such a problem:

  1. Listen to problem description just enough to determine what kind of data your method is supposed to give back. This locks in your method’s return type.
  2. Determine if any data is being sent to your method through parameters. If so, declare formal parameters in the parameter list. Recall that declarations always follow this format: type name.
  3. Only when the return type and parameters are in place do you write the statements that make the method perform its intended task. Do not create a Scanner to try to assign the parameters’ values. The parameters already have values from the caller.

The intent here is just pick off small portions of your task at a time. You are such a smart human that your brain is instantly all over the problem. But when your brain is all over, it’s all over. Get some constraints in place first.

There will be several problems like this on the exam and many more as we continue in this class and in this discipline, so let’s practice at one:

Write a method getQuotientRemainder that accepts a numerator and a denominator, both ints. Return the quotient separated from the remainder separated by the character 'R'. For example, getQuotientRemainder(13, 5) yields "2R3".

Next up we discuss some new operators: <, <=, >, >=, ==, and !=. These are called the relational operators. (== and != are sometimes distinguished as equality operators.) We’ll introduce these by writing some methods that serve as “magic 2 balls”, 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"?

See you next class!

Sincerely,

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

Certainly.java

package lecture1005;

public class Certainly {
  public static void assertEquals(String message, int expected, int actual) {
    System.out.printf("%d == %d <- %s%n", expected, actual, message);
  }
  
  public static void assertEquals(String message, boolean expected, boolean actual) {
    System.out.printf("%b == %b <- %s%n", expected, actual, message);
  }
}

Predicates.java

package lecture1005;

public class Predicates {
  public static void main(String[] args) {
    Certainly.assertEquals("Young SS?", false, isEligibleForSocialSecurity(18));
    Certainly.assertEquals("Old SS?", true, isEligibleForSocialSecurity(70 + 100));

    Certainly.assertEquals("Walk on 0 balls?", false, isWalk(0));
    Certainly.assertEquals("Walk on 4 balls?", true, isWalk(4));

    Certainly.assertEquals("Single polygamous?", false, isPolygamous(0));
    Certainly.assertEquals("Monogamous polygamous?", false, isPolygamous(1));
    Certainly.assertEquals("Polygamous polygamous?", true, isPolygamous(2));
    Certainly.assertEquals("Solomon polygamous?", true, isPolygamous(2001));

    Certainly.assertEquals("Abstainer legal?", true, isLegalToDrive(0.0));
    Certainly.assertEquals("Lit legal?", false, isLegalToDrive(10));

    Certainly.assertEquals("Random guesser?", true, isWrongGuess(20, 46));
    Certainly.assertEquals("Jeopardy contestant?", false, isWrongGuess(46, 46));

    Certainly.assertEquals("Demosthenes?", false, isTattooable("Demosthenes", 10));
    Certainly.assertEquals("Garfield?", true, isTattooable("Garfield", 8));
    Certainly.assertEquals("Chris?", true, isTattooable("Chris", 28));
  }

  public static boolean isEligibleForSocialSecurity(int ageInYears) {
    // boolean isEligible = ageInYears >= 62;
    // return isEligible;

    return ageInYears >= 62;
  }

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

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

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

  public static boolean isIllegalToDrive(double bac) {
    return bac >= 0.08;
  }

  public static boolean isWrongGuess(int guess, int answer) {
    return guess != answer;
  }

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