I'm confused with the code below. It has two "constructors"?
First question, can a java code have two "constructors"?
Second, Why it has to have two constructors? The first constructor makes
non-sense to me, it sets the value of numFaces to 6 and faceValue to 1. But
why doesn't it set the value to 6 and 1 right at initializations, like:
private int numFaces=6;
private int faceValue=1;
And why it needs "constructors" to set the values?
What do constructors usually do anyway??
Thanks a lot,
Jenny
public class Die
{
private final int MIN_FACES = 4;
private int numFaces; // number of sides on the die
private int faceValue; // current value showing on the die
public Die () //I'm guessing this is the first constructor?
{
numFaces = 6;
faceValue = 1;
}
public Die (int faces) //I'm guessing this is the second constructor?
{
if (faces < MIN_FACES)
numFaces = 6;
else
numFaces = faces;
faceValue = 1;
}
public int roll ()
{
faceValue = (int) (Math.random() * numFaces) + 1;
return faceValue;
}
public int getFaceValue ()
{
return faceValue;
}
}