StarWars.java Answers

Click here to go back to the questions
// ANSWERS:
//
// (1) Compile and run this file to see the battle between the
// X-Wings and the Tie Fighters unfold.
//
// (2) If you look at the Java code for the XWing and
// TieFighter classes you will notice that they are almost
// identical: They have the same methods and properties, the
// only difference is that the XWing objects are initialised
// with a different value for their shields and weapon
// properties to the TieFighter objects.

// The next few questions will guide you through the process
// of using inheritance to eliminate this unnecessary
// duplication of code.  A new class called "SpaceShip" will
// be created and all of the code that is common to XWing
// and TieFighter will be moved into this class.  The XWing
// and TieFighter classes will then be modified so that they
// both inherit from SpaceShip.

// (3) The first step in this process is to create the outer
// shell of the SpaceShip class, which you should now
// type in:

//  class SpaceShip {
//  }

// (4) Move the properties "shields", "weapon" and "dead"
// out of the XWing and TieFighter classes and into the
// SpaceShip class.  You must change the privacy status of
// the properties from private to protected.

// The protected modifier was invented as an intermediate
// level of privacy between public and private.  Like
// private, it allows visibility to the same class in which
// the method or property was defined, but unlike private it
// also allows visibility to subclasses of the class in
// which the method or property was defined.

// (5) Move the three methods getWeapon, isDead and hit out
// of the XWing and TieFighter classses and into the
// SpaceShip class.  At this point, the XWing and TieFighter
// classes should contain nothing but a constructor.
// 

class SpaceShip {

   // Properties of the class...
   protected int     shields;
   protected int     weapon;
   protected boolean dead;

   // Methods of the class...
   public int getWeapon() {
      return weapon;
   }
   public boolean isDead() {
      return dead;
   }
   public void hit(int damage) {
      shields = shields - damage;
      if (shields < 0) {
         System.out.println("BOOM!!!");
         dead = true;
      }
   }
}

// 
// (6) Finally, add the extends keyword to the first line of
// the XWing and TieFighter classes:

//  class XWing extends SpaceShip {
//  ...
//  class TieFighter extends SpaceShip {
// 

class XWing extends SpaceShip {

   // Constructor of the class...
   public XWing() {
      shields = 1000;
      weapon  = 10;
   }

}

class Tie extends SpaceShip {

   // Constructor of the class...
   public Tie() {
      shields = 500;
      weapon  = 20;
   }
  
}

// 
// (7) Compile and run your program again, making sure
// that it produces the same results now that it is using
// inheritance.

// (8) The SpaceShip class is a superclass of both XWing and
// TieFighter containing everything that X-Wings and Tie
// Fighters contain in common.  Because the role of the
// SpaceShip class is simply to hold these commonalities, we
// might choose to label the class with the abstract
// keyword:

//  abstract class SpaceShip {

// This prevents us from creating instances of the SpaceShip
// class.  Without the abstract modifier, we could happily
// create a new SpaceShip(), which would be an object that
// is not an X-Wing, nor a Tie Fighter, but just a vague
// "space ship".  If we consider this to be a logical
// mistake then we can use abstract to prevent such calls to
// the SpaceShip constructor.

// Change the class SpaceShip to be abstract and observe how
// the compiler will not accept any lines of the form:

//  SpaceShip s = new SpaceShip();

// Remove the abstract keyword and notice how the compiler
// will then allow this line to compile.

Hosted by www.Geocities.ws

1