Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Please, I need this to be done in Java Eclipse, make sure to add comments. Thank

ID: 3753937 • Letter: P

Question

Please, I need this to be done in Java Eclipse, make sure to add comments. Thank you.

Verify that the dice distribute across the potential rolls evenly. The number of 1s, 2s, 3s, 4s, 5s, and 6s should be relatively the same. Since we are rolling the dice using the random generator, they usually will not be the same. So our test has to check for "outliers". Outliers are results that fall outside normal expectations. So what is a normal expectation? Well when dealing with random numbers, we are dealing with probabilities and there is a concept called a normal distribution, a bell curve.

Our testing methodology is to roll randomly and keep track of how many times a side is rolled. We can use a concept of "buckets" to contain the count. So, for example, after a roll of 2, we would increment the 2 bucket. The next roll of 4 (again random result) would increment the 4 bucket. At the end of the rolls buckets 1-6 will each have a total. The sum of the buckets must equal the number of rolls and each bucket total should be within 1 standard deviation from the mean (average)

There is a bug in the provided Die.java. You need to identify it with your tests, and fix it. Turn in the corrected Die.java

//Steps

Test the Die.java class thoroughly
Create a JUnitTest named Week05JUnitTest.java with the following two tests

testMultipleDie() and testRandomDie()

testMultipleDie() will create multiple Die instances and verify the rolls are unique (that the group doesn't roll to the same value).
Use the transitive property to test.

testRandomDie() will run a large number of tests (at least 1000) and check that each facet (1-6) has a value (non-zero) count, calculate the mean and standard deviation, and verify that the counts are evenly distributed within two standard deviations. Standard Deviation

Your output must be in the following format:
3 lines
First line is Counts: bucket 1 total bucket 2 total bucket 3 total bucket 4 total bucket 5 total bucket 6 total
Second line is Distribution: decimal per bucket
Third line: mean: value, std dev: value

An example output for an unsuccessful run is:

Counts: 173 161 174 158 151 0
Distribution: 0.17 0.16 0.17 0.16 0.15 0.00
mean: 136.00, std dev: 61.43
Die failed to be randomly distributed
A facet has a zero count

An example output for a successful run is:
Counts: 166 175 158 169 169 163
Distribution: 0.17 0.18 0.16 0.17 0.17 0.16
mean: 166.00, std dev: 5.35

Which means the following
Side one: 166
Side two: 175
Side three: 158
Side four: 169
Side five: 169
Side six: 163

The distribution is the percentage

which means
Side one: 17%
Side two: 18%
Side three: 16%
Side four: 17%
Side five: 17%
Side six: 16%

The mean and std deviation are as-is

The final test works like the following:
Side one count: 166 is between 2 standard deviations below and 2 above.
Same test for each facet

Support Info:

How to find Standard Deviation

Calculate the Mean (Links to an external site.)Links to an external site. of your data set.

For each number: subtract the Mean and square the result.

Calculate the Mean of those squared differences. The result is called the Variance (Links to an external site.)Links to an external site..

Calculate the square root of the Variance. The result is the Standard Deviation.

/*********************Die.java****************/

package week05;

import java.util.Random;

/**

* This class takes a constructor parameter to determine if it should roll or not

*

* @author Dina

*

*/

public class Die

{

    /**

     * Constructor

     *

     * @param roll

     *            true to roll die, otherwise initialize to NO_NUMBER;

     */

    public Die(boolean roll)

    {

        random = new Random();

        if(roll)

        {

            roll();

        }

        else

        {

            number = NO_NUMBER;

        }

    }

    // Rolls the dice

    public void roll()

    {

        number = random.nextInt(MAX_NUMBER - MIN_NUMBER + 1);

    }

    // Returns the number on this dice

    public int getNumber()

    {

        return number;

    }

    // Data Members

    // the largest number on a dice

    private static final int MAX_NUMBER = 6;

    // the smallest number on a dice

    private static final int MIN_NUMBER = 1;

    // to represent a dice that is not yet rolled

    private static final int NO_NUMBER = 0;

    private int number;

    private Random random;

}

//**************************TestHarness******************************/

package week05;

import java.util.List;

import org.junit.runner.Result;

import org.junit.runner.notification.Failure;

/**

* This class executes the JUnit Test specified from the command line

* This will be used by the reference system for testing your code.

*

* @author Dina

*

*/

public class TestHarness

{

               public static void main(String[] args)

               {

                              Result result = org.junit.runner.JUnitCore.runClasses(Week05JUnitTest.class);

                              int failCount = result.getFailureCount();

                              if( failCount > 0 )

                              {

                                             List<Failure> failures = result.getFailures();

                                             for(Failure fail : failures)

                                             {

                                                            trace("FAILED: " + fail.getMessage());

                                             }

                              }

                              else

                              {

                                             trace("SUCCESS");

                              }

               }

               private static void trace(String msg)

               {

                              System.out.println(msg);

               }

}

Explanation / Answer

Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts. Thanks

// Die.java

package week05;

import java.util.Random;

public class Die {

      /**

      * Constructor

      *

      * @param roll

      *            true to roll die, otherwise initialize to NO_NUMBER;

      */

      public Die(boolean roll) {

            random = new Random();

            if (roll) {

                  roll();

            } else {

                  number = NO_NUMBER;

            }

      }

      // Rolls the dice

      public void roll() {

            //corrected the code to add MIN_NUMBER to the random value

            number = random.nextInt(MAX_NUMBER - MIN_NUMBER + 1)+MIN_NUMBER;

      }

      // Returns the number on this dice

      public int getNumber() {

            return number;

      }

      // Data Members

      // the largest number on a dice

      private static final int MAX_NUMBER = 6;

      // the smallest number on a dice

      private static final int MIN_NUMBER = 1;

      // to represent a dice that is not yet rolled

      private static final int NO_NUMBER = 0;

      private int number;

      private Random random;

}

// Week05JUnitTest.java

package week05;

import static org.junit.Assert.*;

import org.junit.Test;

public class Week05JUnitTest {

      @Test

      public void testMultipleDie() {

            // creating 4 dice, rolling them during creation

            Die die1 = new Die(true);

            Die die2 = new Die(true);

            Die die3 = new Die(true);

            Die die4 = new Die(true);

            // getting rolled values of these 4 numbers

            int num1 = die1.getNumber();

            int num2 = die2.getNumber();

            int num3 = die3.getNumber();

            int num4= die4.getNumber();

            // ensuring that these 4 numbers are not same

            assertFalse("Four dice rolled, got same values", (num1 == num2)

                        && (num2 == num3) &&(num3==num4));

      }

      @Test

      public void testRandomDie() {

            // creating a Die, not rolling them during creation

            Die die = new Die(false);

            // creating an array to store counts of each faces

            // count[i] stores occurrences of i

            // index 0 is left empty

            int counts[] = new int[7];

            int n = 1000; // number of runs

            // rolling for n times, incrementing proper counters

            for (int i = 0; i < n; i++) {

                  die.roll();

                  int number = die.getNumber();

                  counts[number]++;

            }

            // displaying the counts

            System.out.print("Counts: ");

            boolean zeroCount = false; // a flag variable to denote if a count is 0

            double sum = 0, mean = 0, sd = 0, sumMeanSquare = 0, variance = 0;

            //looping through the counts

            for (int i = 1; i <= 6; i++) {

                  //displaying the count

                  System.out.print(counts[i] + " ");

                  //checking if the count is zero

                  if (counts[i] == 0) {

                        //zero count

                        zeroCount = true;

                  }

                  //adding to the sum

                  sum += counts[i];

            }

            System.out.print(" Distribution: ");

            //finding the mean

            mean = sum / 6.0;

            //looping through the counts again

            for (int i = 1; i <= 6; i++) {

                  //finding the square of difference between this number and mean

                  //adding to the sumMeansSquare

                  sumMeanSquare += Math.pow(counts[i] - mean, 2);

                  //finding and displaying the distribution value

                  double distribution = counts[i] / sum;

                  System.out.printf("%.2f ", distribution);

            }

            //finding variance

            variance = sumMeanSquare / 6.0;

            //finding standard deviation

            sd = Math.sqrt(variance);

            //displaying mean and sd

            System.out.printf(" mean: %.2f, std dev: %.2f ", mean, sd);

            //if any value is zero, test is failed

            if (zeroCount) {

                  fail("Die failed to be randomly distributed "

                              + "A facet has a zero count");

            }

            //ensuring sum equals n

            assertTrue("Sum of counts not equal to total number of rolls", sum == n);

            //finding the minimum and maximum ranges

            double min = mean - 2 * sd;

            double max = mean + 2 * sd;

            //ensuring that all values are within this range

            for (int i = 1; i <= 6; i++) {

                  assertTrue(

                              "Each bucket total should be within two standard deviations"

                                          + " from the mean", counts[i] >= min

                                          && counts[i] <= max);

            }

      }

}

/*OUTPUT*/

Counts: 172 174 176 161 167 150

Distribution: 0.17 0.17 0.18 0.16 0.17 0.15

mean: 166.67, std dev: 8.94

SUCCESS