Java

OOP GUIDE / WORK

 

RANGEDWEAPON CLASS

 

Topics

  • Inheritance
  • Using protected instead of private
  • Overriding methods
  • Object arrays

 

 

TASK – PART 1 – CONSIDER THE WEAPON CLASS

 

Start by copying the following Weapon class to your IDE.  Note that there is another exercise where you can create this class if you are interested in doing so.

 

 

public class Weapon

{

     private String name;

     private int lowDamage;

     private int highDamage;

     private int damageBonus;

   

     public Weapon(String name, int ld, int hd, int db)

     {

         this.name = name;

         lowDamage = ld;

         highDamage = hd;

         damageBonus = db;

     }

 

     public String getName()

     {

           return name;

     }

 

     public int getLowDamage()

     {

           return lowDamage;

     }

   

     public int getHighDamage()

     {

           return highDamage;

     }

   

     public int getDamageBonus()

     {

           return damageBonus;

     }

   

     public int getDamageAmount()

     {

           int dif = highDamage - lowDamage;

           int dmg = lowDamage + (int)(Math.random() * (dif + 1)) + damageBonus;

           return dmg;

     }

 

     public String toString()

     {

           return getDescriptiveName() + "(" + lowDamage + "-" + highDamage + ") + " + damageBonus;

     }

 

     public void enchantWeapon()

     {

           damageBonus++;

     }

    

     public void curseWeapon()

     {

        damageBonus--;

     }

    

     public String getDescriptiveName()

     {

     if (damageBonus <= -2)

            return "Cursed " + name;

     else if (damageBonus >= 2)

            return name + " of Power";

     else

            return name;

     }

}

 

 

 

TASK – PART 2 – PROBLEM WITH THE WEAPON CLASS

 

Our Weapon class works well for almost any kind of video game weapon with one exception – ranged weapons such as Bows and Slingshots.  Ranged weapons do damage that is based on the weapon itself as well as on the projectile that it will use.

For example, a bow with an arrow will do less damage than a bow with a silver arrow!  (At least, this is what I’ve learned from Zelda!)

 

So we want to create a RangedWeapon class that will use Projectile class objects.  Let’s start with the Projectile class.

 

 

TASK – PART 3 – A PROJECTILE CLASS

 

You will create a Projectile class with the following specifications:

 

  • It has two private instance variables
    • String name (to contain the projectile name such as “arrow”)
    • int damageIncrease (the amount of damage the projectile adds to the base weapon’s damage)

  • It has a constructor with two parameters, one for each instance variable.

  • It has the following instance methods:

o  A get method for name.

o  A get method for damageIncrease.

o  A toString method of the form Arrow(+1) where Arrow is the name and 1 is the value of damageIncrease.

 

Note: To keep things simple, Projectiles will not have a descriptive name.  So a Silver Arrow that does +3 damage increase is still just named a silver arrow.

 

 

TASK – PART 4 – A RANGEDWEAPON CLASS (USING INHERITANCE)

 

You will now create a RangedWeapon class with the following specifications:

 

  • It will extend the Weapon class.
    • It will need to have access to the Weapon class’ instance variables so you need to change all of them from private to protected.

  • It contains only one instance variable (instance object):
    • Projectile proj (the projectile that the ranged weapon will be using)

  • It has a constructor with 5 parameters, four of which are to be passed to the super constructor and one for the projectile.

  • It has another constructor with 6 parameters, four of which are to be passed to the super constructor.  The other two are a String and a damageIncrease value that can be used to construct our own Projectile object.

  • It has the following instance methods:

    • getProjectileName (returns the name of the projectile)

    • getProjectileDamageIncrease (return the projectile’s damage increase)

    • setProjectile(Projectile p) (sets a new type of projectile)

    • setProjectile(String pname, int pdi) (creates a Projectile object and sets it as the instance variable)

  • It also has the following methods that override methods from Weapon (use @Override):

    • getName (returns a combination of the Weapon’s name and Projectile name (Example: Bow & Arrow)

    • getDescriptiveName (returns the Weapon’s descriptive name and the Projectiles’ name (Example: Bow of Power & Arrow)

    • getLowDamage (returns the Weapon’s lowDamage added to the Projectile’s damage increase)

    • getHighDamage (returns the Weapon’s highDamage added to the Projectile’s damage increase.

    • getDamageAmount (returns a random value between getLowDamage() and getHighDamage() added up with the damageBonus of the Weapon.

    • toString (returns a String such as Sling of Power & Stone (4-6) + 2).

 

 

TASK – PART 5 – TESTING OUR CLASSES

 

In a tester class, create the following objects:

 

  • Create a sword weapon that does 3-5 damage (zero bonus damage).
  • Output the sword to screen.
  • Enchant the sword weapon twice.
  • Output the sword to screen.

  • Create a dart projectile that does +1 damage increase.
  • Output the dart to screen.
  • Create a seed projectile that does 0 damage increase.  (Only to be used when desperate.)
  • Output the seed to screen.

  • Create a blowpipe ranged weapon.  The blowpipe itself does 2-3 damage with no bonus damage.  Set its projectile type to a dart.
  • Output the blowpipe to screen.
  • Enchant the blowpipe twice.
  • Output the blowpipe to screen.
  • Let’s pretend you ran out of darts.  Change the blowpipe’s projectile type to seed.
  • Output the blowpipe to screen.