teaching machines

CS1: Lecture 34 – Reverse Engineering Objects

December 2, 2019 by . Filed under cs1, fall 2019, lectures.

Dear students,

In object-oriented programming, the programmer is really a writer of screenplays. Objects are our actors, which we orchestrate around the stage. We cue them to say certain things, have them interact with other objects, and shape them to have an identity that is all their own.

Today, we’ll feel our away around the identities of several objects through some blackboxes. Once we get an idea for what they do, we will implement them in Java.

Here’s your TODO to complete before we meet again:

See you next class!

Sincerely,

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

A coin has two sides
The outside and the inside
Both are bound to change

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

Main.java

package lecture1202.cs145;

public class Main {
  public static void main(String[] args) {
//    StarSpitter spitter = new StarSpitter();
//    for (int i = 0; i < 10; ++i) {
//      System.out.println(spitter.spit());
//    }

    NotSoFinalCountdown counter = new NotSoFinalCountdown(2);
    for (int i = 0; i < 5; ++i) {
      System.out.println(counter.tick());
    }
    counter.clear();
    for (int i = 0; i < 5; ++i) {
      System.out.println(counter.tick());
    }
  }
}

MinMax.java

package lecture1202.cs145;

public class MinMax {
  private double min;
  private double max;
  private int n;

  public MinMax() {
    n = 0;
  }

  public double getMinimum() {
    return min;
  }

  public double getMaximum() {
    return max;
  }

  public void include(double newNumber) {
    if (n == 0) {
      min = max = newNumber;
    } else {
      if (newNumber > max) {
        max = newNumber;
      } else if (newNumber < min) {
        min = newNumber;
      }
    }
    n++;
  }

  public void clear() {

  }
}

NotSoFinalCountdown.java

package lecture1202.cs145;

public class NotSoFinalCountdown {
  private int nClicks;
  private int target;

  public NotSoFinalCountdown(int target) {
    nClicks = 0;
    this.target = target;
  }

  public boolean tick() {
    nClicks++;
    return nClicks >= target;
  }

  public void clear() {
    nClicks = 0;
  }
}

StarSpitter.java

package lecture1202.cs145;

public class StarSpitter {
  private int nClicks;

  public StarSpitter() {
    nClicks = 0;
  }

  public String spit() {
    int nStars = nClicks % 2 + 1;
    nClicks++;
    return "*".repeat(nStars);
  }
}

Alterisk.java

package lecture1202.cs148;

public class Alterisk {
  private int n;
  private boolean isTwo;

  public Alterisk() {
    n = 0;
    isTwo = true;
  }

  public String asterisks() {
//    n++;
//    if (n % 2 == 1) {
//      return "*";
//    } else {
//      return "**";
//    }
    isTwo = !isTwo;
    return isTwo ? "**" : "*";

  }
}

Bounder.java

package lecture1202.cs148;

public class Bounder {
  private Double min;
  private Double max;

  public Bounder() {
    min = null;
    max = null;
  }

  public double getMinimum() {
    return min;
  }

  public double getMaximum() {
    return max;
  }

  public void reset() {
    min = null;
    max = null;
  }

  public void include(double x) {
    if (min == null || max == null) {
      min = max = x;
    } else {
      if (x < min) {
        min = x;
      } else if (x > max) {
        max = x;
      }
    }
  }
}

JackInTheBox.java

package lecture1202.cs148;

public class JackInTheBox {
  private int n;
  private int target;

  public JackInTheBox(int target) {
    this.target = target;
    n = target;
  }

  public boolean crank() {
    n--;
    return n < 1;
  }

  public void reset() {
    n = target;
  }
}

Main.java

package lecture1202.cs148;

public class Main {
  public static void main(String[] args) {
//    Alterisk alterisk = new Alterisk();
//    for (int i = 0; i < 6; i++) {
//      System.out.println(alterisk.asterisks());
//    }

    JackInTheBox jack = new JackInTheBox(3);
    for (int i = 0; i < 5; ++i) {
      System.out.println(jack.crank());
    }
    jack.reset();
    for (int i = 0; i < 5; ++i) {
      System.out.println(jack.crank());
    }
  }
}