Question: This is a short assingment for an entry level class. basic code and plentiful comments to explain are greatly appreciated. thanks. attached in text is
This is a short assingment for an entry level class. basic code and plentiful comments to explain are greatly appreciated.
thanks. attached in text is skeleton code to help. instructions are in the images
import java.util.Random;
public class Die
{
private int _maxNumSides;
private Random _rand;
// class level variable with visibility for sub classes
protected int _faceValue;
// no param constructor for normal 6 sided die
public Die()
{
_rand = new Random();
_maxNumSides = 6;
this.roll();
}
// constructore for later implementation
// public Die(int maxSides)
// {
// _rand = new Random();
// _maxNumSides = maxSides;
// this.roll();
// }
// will randomly reset the number of dots showing on a side
public int roll()
{
_faceValue = _rand.nextInt(_maxNumSides) + 1;
return _faceValue;
}
// retrieves the currently showing number of dots
public int getFaceValue()
{
return _faceValue;
}
// toString override for wider compatibility
@Override public String toString()
{
return Integer.toString(_faceValue);
}
// protected method for inheriting sub classes
protected int sides()
{
return _maxNumSides;
}
}


this is the tester code:
import java.util.ArrayList;
public class LoadedDieTester
{
public static void main(String[] args)
{
final int DIE_ROLLS = 100000; // change this to suit your testing load
ArrayList
ArrayList
int[] distro;
double[] sidesEx =
{ .1, .1, .1, .1, .1 };
double[] sidesEx2 =
{ .1, .1, .1, .1, .1, .1, .1 };
double[] weightsEx =
{ .1, .2, .3, .4, .5, .6 };
double[] evenOdds =
{ .166666, .166666, .166666, .166666, .166666, .166666, };
double[] fourHeavy =
{ .1, .1, .1, .5, .1, .1, };
double[] threeHeavy =
{ .05, .05, .75, .05, .05, .05, };
namesList.add("No weights");
weightsList.add(null); // an empty weights list
namesList.add("Too few weights");
weightsList.add(sidesEx);
namesList.add("Too many weights");
weightsList.add(sidesEx2);
namesList.add("Weights don't add up");
weightsList.add(weightsEx);
namesList.add("Even weights");
weightsList.add(evenOdds);
namesList.add("Four Heavy");
weightsList.add(fourHeavy);
namesList.add("Three Heavy");
weightsList.add(threeHeavy);
System.out.println(" Testing: No param constructor");
try
{
distro = new int[6];
LoadedDie wd = new LoadedDie();
DistributeRolls(wd, distro, DIE_ROLLS);
displayDistribution(distro, DIE_ROLLS);
}
catch (IllegalArgumentException iax)
{
System.out.println(iax.getMessage());
}
catch (Exception ex)
{
System.out.println(ex);
}
// testing the pre-built set of weights
for (int i = 0; i
{
// manage an array to capture the number of times the values are rolled
double[] weights = weightsList.get(i);
if (weights == null)
{
distro = new int[6];
}
else
{
distro = new int[weights.length];
}
System.out.println(" Testing: " + namesList.get(i));
try
{
// create a loaded die the roll it an number of times
// capturing the number of times each value showed up
LoadedDie wd = new LoadedDie(weights);
DistributeRolls(wd, distro, DIE_ROLLS);
displayDistribution(distro, DIE_ROLLS);
}
catch (IllegalArgumentException iax)
{
System.out.println(iax.getMessage());
}
catch (Exception ex)
{
System.out.println(ex);
}
}
}
// will roll a loaded die a specified number of times and keep track of the rolls
private static void DistributeRolls(LoadedDie wd, int[] distro, int totalRolls)
{
for (int rolls = 0; rolls
{
int rolled = wd.roll();
distro[rolled - 1]++;
}
}
// can display the percentage distribution of the values based on the total rolls
private static void displayDistribution(int[] distro, int totalRolls)
{
System.out.println("Value\tRolled\tPercent");
for (int vals = 0; vals
{
System.out.printf("%d\t%,d\t%2.2f%% ", vals + 1, distro[vals],
((double) distro[vals] / totalRolls) * 100);
}
}
}
In this lab, you will implement a LoadedDie class that simulates the roll of a loaded (weighted) die by implementing the following methods. The new LoadedDie class must inherit from the Die class provided. The overloaded constructor takes a set of weights (doubles from 0 to 1: double[]); throw an IllegalArgumentException if the number of sides does not equate to the number of weights or if the cumulative weights do not add up to 1 (or within a strict tolerance) . The no parameter constructor must be overridden to instantiate the super class. A private method that computes the cumulative weighted probabilities as demonstrated in the example table below. The cumulative weighted probability is what is required to assign the value of a rolled die. Face Value Weights (probabilities) Cumulative Weighted Probabilities 0.2 0.3 0.8 0.9 4 0.5 . The roll method will be overridden and rolls the die according to the given weights by referring to the cumulative weighted probabilities. If the no parameter constructor was used to instantiate the LoadedDie class then this override of the roll method must be able to detect there are no weights and roll the Die normally The LoadedDie class should compute and save the cumulative weighted probabilities; it should not save the weights that were passed in to the constructor Also implement a LoadedDieTester class that will contain the main0 method that instantiates then rolls a weighted die at least 10000 times, accumulates the outcomes in an array, and prints the resulting totals for each face value and the percentage of times it appeared. If implemented properly, the results should correspond well with the given weights In this lab, you will implement a LoadedDie class that simulates the roll of a loaded (weighted) die by implementing the following methods. The new LoadedDie class must inherit from the Die class provided. The overloaded constructor takes a set of weights (doubles from 0 to 1: double[]); throw an IllegalArgumentException if the number of sides does not equate to the number of weights or if the cumulative weights do not add up to 1 (or within a strict tolerance) . The no parameter constructor must be overridden to instantiate the super class. A private method that computes the cumulative weighted probabilities as demonstrated in the example table below. The cumulative weighted probability is what is required to assign the value of a rolled die. Face Value Weights (probabilities) Cumulative Weighted Probabilities 0.2 0.3 0.8 0.9 4 0.5 . The roll method will be overridden and rolls the die according to the given weights by referring to the cumulative weighted probabilities. If the no parameter constructor was used to instantiate the LoadedDie class then this override of the roll method must be able to detect there are no weights and roll the Die normally The LoadedDie class should compute and save the cumulative weighted probabilities; it should not save the weights that were passed in to the constructor Also implement a LoadedDieTester class that will contain the main0 method that instantiates then rolls a weighted die at least 10000 times, accumulates the outcomes in an array, and prints the resulting totals for each face value and the percentage of times it appeared. If implemented properly, the results should correspond well with the given weights
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
