Assignment:
Question:A normal dice bag for playing Dungeons and Dragons contains dice with the following numbers of sides: 4, 6, 8, 10, 12, 20, and 100.
Create a class called Die (singular of "dice") that represents an n-sided die, where n is specified in the constructor. The class should have the following public methods:
int roll(): sets (and returns) the face value to a uniform random number between 1 and the number of faces
int getFaceValue(): returns the current face value of the die
String toString(): returns the string representation of the face value
You must also write a program (in the main function of the Die class) that prompts the user for the die to choose and outputs the result of a roll.
(You will find the java.util.Random class useful in this assignment.)
When I showed him my code, he said that he wanted us to use the methods as objects of the class Die. Could someone please explain to me a little clearer as to what that means?
Here is my origonal code that I showed him:
/* *************************************************************************************************************************** * * Program: Die * Author: Jared Benedict * Date: 9/23/13 * Description: It asks the user to select a die, and then it returns the face value of the chosen die. * *************************************************************************************************************************** */ import java.io.*; import java.util.Random; import java.io.Console; public class Die { public static void main (String[] args) { // Declaring the variables. int dieSides; String goAgain = "y"; // While loop that repeats the program until the user chooses to end it. while ( !goAgain.equals("n") ) { // Reading in the user input for the number of sides on the die, Console console = System.console(); String sides = console.readLine("\nPlease select a type of die to roll: 4, 6, 8, 10, 12, 20, or 100 : "); // Taking the string and converting it into an int. dieSides = Integer.parseInt(sides); // In order to make sure the user does not choose an option that does not exist. if (dieSides == 4 || dieSides == 6 || dieSides == 8 || dieSides == 10 || dieSides == 12 || dieSides == 20 || dieSides == 100){ Die.roll(dieSides); Die.getFaceValue(faceValue); String Result = Die.toString(faceValue); System.out.println ("You rolled: " + Result); } // See comment above. else { System.out.println ("The die that you selected does not exist... \nPlease make another selection,,,"); //System.exit(0); } // Asks the user if they would like to roll again. goAgain = console.readLine("\nDo you wish to roll another die? (y/n) : "); } } public static int roll(int dieSides){ // Sets (and returns) the face value to a uniform random number between 1 and the number of faces. int numSides = dieSides; Random generator = new Random(); int faceValue; faceValue = generator.nextInt(numSides) + 1; System.out.println("You rolled a: " + faceValue); //return 0; return faceValue; // make sure to comment this out for the above to work. } public static int getFaceValue(int faceValue){ // returns the current face value of the die. return faceValue; } public static String toString(int faceValue){ // returns the string representation of the face value String result = Integer.toString(faceValue); return result; } }
Note:
He told me that he did not want the methods as static methods and that he wanted them to be objects of the class.
So this is my current code now, but I still don't understand what he is talking about.
/* *************************************************************************************************************************** * * Program: Die * Author: Jared Benedict * Date: 9/23/13 * Description: It asks the user to select a die, and then it returns the face value of the chosen die. * *************************************************************************************************************************** */ import java.io.*; import java.util.Random; import java.io.Console; public class Die { public static void main (String[] args) { // Declaring the variables. int dieSides; String goAgain = "y"; // While loop that repeats the program until the user chooses to end it. while ( !goAgain.equals("n") ) { // Reading in the user input for the number of sides on the die, Console console = System.console(); String sides = console.readLine("\nPlease select a type of die to roll: 4, 6, 8, 10, 12, 20, or 100 : "); // Taking the string and converting it into an int. dieSides = Integer.parseInt(sides); // In order to make sure the user does not choose an option that does not exist. if (dieSides == 4 || dieSides == 6 || dieSides == 8 || dieSides == 10 || dieSides == 12 || dieSides == 20 || dieSides == 100){ Die.roll(dieSides); Die.getFaceValue(faceValue); String Result = Die.toString(faceValue); System.out.println ("You rolled: " + Result); } // See comment above. else { System.out.println ("The die that you selected does not exist... \nPlease make another selection,,,"); //System.exit(0); } // Asks the user if they would like to roll again. goAgain = console.readLine("\nDo you wish to roll another die? (y/n) : "); } } public int roll(int dieSides){ // Sets (and returns) the face value to a uniform random number between 1 and the number of faces. int numSides = dieSides; Random generator = new Random(); int faceValue; faceValue = generator.nextInt(numSides) + 1; System.out.println("You rolled a: " + faceValue); //return 0; return faceValue; // make sure to comment this out for the above to work. } public int getFaceValue(int faceValue){ // returns the current face value of the die. return faceValue; } public String toString(int faceValue){ // returns the string representation of the face value String result = Integer.toString(faceValue); return result; } }
Thank you.