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.
methods like setNumSide mush check value before setting If given value has an invalid
value set it to the default value eg number of sides
fix anything else so your code passes all the test Dicetest DiceTest
change the access qualifier for your random variable from private to protected
add a static count, initialized to and is incremented each time a new dice is created
add a static method getCount that returns the value of this new static count.
overwrite the toString method. of count Dice with sides sides";
Initialization:
LoadedDice must have a default constructor that set sides to the default value of
creates a new Random object, sets loaded side and bias to 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; same a base class Dice
loaded side valid sides start at so means no loaded side
bias probability of no bias
Bias Configuration:
The class must allow setting a bias probability, which determines the likelihood of the
dice landing on the loaded side.
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.
Eg
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
Where we take the number of sides and given loaded probability sum equal to
This was my base class.
import java.util.Random;
public class Dice
private static int count ;
private int numSides;
private Random random;
public Dice
this.numSides ;
this.random new Random;
count;
public Diceint numSides
if numSides
throw new IllegalArgumentExceptionNumber of sides must be at least ;
this.numSides numSides;
this.random new Random;
count;
public Diceint numSides, Random random
if numSides
throw new IllegalArgumentExceptionNumber of sides must be at least ;
this.numSides numSides;
this.random random;
count;
public int roll
return random.nextIntnumSides;
public int getNumSides
return numSides;
public void setNumSidesint numSides
if numSides
throw new IllegalArgumentExceptionNumber of sides must be at least ;
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
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
