beginner assignment

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • pjbroome
    New Member
    • Feb 2008
    • 6

    beginner assignment

    I'm new to all programming. I'm stonewalled, and I'll show what I have.My assignment is this:
    Write a Java Dice class which will simulate the rolling of one die and
    display the die face.
    Follow these requirements for the Dice class:
    · The class must be named Dice.
    · A method named diceRoll() must be included.
    · A random number between 1 and 6 inclusive must be generated.
    · The random number value generated must be returned.
    · The face of the die must be displayed as below:
    Requirements:
     The Dice class should be named Dice.java.
     Include comments defining the purpose of the code.
    Six:
    * *
    * *
    * *
    Five:
    * *
    *
    * *
    Four:
    * *
    * *
    Three:
    *
    *
    *
    Two:
    *
    *
    One:
    *
    Here's my code:[CODE=Java]
    import java.util.Rando m; // Needed for random dice numbers.

    public class Dice
    {
    public static void main (String[] args)
    {
    int face; // The face of the die.

    // A random object for the program.
    Random randomNumbers = new Random();
    // Die face number is set equal to arandom number.
    face = randomNumbers.n extInt(6)+ 1;
    face = diceRoll();
    // Display results.
    if (face == 1)
    System.out.prin tln("One:\n *");
    else if (face == 2)
    System.out.prin tln("Two:\n*\n\ n *");
    else if (face == 3)
    System.out.prin tln("Three:\n*\ n *\n *");
    else if (face == 4)
    System.out.prin tln("Four:\n* *\n\n* *");
    else if (face == 5)
    System.out.prin tln("Five:\n* *\n *\n* *");
    else if (face == 6)
    System.out.prin tln("Six:\n* *\n* *\n* *");
    }
    public int diceRoll()
    {
    return diceRoll();
    }

    }[/CODE]

    Gives me this error:
    Dice.java:14: non-static method diceRoll() cannot be referenced from a static context
    Last edited by BigDaddyLH; Feb 15 '08, 04:56 PM. Reason: added code tags
  • BigDaddyLH
    Recognized Expert Top Contributor
    • Dec 2007
    • 1216

    #2
    Please enclose your posted code in [code] tags (See How to Ask a Question).

    This makes it easier for our Experts to read and understand it. Failing to do so creates extra work for the moderators, thus wasting resources, otherwise available to answer the members' questions.

    Please use [code] tags in future.

    MODERATOR

    Comment

    • BigDaddyLH
      Recognized Expert Top Contributor
      • Dec 2007
      • 1216

      #3
      Originally posted by pjbroome
      [CODE=Java]
      public int diceRoll() {
      return diceRoll();
      }
      [/CODE]
      Rethink your code. This is an infinite loop!

      Comment

      • pjbroome
        New Member
        • Feb 2008
        • 6

        #4
        Originally posted by BigDaddyLH
        Please enclose your posted code in [code] tags (See How to Ask a Question).

        This makes it easier for our Experts to read and understand it. Failing to do so creates extra work for the moderators, thus wasting resources, otherwise available to answer the members' questions.

        Please use [code] tags in future.

        MODERATOR
        OK, sorry about that. Thanks

        Comment

        • BigDaddyLH
          Recognized Expert Top Contributor
          • Dec 2007
          • 1216

          #5
          Originally posted by pjbroome
          OK, sorry about that. Thanks
          No problem. Indent your code, too.

          Comment

          • pjbroome
            New Member
            • Feb 2008
            • 6

            #6
            Thanks. I am just lost because I'm supposed to use the method diceRoll, which is supposed to then be able to be used by other programs which involve rolling one die or two dice, getting the random outcomes that my created class provides. So I'm supposed to create this class, generating and returning the random die results (1 thru 6), and using the function or method diceRoll to do it. I'm gathering by the responses that I'm just way off. I'm in an online class, the book is helpful, but there is no interaction or help with this. But thanks for your help. I've taken it and will continue working. Perhaps somebody out there may know how impossible this is when you are brand new to it all!!

            Comment

            • BigDaddyLH
              Recognized Expert Top Contributor
              • Dec 2007
              • 1216

              #7
              Think about the design of class Dice (isn't the singular Die, by the way?). What is the observable state of a Die? It'\s current face. What can you do with a Die? Roll it, which may change its face...

              [CODE=Java]public class Die {
              ...
              public int getFace() {...}
              public void roll() {...}
              }[/CODE]

              What is the implemented state of a Die? The current state and a Random object to simulate rolling:

              [CODE=Java]public class Die {
              private int face;
              private Random rnd;
              ...
              }[/CODE]

              Is that how you think of code?

              Comment

              • pjbroome
                New Member
                • Feb 2008
                • 6

                #8
                Originally posted by BigDaddyLH
                Think about the design of class Dice (isn't the singular Die, by the way?). What is the observable state of a Die? It'\s current face. What can you do with a Die? Roll it, which may change its face...

                [CODE=Java]public class Die {
                ...
                public int getFace() {...}
                public void roll() {...}
                }[/CODE]

                What is the implemented state of a Die? The current state and a Random object to simulate rolling:

                [CODE=Java]public class Die {
                private int face;
                private Random rnd;
                ...
                }[/CODE]

                Is that how you think of code?
                Yes, thank you for this. The professor says we must name it dice instead of die, even though this is about one die - I'll get points off if I call it die but I wondered the same thing.
                I've got the code now so that it generates the random numbers 1 through 6. I've also got it printing out the way the teacher wants it. The program is doing what she wants it to do - with one exception. She wants me to use "diceRoll() " method in this program, so she can use this class (dice) in another game program that rolls two dice.
                I'm wondering, in your example above, you have:

                [CODE=Java]public class Die {
                ...
                public int getFace() {...}
                public void roll() {...}
                }[/CODE]
                Should I be using the name "rollDice() " instead of your "getFace"? Would renaming that help to get me closer?

                Comment

                • BigDaddyLH
                  Recognized Expert Top Contributor
                  • Dec 2007
                  • 1216

                  #9
                  What is the requirement for rollDice()? What does it do, what are its parameters and its return type? Is is a static method?

                  Comment

                  • pjbroome
                    New Member
                    • Feb 2008
                    • 6

                    #10
                    Originally posted by BigDaddyLH
                    What is the requirement for rollDice()? What does it do, what are its parameters and its return type? Is is a static method?
                    Good questions! These are my questions! Her instructions are, I quote:
                    "The class must be named Dice
                    A method named diceRoll() must be included
                    A random number between 1 and 6 inclusive must be generated
                    The random number value generated must be returned
                    The face of the die must be displayed as below:...[display not included]"

                    So I guess the rollDice() method is supposed to initiate the "roll of the die", which is the generation of a new random number, and then the display of the number. One must "roll a die" before a new number is seen, right?

                    Comment

                    • pjbroome
                      New Member
                      • Feb 2008
                      • 6

                      #11
                      Originally posted by pjbroome
                      Good questions! These are my questions! Her instructions are, I quote:
                      "The class must be named Dice
                      A method named diceRoll() must be included
                      A random number between 1 and 6 inclusive must be generated
                      The random number value generated must be returned
                      The face of the die must be displayed as below:...[display not included]"

                      So I guess the rollDice() method is supposed to initiate the "roll of the die", which is the generation of a new random number, and then the display of the number. One must "roll a die" before a new number is seen, right?
                      Here's my code now. Professor has a program that rolls 2 dice and it calls on the "diceRoll() " method that's supposed to be in my code to do it. I don't know how to get it to do that. The following code does what she wants "diceRoll() " to do, but how do I insert this method in this code??:

                      Code:
                      import java.util.Random; // Needed for random dice numbers.
                      
                      public class Dice
                      {
                        public static void main (String[] args)
                        {	
                      	int face; // The face of the die.
                      	
                         	
                        	// A random object for the program.
                         Random randomNumbers = new Random();
                         // Die face number is set equal to a random number.
                         face = randomNumbers.nextInt(6)+ 1;
                      
                         // Display results.
                         if (face == 1)
                      	System.out.println("One:\n\n *");
                      	else if (face == 2)
                      	System.out.println("Two:\n*\n\n  *");
                      	else if (face == 3)
                      	System.out.println("Three:\n*\n  *\n    *");
                      	else if (face == 4)
                      	System.out.println("Four:\n*  *\n\n*  *");
                      	else if (face == 5)
                      	System.out.println("Five:\n*   *\n  *\n*   *");
                      	else if (face == 6)
                      	System.out.println("Six:\n*  *\n*  *\n*  *");
                       	    
                      	} 
                      }

                      Comment

                      • Laharl
                        Recognized Expert Contributor
                        • Sep 2007
                        • 849

                        #12
                        Here's a hint: main() is a method. You need another method, called diceRoll(), that returns an integer. The 'void' keyword in main() means that it has no return type, but diceRoll() needs to return an int. How would you change the first line of main() (public static void main(String[] args)) to represent a method that takes no input and returns an integer?

                        Comment

                        Working...