This class will be designed to represent a type of character in a RPG (Role Play
ID: 3668744 • Letter: T
Question
This class will be designed to represent a type of character in a RPG (Role Playing Game.) Some of you are probably already familiar with the idea of a RPG but even if you are not don’t worry – I could have just as easily asked for a Professor class or a Veterinarian class or really just about ANY class – they are ALL done the same way. All you will need to know is what data and methods you should create for your class.
MAKE SURE YOU READ EVERY LINE AND WORD OF THE ASSIGNMENT. Then go through piece by piece and make sure you implement it.
Data (10 points)
Consider the following short list of attributes / properties for a fighter (5 points)
1. Name .................... The name of the fighter
2. Level ..................... An int from 1-20. Represents the fighter’s rank – helpful in battles
3. Experience ............ An int that represents the accumulation of points obtained during battles and other things
4. Strength ................ An int from 1-20. Represents the fighter’s strength – helpful in battles and other things
5. Dexterity ............... An int from 1-20. Represents the fighter’s agility – helpful in battles and other things
6. Charisma ............... An int from 1-20. Represents the fighter’s personality. How good they are with people/animals
7. BattlesWon ........... An int that represents the number of battles the fighter has won.
8. Speed .................... An int from 0-10. Represents how fast the fighter is moving
9. Life ........................ An int from 0-100. Represents the fighter’s life points. 0 = dead. 100 = full life.
In addition, make Getters and Setters for all of these instance variables, following the rules/practices we did in class (remember the Test example?). (5 points)
Basic Methods (10 points)
Of course there are more that could and should be included in a Fighter class. In addition to the attributes / properties of the fighter we should include some things the fighter can do. These are called methods. Consider the following short list of methods for a fighter:
1. Run() ..................... A method that will indicate the fighter is running. It might set a speed property higher
2. Walk() ................... A method that would probably set the fighter’s speed lower than running but higher than 1
3. Stop() .................... A method that will set the fighter’s speed to 0.
4. Talk() ..................... A method that simulates talking to someone/something and uses the fighter’s charisma value
5. Fight() ................... A method that controls a battle. Attributes (Level, Strength, Dexterity, etc.) will be used.
Additional Requirements (Methods and Attributes) (10 points)
There are many other methods you could add to your fighter if you think about it. Some methods might be able to handle more than one thing. The point is you as a designer must get a “best guess” on the properties and methods this class might
CSE 271 – Spring 2016 2-12-2016 due: 11:59pm 2-16-2016
need in order to accomplish the goal of a fighter in a RPG. In order to do this you must first know how this class will be used. Consider the following requirements for the Fighter class you are designing:
1. The fighter will be placed in a game that has treasure, objects they can carry and use, enemies, animals, and friends.
2. The fighter will have the ability to run, walk, stand still, talk to other characters and animals, and attack foe.
3. The fighter will (walk) travel around a large map looking for items. Therefore, your Fighter class must have methods for “picking up item” and “dropping item”.
There are, of course, many other attributes we could add and many more things the character could do. Your job at this point is to create this class with properties, methods, constructors, getters, and setters that will apply to the Fighter class.
Additional Requirements Action Items
You are to add at least 2 more properties and 2 more methods of your own and they should make sense. For some of the methods you should include basic code. Optionally, you can add code to many of the methods.
Constructors
Following what we did in the DOG example in Thursday’s lecture, create one all encompassing constructor that allows you to initialize every single instance variable to provided values. HINT: You can use Eclipse’s SOURCE->Generator constructor functionality. Then, create at least two more constructors that calls that all encompassing constructor (ex this (…), like we did for DOG), that provides two additional ways of making fighters.
General (5 points)
Well commented (HINT: use “Generate Comments” when creating your class and getters and setters and then fill it in. Use “Generate Element Comments” for your other methods and fill it in. File is named corrected. HINT: You will not be running this class, so you shouldn’t i
Explanation / Answer
Class Fighter:
package main.resources;
import java.util.List;
public class Fighter {
private String name;
private int level;
private int experience;
private int strength;
private int dexterity;
private int charisama;
private int battlesWon;
private int speed;
private int life;
private List<Item> items;
private List<Weapons> weapons;
public List<Weapons> getWeapons() {
return weapons;
}
public void setWeapons(List<Weapons> weapons) {
this.weapons = weapons;
}
public List<Item> getItems() {
return items;
}
public void setItems(List<Item> items) {
this.items = items;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getLevel() {
return level;
}
public void setLevel(int level) throws Exception {
if (FighterHelper.checkLevel(level))
this.level = level;
else
throw new Exception("Level value should be 1-20.");
}
public int getExperience() {
return experience;
}
public void setExperience(int experience) {
this.experience = experience;
}
public int getStrength() {
return strength;
}
public void setStrength(int strength) throws Exception {
if (FighterHelper.checkStrength(strength))
this.strength = strength;
else
throw new Exception("Strength value should be 1-20.");
}
public int getDexterity() {
return dexterity;
}
public void setDexterity(int dexterity) throws Exception {
if (FighterHelper.checkDexterity(dexterity))
this.dexterity = dexterity;
else
throw new Exception("Dexterity value should be 1-20.");
}
public int getCharisama() {
return charisama;
}
public void setCharisama(int charisama) throws Exception {
if (FighterHelper.checkCharmisa(charisama))
this.charisama = charisama;
else
throw new Exception("Charmisa value should be 1-20.");
}
public int getBattlesWon() {
return battlesWon;
}
public void setBattlesWon(int battlesWon) {
this.battlesWon = battlesWon;
}
public int getSpeed() {
return speed;
}
public void setSpeed(int speed) throws Exception {
if (FighterHelper.checkSpeed(speed))
this.speed = speed;
else
throw new Exception("Speed value should be 0-10.");
}
public int getLife() {
return life;
}
public void setLife(int life) throws Exception {
if (FighterHelper.checkLife(life))
this.life = life;
else
throw new Exception("Life value should be 0-100.");
}
public void walk() throws Exception {
int temp = (getSpeed() / 2);
setSpeed(temp > 1 ? temp : 1);
}
public void run() throws Exception {
setSpeed(10);
}
public void stop() throws Exception {
setSpeed(0);
}
public boolean fight(Fighter f) throws Exception {
int divident = 2;
if (this.getLevel() > f.getLevel()) {
divident = 4;
}
f.setStrength(f.getStrength() - (this.getLevel() / divident));
f.setDexterity(f.getDexterity() - (this.getLevel() / divident));
if (f.getStrength() == 0)
return false;
else
return true;
}
public void dropItem(Item e) {
items.remove(e);
}
public void addItem(Item e) {
items.add(e);
}
@Override
public String toString() {
return "Fighter [name=" + name + ", level=" + level + ", experience=" + experience + ", strength=" + strength
+ ", dexterity=" + dexterity + ", charisama=" + charisama + ", battlesWon=" + battlesWon + ", speed="
+ speed + ", life=" + life + "]";
}
}
Class FighterHelper
package main.resources;
public class FighterHelper {
private static boolean checkRange(int value, int startRange, int endRange) {
if (value < startRange || value > endRange) {
return false;
}
return true;
}
public static boolean checkLevel(int value) {
return checkRange(value, 1, 20);
}
public static boolean checkStrength(int value) {
return checkRange(value, 1, 20);
}
public static boolean checkDexterity(int value) {
return checkRange(value, 1, 20);
}
public static boolean checkCharmisa(int value) {
return checkRange(value, 1, 20);
}
public static boolean checkSpeed(int value) {
return checkRange(value, 0, 10);
}
public static boolean checkLife(int value) {
return checkRange(value, 0, 100);
}
}
Class Item:
package main.resources;
public class Item {
private String name;
private String type;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
}
Class Weapons:
package main.resources;
public class Weapons {
private String name;
private String weapontype;
private int range;
private int ammo;
private int killRate;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getWeapontype() {
return weapontype;
}
public void setWeapontype(String weapontype) {
this.weapontype = weapontype;
}
public int getRange() {
return range;
}
public void setRange(int range) {
this.range = range;
}
public int getAmmo() {
return ammo;
}
public void setAmmo(int ammo) {
this.ammo = ammo;
}
public int getKillRate() {
return killRate;
}
public void setKillRate(int killRate) {
this.killRate = killRate;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.