teaching machines

CS 245 Lecture 5 – Inheritance

February 4, 2014 by . Filed under cs245, lectures, spring 2014.

Agenda

TODO

Program This

Assume you have a Planet class:

public class Planet {
  private String name;
  private double ausFromTheSun;
  private double relativeMass;

  public Planet(String name,
                double ausFromTheSun,
                double relativeMass) {
    this.name = name;
    this.ausFromTheSun = ausFromTheSun;
    this.relativeMass = relativeMass;
  }

  public String toString() {
    return String.format("%15s %6.2f %6.2f", name, ausFromTheSun, relativeMass);
  }
}

What changes do you need to make in order for Planet to be Comparable? In particular, a planet should be considered greater than another if it has a larger relative mass.

public interface Comparable<T> {
  // Returns
  //   negative if this < other,
  //   0 if this equals other, and
  //   positive if this > other.
  public int compareTo(T other);
}

Once Planet is Comparable, some high-level conductor code in Arrays.sort can automatically sort an array for us.

Inheritance

With interfaces, we get interface inheritance, in which:

Through extension, we get implementation inheritance, in which

Code

Planet.java

package lecture05;

public class Planet implements Comparable<Planet> {
  private String name;
  private double ausFromTheSun;
  private double relativeMass;

  public Planet(String name,
                double ausFromTheSun,
                double relativeMass) {
    this.name = name;
    this.ausFromTheSun = ausFromTheSun;
    this.relativeMass = relativeMass;
  }

  public String toString() {
    return String.format("%15s %6.2f %6.2f", name, ausFromTheSun, relativeMass);
  }

  public int compareTo(Planet other) {
    if (Math.abs(relativeMass - other.relativeMass) < 0.001) {
      return 0;
    } else if (relativeMass < other.relativeMass) {
      return -1;
    } else {
      return 1;
    }
  }
}

PlanetSorter.java

package lecture05;

import java.util.Arrays;

public class PlanetSorter {
  public static void main(String[] args) {
    Comparable[] planets = {
      new Planet("Saturn", 9.5, 95.0),
      new Planet("Earth", 1.0, 1.0),
      new Planet("Venus", 0.7, 0.815),
      new Planet("Jupiter", 5.2, 318.0),
      new Planet("Uranus", 19.6, 14.0),
      new Planet("Neptune", 30.0, 17.0),
      new Planet("Mars", 1.5, 0.107),
      new Planet("Mercury", 0.4, 0.055),
//      "obtuse"
    };
    
    Arrays.sort(planets);
    
    for (Comparable p : planets) {
      System.out.println(p);
    }
  }
}

Bread.java

package lecture05;

public class Bread {

}

Toast.java

package lecture05;

public class Toast {
  public int getCalories() {
    return 100 * 2;
  }
}

Toaster.java

package lecture05;

public class Toaster {
  public Toast toast(Bread bread) {
    return new Toast();
  }
}

Dad.java

package lecture05;

public class Dad {
  private int hitPoints;
  
  public Dad() {
    hitPoints = 100;
  }
  
  public void eatBreakfast(Toaster toaster) {
    Toast toast = toaster.toast(new Bread());
    hitPoints += toast.getCalories();
    ToasterPlus toasterPlus = (ToasterPlus) toaster;
    System.out.println(toasterPlus.getTime());
  }
  
  public int getHitPoints() {
    return hitPoints;
  }
}

ToasterPlus.java

package lecture05;

import java.awt.Toolkit;
import java.util.Date;

public class ToasterPlus extends Toaster {
  public Date getTime() {
    return new Date();
  }

  // We don't need this here, because we are extending Toaster.
  public Toast toast(Bread bread) {
    Toast toast = super.toast(bread);
    for (int i = 0; i < 100; ++i) {
      Toolkit.getDefaultToolkit().beep();
      try {
        Thread.sleep(100);
      } catch (InterruptedException e) {
      }
    }
    return toast;
  }
}

Story.java

package lecture05;

public class Story {
  public static void main(String[] args) {
    Dad lowell = new Dad();
    Toaster toaster = new ToasterPlus();
    
    System.out.println(lowell.getHitPoints());
    lowell.eatBreakfast(toaster);
    System.out.println(lowell.getHitPoints());
  }
}

RandomPlus.java

package lecture05;

import java.util.Random;

public class RandomPlus extends Random {
  public int nextIntInRange(int lo, int hi) {
    return lo + nextInt(hi - lo);
  }
  
  public static void main(String[] args) {
    RandomPlus g = new RandomPlus();
    for (int i = 0; i < 1000; ++i) {
      System.out.println(g.nextIntInRange(1, 4));
    }
  }
}

Raffle.java

package lecture05;

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

public class Raffle extends ArrayList<String> {
  
  @Override
  public boolean add(String name) {
    if (name.equals("Jill")) {
      // whoops
    } else {
      super.add(name);
    }
    
    return true;
  }
  
  public String drawName() {
    Random g = new Random();
    int i = g.nextInt(size());
    return this.remove(i);
  }
  
  public static void main(String[] args) {
    Raffle raffle = new Raffle();
    raffle.add("Jill");
    raffle.add("Alex G");
    raffle.add("Nick L");
    raffle.add("Jeff J");
    raffle.add("Andrew P");
    raffle.add("Nick D");
    raffle.add("Jeremy R");
    raffle.add("Trent S");
    raffle.add("Robert F");
    
    System.out.println(raffle.drawName());
    System.out.println(raffle.drawName());
    System.out.println(raffle.drawName());
    System.out.println(raffle.drawName());
    System.out.println(raffle.drawName());
  }
}

MyFrame.java

package lecture05;

import javax.swing.JButton;
import javax.swing.JFrame;

public class MyFrame extends JFrame {
  public MyFrame() {
    super("my title");
//    super();
    
    JButton button = new JButton("Exit");
    add(button);
    
//    button.addActionListener();
    
    setSize(1024, 512);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setVisible(true);
  }
  
  public static void main(String[] args) {
    MyFrame myFrame = new MyFrame();
  }
}

Haiku

Dad: Where’s my Camry?
Jill: Out back. I “serviced” it.
Dad: The space shuttle!?