teaching machines

CS 1: Lecture 31 – Reverse Engineering Objects

November 27, 2017 by . Filed under cs1, cs145, cs148, fall 2017, 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!

Objects are like cats
They remember things real well
What you did to them

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

Astericky.java

package lecture1127;

public class Astericky {
  private boolean isOdd;

  public Astericky() {
    isOdd = false;
  }

  public String poke() {
    isOdd = !isOdd;
    if (isOdd) {
      return "*";
    } else {
      return "**";
    }
  }
}

Hugs.java

package lecture1127;

public class Hugs {
  private final int nEnd;
  private int nMatches;
  
  public Hugs(int nMatches) {
    nEnd = nMatches;
    this.nMatches = 0;
  }
  
  public boolean tick() {
    nMatches++;
    return nMatches >= nEnd;
  }
  
  public void reset() {
    nMatches = 0;
  }
}

Raffle.java

package lecture1127;

import java.util.ArrayList;
import java.util.Random;

public class Raffle {
  private ArrayList<String> names;
  private Random generator;
  
  public Raffle() {
    names = new ArrayList<String>();
    generator = new Random();
  }

  public void add(String name) {
    if (!name.equals("Jill")) {
      names.add(name);
    }
  }

  public String draw() {
    int i = generator.nextInt(names.size());
    return names.remove(i);
  }
}

Blackboxes.java

package lecture1127;

public class Blackboxes {
  public static void main(String[] args) {
//    Astericky ricky = new Astericky();
//    System.out.println(ricky.poke());
//    System.out.println(ricky.poke());
//    System.out.println(ricky.poke());
//    System.out.println(ricky.poke());
//    System.out.println(ricky.poke());
//    System.out.println(ricky.poke());
//    System.out.println(ricky.poke());
//    System.out.println(ricky.poke());
//    System.out.println(ricky.poke());
//    System.out.println(ricky.poke());
//    System.out.println(ricky.poke());
//    System.out.println(ricky.poke());
//    System.out.println(ricky.poke());
//    System.out.println(ricky.poke());
//    System.out.println(ricky.poke());
//    System.out.println(ricky.poke());
//    System.out.println(ricky.poke());
//    System.out.println(ricky.poke());
//    System.out.println(ricky.poke());
//    System.out.println(ricky.poke());
//    System.out.println(ricky.poke());
//    System.out.println(ricky.poke());
//    System.out.println(ricky.poke());
//    Hugs hugs = new Hugs(3);
//    System.out.println(hugs.tick());
//    System.out.println(hugs.tick());
//    System.out.println(hugs.tick());
//    System.out.println(hugs.tick());
//    System.out.println(hugs.tick());
//    hugs.reset();
//    System.out.println(hugs.tick());
//    System.out.println(hugs.tick());
//    System.out.println(hugs.tick());
//    System.out.println(hugs.tick());
//    System.out.println(hugs.tick());

    Raffle raffle = new Raffle();
//    raffle.add("Rick O'Shea");
    for (int i = 0; i < 100; ++i) {
      raffle.add("Jill");
    }
    System.out.println(raffle.draw());
  }
}

DoubleClick.java

package lecture1127;

public class DoubleClick {
  private boolean wasTwo;

  public DoubleClick() {
    wasTwo = false;
  }

  public String getStars() {
    wasTwo = !wasTwo;
    if (wasTwo) {
      return "*";
    } else {
      return "**";
    }
  }
}

Clickdown.java

package lecture1127;

public class Clickdown {
  private final int nClicks;
  private int remainingClicks; 
  
  public Clickdown(int nClicks) {
    // instance var = parameter
    this.nClicks = nClicks;
    reset();
  }
  
  public boolean click() {
    remainingClicks--;
    return remainingClicks <= 0;
  }
  
  public void reset() {
    remainingClicks = nClicks;
  }
}

Main.java

package lecture1127;

public class Main {
  public static void main(String[] args) {
//    DoubleClick clickee = new DoubleClick();
//    System.out.println(clickee.getStars());
//    System.out.println(clickee.getStars());
//    System.out.println(clickee.getStars());
//    System.out.println(clickee.getStars());
//    System.out.println(clickee.getStars());
//    System.out.println(clickee.getStars());
//    System.out.println(clickee.getStars());
//    System.out.println(clickee.getStars());
//    System.out.println(clickee.getStars());
//    System.out.println(clickee.getStars());
    Clickdown ticker = new Clickdown(4);
    System.out.println(ticker.click());
    System.out.println(ticker.click());
    System.out.println(ticker.click());
    System.out.println(ticker.click());

    System.out.println(ticker.click());
    System.out.println(ticker.click());
    System.out.println(ticker.click());
    System.out.println(ticker.click());
    
    ticker.reset();
    
    System.out.println(ticker.click());
    System.out.println(ticker.click());
    System.out.println(ticker.click());
    System.out.println(ticker.click());

    System.out.println(ticker.click());
    System.out.println(ticker.click());
    System.out.println(ticker.click());
    System.out.println(ticker.click());

  }
}