teaching machines

CS 245 Lecture 6 – Inheritance

September 19, 2013 by . Filed under cs245, fall 2013, lectures.

Agenda

TODO

Inheritance

Interface inheritance: reuse method signatures so that generalized code can operate on our classes. This lets old code call new code.

Implementation inheritance: reuse method signatures and bodies so that we can extend existing classes. This lets new code build on old code. We also get interface inheritance.

Program This

Make an extension of JFrame that automatically sets its size to 512×512 and makes your application exit on close. Have its constructor accept a the window’s title.

Code

Story.java

package lecture0919;

public class Story {
  public static void main(String[] args) {
    Dad dad = new Dad();
    dad.eatBreakfast(new ToasterPlus());
  }
}

Dad.java

package lecture0919;

public class Dad {
  private int hitPoints;

  public void eatBreakfast(Toaster toaster) {
    Toast toast = toaster.toast();
    hitPoints += toast.getCalorieCount();

    // toaster.getTime(); ILL EAGLE
  }
}

Toast.java

package lecture0919;

public class Toast {
  public int getCalorieCount() {
    return 155;
  }
}

Toaster.java

package lecture0919;

public class Toaster {

  public Toast toast() {
    try {
      Thread.sleep(3000);
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
    
    return new Toast();
  }

}

ToasterPlus.java

package lecture0919;

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

public class ToasterPlus extends Toaster {

  public Toast toast() {

    // toast like normal
    Toast toast = super.toast();

    for (int i = 0; i < 10; ++i) {
      Toolkit.getDefaultToolkit().beep();
      System.out.println("\007");
      try {
        Process p = Runtime.getRuntime().exec("mplayer file:///usr/share/aisleriot/sounds/splat.ogg");
        p.waitFor();
      } catch (IOException e) {
      } catch (InterruptedException e) {
      }
    }
    
    System.out.println("Your toast was done at " + getTime());
    
    return toast;
  }

  public Date getTime() {
    return new Date();
  }
}

RandomWithRange.java

package lecture0919;

import java.util.Arrays;
import java.util.Random;

public class RandomWithRange extends Random {

  public int nextInt(int lower, int upper) {
    return nextInt(upper - lower + 1) + lower;
  }
  
  public static void main(String[] args) {
    RandomWithRange generator = new RandomWithRange();
    int[] counts = new int[3];
    
    for (int i = 0; i < 1000000000; ++i) {
      int r = generator.nextInt(-1, 1);
      ++counts[r + 1];
    }
    
    System.out.println(Arrays.toString(counts));
  }
}

WinnerWinnerChickenDinner.java

package lecture0919;

public class WinnerWinnerChickenDinner {
  public static void main(String[] args) {
    Raffle raffle = new Raffle();
    
    raffle.add("Jamison");
    raffle.add("Justin");
    raffle.add("Noah");
    raffle.add("Ben");
    raffle.add("Alan");
    raffle.add("Brad");
    raffle.add("Me");
    
    String winner = raffle.drawRandom();
    System.out.println(winner);
  }
}

Raffle.java

package lecture0919;

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

public class Raffle extends ArrayList<String> {

  @Override
  public boolean add(String name) {
    if (!name.startsWith("Jill")) {
      super.add(name);
    }
    
    return true;
  }
  
  public String drawRandom() {
    
    Random generator = new Random();
    int randomIndex = generator.nextInt(size());
    
    String name = get(randomIndex);
    
    remove(randomIndex);
    
    return name;
  }
}

Haiku

“Give me your money!”
“You sure? It’s Canadian.”
“Super. Nevermind.”