Java

OOP GUIDE / WORK

 

RANGEDWEAPON CLASS 2 SOLUTIONS

 

 

TASK – PART 1 – SETUP

 

No solution required.

 

 

TASK – PART 2 – MORE SETUP


No solution required.

 

 

TASK – PART 3 – POLYMORPHISM

 

Here is my code:

 

public class Tester

{

     public static void main(String[] args)

     {

        Weapon[] w = new Weapon[8];

        w[0] = new Weapon("Sword",6,8,0);

  

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

        w[1] = new RangedWeapon("Blowpipe",2,3,0,dart);

 

        Projectile arrow = new Projectile("Arrow", 2);

        w[2] = new RangedWeapon("Bow",3,4,0,arrow);

 

        w[3] = new Weapon("Axe",7,9,0);

       

        w[4] = new Weapon("Spear",10,11,0);

       

        w[5] = new Weapon("Morning Star",5,10,0);

       

        Projectile sarrow = new Projectile("Silver Arrow", 3);

        w[6] = new RangedWeapon("Crossbow",3,4,0,sarrow);

       

        w[7] = new Weapon("Dagger",2,3,0);

       

        //Work

 

        int totalDamage = 0;

        int roundDamage;

        for (int round = 1; round <= 20; round++)

        {

           System.out.println("*************************************");

           System.out.println("ROUND " + round);

          

           roundDamage = 0;

           for (int i=0; i<w.length; i++)

           {

                int dmg = w[i].getDamageAmount();

                System.out.println(w[i].getDescriptiveName() + " does " + dmg + " damage.");

                roundDamage += dmg;

           }

           System.out.println("\nRound damage: " + roundDamage);

           totalDamage += roundDamage;

        }

        System.out.println("\n\nTotal damage: " + totalDamage);

     }

}