Question
Dungeons and Dragons is a role-playing game where players use dice to generate personae (characters) that they will act the role of in various fantasy
Dungeons and Dragons is a role-playing game where players use dice to generate personae (characters) that they will act the role of in various fantasy settings. Imagine you are working for a game-programming company that is developing an RPG (role-playing game.)
Your job is first to debug the ScoreGenerator class and then to finish the Character class.
Each Character has a name and six abilities. The abilities are: Strength, Intelligence, Wisdom, Dexterity, Constitution, and Charisma. Here is an example printout of a character:
Name: UG MCFOOZLE | |
Strength: | 12 |
Intelligence: | 13 |
Wisdom: | 16 |
Dexterity: | 17 |
Constitution: | 13 |
Charisma: | 8 |
The ScoreGenerator class is used to generate an ability score ranging from 3 to 18. Four six-sided dice are rolled, the lowest value is discarded and the sum of the highest three is the ability score. A Character is made up of a name (String) and 6 abilities (Ability). The Ability class has been written for you and the Die class has been written for you.
-
Compile the ScoreGenerator class and fix the errors that you find. When you get the class to compile, run the debugger and follow the changes to variable values that occur as it generates an ability. Fix the errors that you observe. Hint: use the debugger to `step into' the getMinimum method when it is called from the generateScore method.
-
Now finish the Character class. The comments inside of the class tell you what is needed. Your finished product will enable the Party class to generate 3 characters and print out their profiles.
public class ScoreGenerator { private Die die1, die2, die3, die4;// Dice used to roll up ability scores
/** * Construct an ability generator out of four Die objects. */ public ScoreGenerator() { die1 = new Die(); die2 = new Die(); die3 = new Die(); die4 = new Die(); }
/** * Roll the dice. */ private void rollDice() { die1.rollDie(); die2.rollDie(); die3.rollDie(); die4.rollDie(); }
/** * Generate a new ability score by rolling 4 six-sided dice, * discarding the lowest and summing the remaining three dice. */ public int generateScore() { //correct the compiler errors in this class first int sum = 0;// After you get the class to compile, set a breakpoint here and REMOVE THIS COMMENT int min = 6; rollDice(); int face1 = die1; int face2 = die2; int face3 = die3; int face4 = die4;
min = getMinimum(face1, face2, face2, face4); sum = face1 + face2 + face3 + face4 + min; return sum; }
/** * Get the minimum face of the four dice. * NOTE: FIXING THE ERROR IN THIS METHOD SHOULD NOT TAKE MUCH WORK. IF YOU * ARE ADDING MANY LINES OF CODE YOU ARE WORKING TOO HARD. */ private int getMinimum(int a, int b, int c, int d) { int min = 0; if(a < min) { min = a; } if(b < min) { min = b; } if(c < min) { min = c; } if(d < min) { min = d; } return min; } }
/** * Stores original and current values of an ability. * * @author Derek Green * @version Feb 28, 04 */ public class Ability { private int originalScore; private int currentScore;
/** * Construct an ability. */ public Ability(int score) { originalScore = score; currentScore = score; } /** * Get original score. */ public int getOriginalScore() { return originalScore; }
/** * Get current score. */ public int getCurrentScore() { return currentScore; } /** * Set current score. */ public void setCurrentScore(int newScore) { currentScore = newScore; } }
/** * A single six-sided die. * * @author Derek Green * @version 2/7/04 */ public class Die { private int face; /** * Create a single die. Face defaults to a random int. */ public Die() { rollDie(); }
/** * Simulate the rolling of a die. */ public void rollDie() { int num = (int)(Math.random() * 6 + 1); face = num; } /** * Get the face value of the die. */ public int getFace() { return face; } }
/** * A single character description for use with Dungeons and Dragons or similar * RPG. * * @author Derek Green * @version Feb 28, 04 */ public class Character { private String name; private Ability strength; private Ability intelligence; private Ability wisdom; private Ability dexterity; private Ability constitution; private Ability charisma;
/** * Construct a Character. */ public Character(String name) { /* create a ScoreGenerator and use it to initialize the ability fields, initialize the name field, and REMOVE ALL OF THIS COMMENT */ ScoreGenerator myGenerator = new ScoreGenerator(); }
/** * Print out character details. */ public void printCharacter() { /* Print out the details of a character EXACTLY as in the following example: ------------------------------ Name: UG MCFOOZLE Strength: 12 Intelligence: 13 Wisdom: 16 Dexterity: 17 Constitution: 13 Charisma: 8 ------------------------------ Hint: The String class has a method toUpperCase() which returns the String it is applied to with all letters uppercase. */ } }
/** * Generates a small party of adventurers. * * @author Derek Green * @version Feb 28, 04 */ public class Party { private Character ug; private Character bub; private Character dino; /** * Construct a party. */ public Party() { ug = new Character("Ug McFoozle"); bub = new Character("Bub Crooger"); dino = new Character("Dino Flagellate"); }
/** * Print out the details of the adventuring party. */ public void printParty() { ug.printCharacter(); bub.printCharacter(); dino.printCharacter(); } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started