Java

OOP GUIDE / WORK

 

WEAPON CLASS SOLUTIONS

 

 

TASK – PART 1 – PARTIAL WEAPON CLASS

 

No Work

 

 

TASK – PART 2 – COMPLETE THE CLASS

 

Here is my class:

 

 

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 name + "(" + lowDamage + "-" + highDamage + ") + " + damageBonus;

      }

     

      public void enchantWeapon()

      {

            damageBonus++;

      }

 

}

 

 

 

TASK – PART 3 – CODE ANALYSIS

 

Answer the following questions:

 

A)    Is the class mutable or immutable?  Explain.

Answer: It is mutable because the enchantWeapon() method changes an instance variable.

B)    What will the following code output to screen?  Try to figure it out before running it.

Weapon w1 = new Weapon("Sword", 4, 8, 0);
System.out.println(w);


Answer: Sword (4-8) + 0

 

C)   What will the following code output to screen?  Try to figure it out before running it.

Weapon w2 = new Weapon("Club", 3, 5, 0);

w2.enchantWeapon();

w2.enchantWeapon();
System.out.println(w2);


          Answer: Club (3-5) + 2

D)   What will the following code output to screen?  Try to figure it out before running it.

Weapon w3 = new Weapon("Nunchucks", 2, 4, 0);
System.out.println(w3.getDamageAmount());

Answer: A random integer between 2 and 4, inclusively.

E)    What will the following code output to screen?  Try to figure it out before running it.

Weapon w4 = new Weapon("Spear", 4, 8, 0);
w4.enchantWeapon();
System.out.println(w4.getDamageAmount());


Answer: A random integer between 5 and 9, inclusively.

F)    Can the value of damageBonus ever be negative?

Answer: Yes it can initially be set to a negative value when the Weapon is constructed.

 

G)   Add an instance method named curseWeapon() that does the opposite of enchantWeapon().


          Here is my method:

 

     public void curseWeapon()

     {

           damageBonus--;

     }

 

H)   Add an instance method named getDescriptiveName() that returns the name of the weapon with a descriptor if the damageBonus is too low or too high.  If it is less or equal to -2, the weapon is “Cursed”.  If it is greater or equal to 2, then the weapon is “of Power”.

For example,

·       A Sword with a damageBonus of 3 is a Sword of Power. 

·       A Staff with a damageBonus of -1 is a Staff.

·       A Spear with a damageBonus of -2 is a Cursed Spear.

 

          Here is my method:

 

     public String getDescriptiveName()

     {

           if (damageBonus >= 2)

                return name + " of Power";

           else if (damageBonus <= -2)

                return "Cursed " + name;

           else

                return name;

     }

 

I)      Update the toString() method so that it uses getDescriptiveName() instead of the regular name.

 

          Here is my updated toString() method:

 

public String toString()

{

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

}

 

J)     What will the following code output to screen?  Try to figure it out before running it.

Weapon w5 = new Weapon("Axe", 5, 7, 0);
w5.enchantWeapon();
w5.enchantWeapon();
System.out.println(w5);


          Answer: Axe of Power (5-7) + 2

 

K)    What is the least amount of enchantments needed to convert a cursed weapon into a powerful weapon?

Answer: 4
Explanation: The best cursed weapon has a -2 damageBonus.  It takes 4 increases of 1 to get a damageBonus of 2, which is the minimum for a powerful weapon.

 

L)    In the main method of a tester class, create a few weapons.  Test out all of the instance methods.

Here is my tester class:

 

public class WeaponTester

{

      public static void main(String[] args)

      {

            Weapon weapon = new Weapon("Dagger", 1, 3, 0);

            weapon.enchantWeapon();

            weapon.enchantWeapon();

            System.out.println(weapon);

            weapon.curseWeapon();

            System.out.println(weapon);

            System.out.println("Damage: " + weapon.getDamageAmount());

 

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

 

            Weapon weapon2 = new Weapon("Morning Star", 6, 6, 0);

            weapon2.curseWeapon();

            weapon2.curseWeapon();

            System.out.println(weapon2);

            System.out.println("Damage: " + weapon2.getDamageAmount());

 

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

 

            Weapon weapon3 = new Weapon("Mjolnir", 3, 4, 0);

            System.out.println(weapon3);

            System.out.println("Mr. Campeau, Odin will enchant your hammer!");

            for (int i=0; i<100; i++)

                  weapon3.enchantWeapon();

            System.out.println(weapon3);

            System.out.println("Damage: " + weapon3.getDamageAmount());

      }

}

 

 

Here is what my tester class outputs:

 

Dagger of Power(1-3) + 2

Dagger(1-3) + 1

Damage: 4

=====

Cursed Morning Star(6-6) + -2

Damage: 4

=====

Mjolnir(3-4) + 0

Mr. Campeau, Odin will enchant your hammer!

Mjolnir of Power(3-4) + 100

Damage: 104