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());
}
}
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());
}
}
Comment