Question: create subclasses for different types of dice LoadedDice for a biased dice Use the setter to set the instance variables in the constructors. Do not

create subclasses for different types of dice LoadedDice for a biased dice
Use the setter to set the instance variables in the constructors. Do not repeat code range
validating code.
3. methods like setNumSide mush check value before setting If given value has an invalid
value set it to the default value (e.g. number of sides =6)
4. fix anything else so your code passes all the test (Dicetest1... DiceTest4).
5. change the access qualifier for your random variable from private to protected
6. add a static count, initialized to 0 and is incremented each time a new dice is created
7. add a static method getCount() that returns the value of this new static count.
8. overwrite the toString() method. (1 of "+ count +" Dice with + sides +" sides";)
Initialization:
LoadedDice must have a default constructor that set sides to the default value of 6,
creates a new Random object, sets loaded side and bias to 0(as ion not used)
LoadedDice also must have constructors for
o number of sides, loaded side, and bias;
o number of sides, loaded side, bias, and random;
o The default values are
number of sides=6; - same a base class Dice
loaded side=6,- valid sides start at 1- so 0 means no loaded side
bias=0.5.- probability of 0.0- no bias
2. Bias Configuration:
The class must allow setting a bias probability, which determines the likelihood of the
dice landing on the loaded side.
3. Dice Rolling:
Overriding the roll method, LoadedDice should use the bias probability to potentially
alter the outcome towards the loaded side.
If the bias condition is not met, the roll should result in a fair roll among all sides.
E.g.
if ( randy.nextDouble()< bias)
// The roll is biased towards the loaded side
// so return the loaded side
else
// Perform a regular roll (eg superclass's roll method)
A better way to do this would be set probabilities for each side
Default would be something like
(0-1/6)-->1,
(1/6-2/6)-->2,
(2/6-3/6)-->3,
(3/6-4/6)-->4,
(4/65/6)-->5,
(5/6-6/6)-->5.
Where we take the number of sides and given loaded probability sum equal to 1
This was my base class.
import java.util.Random;
public class Dice {
private static int count =0;
private int numSides;
private Random random;
public Dice()
{
this.numSides =6;
this.random = new Random();
count++;
}
public Dice(int numSides)
{
if (numSides <1)
{
throw new IllegalArgumentException("Number of sides must be at least 1");
}
this.numSides = numSides;
this.random = new Random();
count++;
}
public Dice(int numSides, Random random)
{
if (numSides <1)
{
throw new IllegalArgumentException("Number of sides must be at least 1");
}
this.numSides = numSides;
this.random = random;
count++;
}
public int roll()
{
return random.nextInt(numSides)+1;
}
public int getNumSides()
{
return numSides;
}
public void setNumSides(int numSides)
{
if (numSides <1)
{
throw new IllegalArgumentException("Number of sides must be at least 1");
}
this.numSides = numSides;
}
public static int getCount()
{
return count;
}
public String toString()
{
return "Dice with "+ numSides + "sides. Count: "+ count;
}
}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!