Java

OOP GUIDE / WORK

 

WEAPON CLASS

 

Topics

  • Encapsulation
  • Code analysis
  • Code completion

 

 

TASK – PART 1 – PARTIAL WEAPON CLASS

 

Consider the following partially completed Weapon class that represents a weapon this is part of a video game.  Copy it to your IDE.

 

 

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)

     {

           <complete this>

     }

    

     public <complete this> getName()

     {

           <complete this>

     }

    

     public int getLowDamage()

     {

           <complete this>

     }

    

     public int getHighDamage()

     {

           <complete this>

     }

    

     public int getDamageBonus()

     {

           <complete this>

     }

    

     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 2 – COMPLETE THE CLASS

 

Fix each section that says <complete this>.

 

TASK – PART 3 – CODE ANALYSIS

 

Answer the following questions:

 

A)    Is the class mutable or immutable?  Explain.

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(w1);


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);

 

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());


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());


F)    Can the value of damageBonus ever be negative?

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

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.

 

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

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);

 

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

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