Your last name (BLOCK CAPITALS): _______________ first name:__________

Section ID______

 

CSE143

Miniquiz #04 - Inheritance, July 3, 2003

12 minutes, 10 points

 

1. Draw the class hierarchy (i.e. the class diagrams) for the code given on the following page [5 pts] :

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

2. Using the same code, draw the object diagram after the following statements have been executed [5 pts]:

 

Performer p;

Actor john = new Actor("John Cusack", 37);

Guitarist jimi = new Guitarist("Jimi Hendrix", 27);

p = john;

p = jimi;

 

 



public abstract class Performer {

   protected String name;

   protected int age;

   protected int skillLevel;

 

   public Performer(String name, int age) {

      this.age = age;

      this.name = name;

      skillLevel = 0;

   }

  

   public abstract void perform();

 

   public abstract void rehearse(int hours);

}

 

public abstract class Musician extends Performer {

   protected String instrument;

 

   public Musician(String name, int age, String instrument) {

      super(name, age);

      this.instrument = instrument;

   }

 

   public void perform() {

     playMusic();

   }

  

   public abstract void playMusic();

 

}

 

public class Guitarist extends Musician {

   public Guitarist(String name, int age) {

      super(name, age, "Guitar");

   }

 

   public void rehearse(int hours) {

      skillLevel += hours/4;

   }

 

   public void playMusic() {

      if (skillLevel > 100) {

         System.out.println("Wow, that was some sweet music you're playing...");

      } else {

         System.out.println("Dude, that was awful...");

      }

   }

}


public class Actor extends Performer {

 

  public Actor(String name, int age) {

    super(name, age);

  }

 

   public void rehearse(int hours) {

      skillLevel += hours/10;

   }

 

   public void perform() {

     act();

   }

  

   private void act(){

      if (skillLevel > 100) {

         System.out.println("Wow, your performance was so moving...");

      } else {

         System.out.println("You need to rehearse more...");

      }

   }     

 

}