Difference between revisions of "Weapon class assignment"

From WLCS
(Attributes)
Line 8: Line 8:
 
The Weapon class should use instance variables to represent the following attributes.  All attributes should be declared '''private''':
 
The Weapon class should use instance variables to represent the following attributes.  All attributes should be declared '''private''':
  
* String name: the name of the weapon (e.g., "sword" or "magic wand").
+
* '''String name''': the name of the weapon (e.g., "sword" or "magic wand").
  
* int damageModifier: an integer added to the damage inflicted when the creature scores a hit.
+
* '''int damageModifier''': an integer added to the damage inflicted when the creature scores a hit.
  
* String hitVerb: a verb to describe the action of the weapon when it scores a hit (e.g., "slashes" or "zaps").
+
* '''String hitVerb''': a verb to describe the action of the weapon when it scores a hit (e.g., "slashes" or "zaps").
  
* String missVerb: a verb to describe the action of the weapon when it misses (e.g., "whiffs" or "misses").
+
* '''String missVerb''': a verb to describe the action of the weapon when it misses (e.g., "whiffs" or "misses").
  
 
== Methods ==
 
== Methods ==
Line 20: Line 20:
  
 
===Constructors:===
 
===Constructors:===
* Weapon(): the default constructor should initialize the damage modifier to 0, the name to "bare hands", the hit verb to "strikes" and the miss verb to "misses".
+
* '''Weapon()''': the default constructor should initialize the damage modifier to 0, the name to "bare hands", the hit verb to "strikes" and the miss verb to "misses".
  
* Weapon(int modifier, String name, String hit, String miss): this specific constructor should initialize all the attributes to the specified values.
+
* '''Weapon(int modifier, String name, String hit, String miss)''': this specific constructor should initialize all the attributes to the specified values.
  
 
===Accessors/Mutators:===
 
===Accessors/Mutators:===
 
* All the attributes are declared private, so you should create accessor and mutator methods for each attribute.  The name of the accessor/mutator should follow the standard Java naming conventions  
 
* All the attributes are declared private, so you should create accessor and mutator methods for each attribute.  The name of the accessor/mutator should follow the standard Java naming conventions  
** getName()
+
** '''getName()''': returns the name attribute
** setName(String newName)
+
** '''setName(String newName)''': sets the name attribute to the newName parameter
** getHitVerb()
+
** '''getHitVerb()''': returns the hit verb
** setHitVerb(String newHitVerb)
+
** '''setHitVerb(String newHitVerb)''': sets the hitVerb attribute to the newHitVerb parameter
** getMissVerb()
+
** '''getMissVerb()''': returns the miss verb
** setMissVerb(String newMissVerb)
+
** '''setMissVerb(String newMissVerb)''': sets the missVerb attribute to the newMissVerb parameter
** getDamageModifier()
+
** '''getDamageModifier()''': returns the damageModifier
** setDamageModifier(int newDamageModifier)
+
** '''setDamageModifier(int newDamageModifier)''': sets the damageModifier attribute to the newDamageModifier parameter

Revision as of 10:53, 30 April 2010

Objective

  • You will learn to create a basic class that encapsulates several attributes and methods

Purpose

  • The purpose of the Weapon class is to encapsulate the necessary attributes and methods that represent a weapon in the game

Attributes

The Weapon class should use instance variables to represent the following attributes. All attributes should be declared private:

  • String name: the name of the weapon (e.g., "sword" or "magic wand").
  • int damageModifier: an integer added to the damage inflicted when the creature scores a hit.
  • String hitVerb: a verb to describe the action of the weapon when it scores a hit (e.g., "slashes" or "zaps").
  • String missVerb: a verb to describe the action of the weapon when it misses (e.g., "whiffs" or "misses").

Methods

The Weapon class should have the following methods:

Constructors:

  • Weapon(): the default constructor should initialize the damage modifier to 0, the name to "bare hands", the hit verb to "strikes" and the miss verb to "misses".
  • Weapon(int modifier, String name, String hit, String miss): this specific constructor should initialize all the attributes to the specified values.

Accessors/Mutators:

  • All the attributes are declared private, so you should create accessor and mutator methods for each attribute. The name of the accessor/mutator should follow the standard Java naming conventions
    • getName(): returns the name attribute
    • setName(String newName): sets the name attribute to the newName parameter
    • getHitVerb(): returns the hit verb
    • setHitVerb(String newHitVerb): sets the hitVerb attribute to the newHitVerb parameter
    • getMissVerb(): returns the miss verb
    • setMissVerb(String newMissVerb): sets the missVerb attribute to the newMissVerb parameter
    • getDamageModifier(): returns the damageModifier
    • setDamageModifier(int newDamageModifier): sets the damageModifier attribute to the newDamageModifier parameter