University of Toronto - Summer 2001
Department of Computer Science

Week 3 - Memory Model Review

Java Program

public class Rink {
   public static void main (String[] args) {
      Player wayne = new Player (99, "Oilers");
      Coach  smith = new Coach  ("Smith", 44);
      wayne.setCoach (smith);

      System.out.println ("Print Wayne's Coach…");
      wayne.printCoachName();

      System.out.println ("Inc Coach's age…");
      wayne.incCoachAge(5);

      System.out.println ("Summary Info for Wayne…");
      System.out.println (wayne.getInfo());
   }
}

class Player {
   private int jersey;
   private String team;
   private Coach coach;

   public Player (int jersey, String team) {
      this.jersey = jersey;
      this.team = team;
   }

   public void setCoach (Coach coach) {
      this.coach = coach;
   }

   public void printCoachName () {
      System.out.println ("Coach" + " is " + coach.getName());
   }

   public void incCoachAge (int years){
      this.coach.incAge (years);
   }

   public String getInfo() {
      return "Player's Number: " + this.jersey + "\n" +
          "Player's Team: " + this.team + "\n" +
          this.coach.getInfo();
   }
}

class Coach {
   private String name;
   private int age;

   public Coach (String name, int age){
      this.name = name;
      this.age = age;
   }

   public String getName() {
      return this.name;
   }

   public void incAge (int years) {
      this.age += years;
   }

   public String getInfo() {
      return "Coach's Name: " + this.name + "\n" +
         "         Age: " + this.age + "\n";
    }
}

Memory Model

Show what happens in memory...


Program Output

Print Wayne's Coach...
Coach is Smith
Inc Coach's age...
Summary Info for Wayne...
Player's Number: 99
Player's Team: Oilers
Coach's Name: Smith
         Age: 49