By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,538 Members | 1,130 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,538 IT Pros & Developers. It's quick & easy.

Die program

P: 13
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.Random;
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.nextInt(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.println("Rolling Die...");
myDie.roll();
System.out.println("It's " +myDie.getFaceValue());
}
}
Dec 10 '06 #1
Share this Question
Share on Google+
5 Replies


DeMan
100+
P: 1,806
I notice that your class is declared myDie but you are trying to initialise a Die object.

Expand|Select|Wrap|Line Numbers
  1. public class Die2
  2. {
  3.   public static void main (String [] args)
  4.   {
  5.     myDie thisDie=new myDie();
  6.     System.out.println("Rolling Die...");
  7.     thisDie.roll();
  8.     System.out.println("It's " +thisDie.getFaceValue());
  9.   }
  10. }
  11.  
Dec 10 '06 #2

P: 13
the program works but what does it mean to have the user pick the number of sides on the die
Dec 10 '06 #3

DeMan
100+
P: 1,806
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).
Dec 10 '06 #4

P: 13
would this work

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 guess= Keyboard.readInt();

myDie thisDie=new myDie();
System.out.println("Rolling Die...");
thisDie.roll();
System.out.println("It's " +thisDie.getFaceValue());
}
}
Dec 10 '06 #5

DeMan
100+
P: 1,806
I think they mean something more like :
Expand|Select|Wrap|Line Numbers
  1. import cs1.Keyboard;
  2. public class Die2 
  3. {
  4.   public static void main (String [] args)
  5.   { 
  6.   System.out.println("Enter the number of faces you want a die to have ");
  7.   int getFace= Keyboard.readInt();
  8.  
  9.   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.  
  10. //An eight faced die for example, a two faced die (coin).
  11.   System.out.println("Rolling Die...");
  12.   thisDie.roll(); 
  13.   System.out.println("It's " +thisDie.getFaceValue());
  14.   }
  15. }
  16.  
Dec 10 '06 #6

Post your reply

Sign in to post your reply or Sign up for a free account.