teaching machines

CS 145 Lecture 23 – Composing with objects

December 5, 2011 by . Filed under cs145, fall 2011, lectures.

Agenda

Code

Stopwatch.java

package lecture;

public class Stopwatch {
  /** Time at which stopwatch is started */
  private long startTime;

  /** Time at which stopwatch is stopped */
  private long stopTime;

  /**
   * Starts stopwatch.
   */
  public void start() {
    startTime = System.currentTimeMillis();
  }

  /**
   * Stops stopwatch.
   */
  public void stop() {
    stopTime = System.currentTimeMillis();
  }

  /**
   * Gets the number of seconds elapsed between start and stop.
   * 
   * @return Number of elapsed seconds.
   */
  public double getElapsedSeconds() {
    double difference = (stopTime - startTime) / 1000.0;
    return difference;
  }
  
  public double getElapsedSecondsInSitu() {
    double difference = (System.currentTimeMillis() - startTime) / 1000.0;
    return difference;
  }
}

Flashlight.java

package lecture;

public class Flashlight {
  private boolean isOn;
  private double charge;
  private Stopwatch timer;

  public Flashlight() {
    isOn = false;
    charge = 100.0;
  }

  public void turnOn() {
    if (getCharge() > 0) {
      isOn = true;
      timer = new Stopwatch();
      timer.start();
    }
  }

  public void turnOff() {
    timer.stop();
    double elapsed = timer.getElapsedSeconds();
    charge -= elapsed * 2;
    isOn = false;
  }

  public double getCharge() {
    if (isOn) {
      double elapsed = timer.getElapsedSecondsInSitu();
      return charge - elapsed * 2;
    } else {
      return charge;
    }
  }

  public boolean isOn() {
    return isOn && getCharge() > 0;
  }

  public static void main(String[] args) throws InterruptedException {
    Flashlight flashy = new Flashlight();
    flashy.turnOn();
    Thread.sleep(1000);
    System.out.println(flashy.getCharge());
    Thread.sleep(1000);
    flashy.turnOff();
    System.out.println(flashy.getCharge());
  }
}

Player.java

package lecture;

public class Player {
  private String name;
  private boolean isFemale;
  
  public Player(String name,
                boolean isFemale) {
    this.name = name;
    this.isFemale = isFemale;
  }

  public String getName() {
    return name;
  }

  public boolean isFemale() {
    return isFemale;
  }
}

Room.java

package lecture;

import java.util.Scanner;

public class Room {
  public static void main(String[] args) {
    Player player = new Player("Philip", true);
    Scanner in = new Scanner(System.in);
    Flashlight torch = null;
    boolean hasExited = false;

    while (!hasExited) {
      System.out.print("> ");

      String line = in.nextLine();
      Scanner lineParser = new Scanner(line);
      
      String verb = "";
      if (lineParser.hasNext()) {
        verb = lineParser.next();
      }

      boolean handled = false;

      if (verb.equals("look")) {
        if (torch == null || !torch.isOn()) {
          System.out.println("You see blackness.");
        } else {
          System.out.println("The room is nondescript. But there's a clown smoking in the corner.");
          System.out.println("And a ladder going up.");
        }
        handled = true;
      }

      else if (verb.equals("grope")) {
        System.out.println("You found a flashlight and one angry clown.");
        torch = new Flashlight();
        handled = true;
      }

      else if (verb.equals("turn")) {
        String directObject = lineParser.next();
        if (directObject.equals("flashlight") && torch != null) {
          String state = lineParser.next();
          if (state.equals("on")) {
            if (!torch.isOn()) {
              torch.turnOn();
            } else {
              System.out.println("It's already on.");
            }
            handled = true;
          } else if (state.equals("off")) {
            if (torch.isOn()) {
              torch.turnOff();
            } else {
              System.out.println("It's already off.");
            }
            handled = true;
          }
        } else {
          System.out.println("You don't have a " + directObject + ".");
          handled = true;
        }
      }

      else if (verb.equals("climb")) {
        if (torch == null || !torch.isOn()) {
          System.out.println("Climbing in the dark is dangerous.");
        } else {
          String directObject = lineParser.next();
          if (directObject.equals("ladder")) {
            hasExited = true;
          } else {
            System.out.println(directObject + " can't be climbed.");
          }
        }
        handled = true;
      }

      else if (verb.equals("check")) {
        if (lineParser.hasNext() && lineParser.next().equals("flashlight") && torch != null) {
          System.out.println("The flashlight is at " + torch.getCharge() + "% power.");
          if (torch.isOn()) {
            System.out.println("It is on.");
          } else {
            System.out.println("It is off.");
          }
        } else {
          System.out.println("I don't know how to check that");
        }
        handled = true;
      }

      if (!handled) {
        System.out.println("I don't understand that.");
      }
    }
  }
}

TODO

Haiku

we’re of class Human
but our statics are so few
you say toe-MAH-toe