Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Could someone please better explain this to me?

  1. #1
    Member
    Join Date
    Aug 2013
    Posts
    40
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Could someone please better explain this to me?

    Assignment:
    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.)
    Question:
    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.


  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Could someone please better explain this to me?

    First, when reading the instructions, the entire first line is irrelevant, and in no way represents requirements of the code. (In fact it only complicates matters)
    The instructions say for the Die class to handle n number of sides (as opposed to the specific set required for the given game sample). Not to mention this would generalize the code and make it more reusable for the future. What if you wanted a 7 sided die? Or any other number not included? What about 999,999 sides? Will the code eventually have millions of || for all of the possible number of sides?

    You can not use static because it would be difficult to have many dice with a different number of sides. Each Die object needs to have a variable to store it's individual number of sides. When you design the class, forget about the main method. That method will create objects of the class and call their methods, but it should not store information about the state of the Die objects.
    When you get done you should be able to create a group of dice that includes one of each in the sample list without trouble, each one remembering it's own state.

    This tutorial should provide some helpful details

  3. #3
    Member
    Join Date
    Aug 2013
    Posts
    40
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Could someone please better explain this to me?

    I am having trouble understanding what to do in main. Like as to how to call the methods. Could you better explain that to me?

    Here is my current code:

    /* ***************************************************************************************************************************
     * 
     * 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 {
        int faceValue;
        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);
                System.out.println("test worked");
            }
            // 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();
             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;
        }
     
    }

  4. #4
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Could someone please better explain this to me?

    Your instructor wants you to create a Die object (like with constructors and stuff), not do everything in your main. Your main should create Die objects and then call the roll(), getFaceValue(), and toString() methods on your Die objects.

    Your requirements say you should have a constructor for Die which accepts an int, consider reviewing what you know about constructors before you go any further. Creating constructors is probably the most important thing to learn (or at least one of the most important things), so I'm reluctant to just give it to you. Review what notes you have, try it yourself, and if you run into any trouble or have any specific questions about how to do it, feel free to ask them here.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

Similar Threads

  1. Please explain to me what I am doing wrong?
    By Z9007 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: July 16th, 2013, 12:54 PM
  2. [SOLVED] Can someone explain to me?
    By unleashed-my-freedom in forum Java Theory & Questions
    Replies: 5
    Last Post: July 3rd, 2012, 04:10 AM
  3. i need an explain please !
    By keep smiling in forum Java Theory & Questions
    Replies: 3
    Last Post: December 21st, 2011, 11:22 AM
  4. Replies: 1
    Last Post: December 13th, 2010, 05:13 AM
  5. can anyone explain this?
    By chronoz13 in forum Java Theory & Questions
    Replies: 4
    Last Post: October 12th, 2009, 02:51 AM