CS 1: Lecture 31 – Reverse Engineering Objects
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:
- CS 145, lab 11 is posted. Feel free to start early.
- We will have a peer review for homework 5 in lab this week. Some of you who received amnesty credit for homework 5, I accidentally gave you review credit as well. You do not actually have that and will need to attend a review session to claim your Blugolds.
- Homeworks 6 and 7 were posted before break. You’ll need to Team / Pull… from the template repository to get their SpecCheckers. No amnesty period will be available for these homeworks. My advice: start immediately. They, like homework 4, are full homeworks—each worth 10 Blugolds.
- Read chapter 8. CS 145, I recommend you do this before lab tomorrow. On a quarter sheet, outline an object in Java code to represent some entity of your choosing. Declare its state/instance variables. List at least three behaviors/methods—include parameters, but leave the implementation (between the curly braces) empty.
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());
}
}