Java

OOP GUIDE / WORK

 

RANGEDWEAPON CLASS SOLUTIONS

 

TASK – PART 1 & 2

 

No work

 

TASK – PART 3 – A PROJECTILE CLASS

 

Here is my code:

 

 

 

public class Projectile

{

     private String name;

     private int damageIncrease;

    

     public Projectile(String n, int di)

     {

           name = n;

           damageIncrease = di;

     }

    

     public String toString()

     {

           return name + "(+" + damageIncrease + ")";

     }

    

     public int getDamageIncrease()

     {

           return damageIncrease;

     }

    

    public String getName()

    {

    return name;

    }

}

 

 

 

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

 

Here is my code:

 

 

public class RangedWeapon extends Weapon

{

   private Projectile proj;

  

   public RangedWeapon(String name, int ld, int hd, int db, Projectile p)

   {

         super(name, ld, hd, db);

         proj = p;

   }

  

   public RangedWeapon(String name, int ld, int hd, int db, String pname, int pdi)

   {

         super(name, ld, hd, db);

         proj = new Projectile(pname, pdi);

   }

     

   public int getProjectileDamageIncrease()

   {

         return proj.getDamageIncrease();

   }

  

   public String getProjectileName()

   {

         return proj.getName();

   }

 

   public void setProjectile(Projectile p)

   {

         proj = p;

   }

  

   public void setProjectile(String pname, int pdi)

   {

         proj = new Projectile(pname, pdi);

   }

  

   @Override

   public String getName()  

   {

         return name + " & " + proj.getName();

   }

  

   @Override

   public String getDescriptiveName()

   {

         return super.getDescriptiveName() + " & " + proj.getName();

   }

  

   @Override

   public int getLowDamage()

   {

         return lowDamage + proj.getDamageIncrease();

   }

  

   @Override

   public int getHighDamage()

   {

         return highDamage + proj.getDamageIncrease();

   }

  

   @Override

   public int getDamageAmount()

   {

      int dif = getHighDamage() - getLowDamage();

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

      return dmg;

   }

 

   @Override

   public String toString()

   {

       String combinedName =  getDescriptiveName();

       int myLd = getLowDamage();

       int myHd = getHighDamage();

       return combinedName + "(" + myLd + "-" + myHd + ")" + " + " + damageBonus;

   }

}

 

 

 

 

 

TASK – PART 5 – TESTING OUR CLASSES

 

Here is my code:

 

 

 

public class RangedWeaponTester

{

     public static void main(String[] args)

     {

           Weapon sword = new Weapon("Sword",3,5,0);

           System.out.println(sword);

           sword.enchantWeapon();

           sword.enchantWeapon();

           System.out.println(sword);

          

           Projectile dart = new Projectile("Dart", 1);

           System.out.println(dart);

          

           Projectile seed = new Projectile("Seed", 0);

           System.out.println(seed);

          

           RangedWeapon blowpipe = new RangedWeapon("Blowpipe",2,3,0,dart);

           System.out.println(blowpipe);

           blowpipe.enchantWeapon();

           blowpipe.enchantWeapon();

           System.out.println(blowpipe);

           blowpipe.setProjectile(seed);

           System.out.println(blowpipe);

     }

}

 

 

The above gives the following output:

 

Sword(3-5) + 0

Sword of Power(3-5) + 2

Dart(+1)

Seed(+0)

Blowpipe & Dart(3-4) + 0

Blowpipe of Power & Dart(3-4) + 2

Blowpipe of Power & Seed(2-3) + 2