Courses‎ > ‎AP Computer Science‎ > ‎Konstantinovich‎ > ‎notes‎ > ‎

2017-10-19 HW

posted Oct 19, 2017, 4:16 PM by Konstantinovich Samuel   [ updated Oct 21, 2017, 6:45 PM ]
Several takeaways from yesterday:

1. Abstract classes cannot be instantiated.

2. Abstract methods are placeholders for future implementation.

3. Abstract methods require that the class is abstract.

4. Method calls always use the most recently overidden method.  (This isn't specific to abstract!)
 e.g.
 toString() calls info(), and shows the subclass description.

5. Concrete methods can call abstract methods. This means that the concrete method must be in an abstract class.
 e.g.
 getName() calls the abstract title() method.


abstract class Player{
  private int health;
  private String name;

  /**constructor*/
  public Player(int hp, String n){
   health = hp;
   name = n;
  }

  public int getHealth(){
   return health;
  }

  public String getName(){
   return title() + name;
  }

  public abstract String title();

  public String toString(){
    return getName() +" ("+ getHealth()+ ")\n" + info();
  }

  public abstract void attack(Player other);

  public String info(){
    return "Error - lazy programmer detected (write a proper description)";
  }
}




public class Mage extends Player{
    //You automatically inherit the accessors yay free methods.
    /**constructor*/
    public Mage(int hp, String n){
     super(hp,n);
    }
    //You MUST implement attack in order for this class to compile.
    public void attack(Player other){
      //Do something
    }

    //You MAY choose to override title + info
    public String title(){
      return "Grand Sage ";

    }

    public String info(){
     return "A manipulator of the cosmos and creator of explosions.";
    }
}
 
public class Barbarian extends Player{
    //You automatically inherit the accessors yay free methods.
    /**constructor*/
    public Barbarian(int hp, String n){
     super(hp,n);
    }
    //You MUST implement attack in order for this class to compile.
    public void attack(Player other){
      //Do something
    }
    //You MAY choose to override title + info
    public String title(){
      return "Destroyer ";
    }

    public String info(){
     return "A warrior that fights with brute strength and pure rage.";
    }
}

public class Driver{
  public static void main(String[] args){
    Barbarian x = new Barbarian(24,"Gronk");
    Mage y = new Mage(24,"Tim");
    Player a = new Barbarian(20,"Conan");
    Player b = new Mage(15,"Merlin");

    System.out.println(x.getName());
    System.out.println(y.getName());
    System.out.println(a.getName());
    System.out.println(b.getName());
    System.out.println("--------------");
    System.out.println(x);
    System.out.println(y);
    System.out.println(a);
    System.out.println(b);

  }

}

Output:
Destroyer Gronk (24)
A warrior that fights with brute strength and pure rage.
Grand Sage Tim (24)
A manipulator of the cosmos and creator of explosions.
Destroyer Conan (20)
A warrior that fights with brute strength and pure rage.
Grand Sage Merlin (15)
A manipulator of the cosmos and creator of explosions.
--------------
Destroyer Gronk
Grand Sage Tim
Destroyer Conan
Grand Sage Merlin




HW: <UPDATE LABEL: HW was mentioned in class (hopefully both periods >
Complete the following publically available AP question. I omitted part C because it contains a class you haven't seen yet!
You must get used to the idea that you can use classes without knowing all of the details. Complete this on paper without discussion. After we are all done we will go over this.








Comments