Die program

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sallyk57
    New Member
    • Oct 2006
    • 13

    Die program

    The questions asks write an application to roll a die and display the results. It also says let the user pick the number of sides on the die. I wrote a program Die2 which rolls a die and display the results, but i don't how to do the second part. thanks

    import java.util.Rando m;
    public class myDie
    {
    private final int MIN_FACES = 4;

    private static Random generator = new Random();
    private int numFaces; // number of sides on the die
    private int faceValue; // current value showing on the die

    // Defaults to a six-sided die. Initial face value is 1.

    public myDie ()
    {
    numFaces = 6;
    faceValue = 1;
    }
    // Explicitly sets the size of the die. Defaults to a size of
    // six if the parameter is invalid. Initial face value is 1.
    public myDie (int faces)
    {
    if (faces < MIN_FACES)
    numFaces = 6;
    else
    numFaces = faces;

    faceValue = 1;
    }
    // Rolls the die and returns the result.
    public int roll ()
    {
    faceValue = generator.nextI nt(numFaces) + 1;
    return faceValue;
    }
    // Returns the current die value.
    public int getFaceValue ()
    {
    return faceValue;
    }
    }



    public class Die2
    {
    public static void main (String [] args)
    {
    Die myDie=new Die();
    System.out.prin tln("Rolling Die...");
    myDie.roll();
    System.out.prin tln("It's " +myDie.getFaceV alue());
    }
    }
  • DeMan
    Top Contributor
    • Nov 2006
    • 1799

    #2
    I notice that your class is declared myDie but you are trying to initialise a Die object.

    Code:
    public class Die2
    {
      public static void main (String [] args)
      {
        myDie thisDie=new myDie();
        System.out.println("Rolling Die...");
        thisDie.roll();
        System.out.println("It's " +thisDie.getFaceValue());
      }
    }

    Comment

    • sallyk57
      New Member
      • Oct 2006
      • 13

      #3
      the program works but what does it mean to have the user pick the number of sides on the die

      Comment

      • DeMan
        Top Contributor
        • Nov 2006
        • 1799

        #4
        I think it means you need to be able to adjust the program to return a random number between 1 and n (where n can be selected by the user). In reality, dice with more than 6 sides exist (for Dungeons & Dragons type games mainly). You can think of a 2 sided die as a coin (other die with less than 6 sides are hard to picture because they probably aren't possible in the real world).

        Anyway, I think what they want you to do is add a method to your program to modify the number of faces (Which you already give an option for through your constructor). You have written the code well, so that such a modification is trivial (and in fact is almost identical to your constructor).

        Comment

        • sallyk57
          New Member
          • Oct 2006
          • 13

          #5
          would this work

          import cs1.Keyboard;
          public class Die2
          {
          public static void main (String [] args)
          {
          System.out.prin tln("Enter the number of faces you want a die to have ");
          int guess= Keyboard.readIn t();

          myDie thisDie=new myDie();
          System.out.prin tln("Rolling Die...");
          thisDie.roll();
          System.out.prin tln("It's " +thisDie.getFac eValue());
          }
          }

          Comment

          • DeMan
            Top Contributor
            • Nov 2006
            • 1799

            #6
            I think they mean something more like :
            Code:
            import cs1.Keyboard;
            public class Die2 
            {
              public static void main (String [] args)
              { 
              System.out.println("Enter the number of faces you want a die to have ");
              int getFace= Keyboard.readInt();
            
              myDie thisDie=new myDie(getFace); //We use the other for of constructor to create a die that can cope with random numbers other than 6.  
            //An eight faced die for example, a two faced die (coin).
              System.out.println("Rolling Die...");
              thisDie.roll(); 
              System.out.println("It's " +thisDie.getFaceValue());
              }
            }

            Comment

            Working...