Difference between revisions of "Weapon class assignment"
From WLCS
(→Methods) |
|||
Line 19: | Line 19: | ||
The Weapon class should have the following methods: | The Weapon class should have the following methods: | ||
− | ===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()''': returns the name attribute | ** '''getName()''': returns the name attribute | ||
Line 34: | Line 34: | ||
** '''getDamageModifier()''': returns the damageModifier | ** '''getDamageModifier()''': returns the damageModifier | ||
** '''setDamageModifier(int newDamageModifier)''': sets the damageModifier attribute to the newDamageModifier parameter | ** '''setDamageModifier(int newDamageModifier)''': sets the damageModifier attribute to the newDamageModifier parameter | ||
+ | |||
+ | === Other Method(s): === | ||
+ | * Add the following method to your Weapon class: | ||
+ | |||
+ | <source lang="java"> | ||
+ | public String getFullName() | ||
+ | { | ||
+ | String modifier = ""; | ||
+ | if ( damageModifier < 0 ) | ||
+ | modifier = "cursed"; | ||
+ | else if ( damageModifier < 3 ) | ||
+ | modifier = "normal"; | ||
+ | else if ( damageModifier < 6 ) | ||
+ | modifier = "shiny"; | ||
+ | else if ( damageModifier < 9 ) | ||
+ | modifier = "high quality"; | ||
+ | else if ( damageModifier < 12 ) | ||
+ | modifier = "elite"; | ||
+ | else | ||
+ | modifier = "magical"; | ||
+ | return modifier + " " + getName(); | ||
+ | } | ||
+ | </source> |
Revision as of 09:56, 7 May 2010
Contents
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
Other Method(s):
- Add the following method to your Weapon class:
public String getFullName()
{
String modifier = "";
if ( damageModifier < 0 )
modifier = "cursed";
else if ( damageModifier < 3 )
modifier = "normal";
else if ( damageModifier < 6 )
modifier = "shiny";
else if ( damageModifier < 9 )
modifier = "high quality";
else if ( damageModifier < 12 )
modifier = "elite";
else
modifier = "magical";
return modifier + " " + getName();
}