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

I am having trouble writing a demo driver for a class I wrote. Below I poseted t

ID: 3715742 • Letter: I

Question

I am having trouble writing a demo driver for a class I wrote. Below I poseted the requirements for the demo and the code for the Die class.

DieRollStatistics Program Requirements: The processing done by this DieRollStatistics application program requires two code segments. The first code segment

accumulates the roll statistics for a single Die object and outputs those statistics in a simple report. The second code segment accumulates the roll statistics for two Die objects and outputs those statistics in a simple report. The overall general functionality of this application program is as follows: • The program must define a constant number of 6 sides for the Die objects. • The program must define a Scanner object, two Die objects, and an integer array object that has slots or elements that represent each side of a Die object (i.e. if the Die object has 6 sides, then the array has a size of 6 elements or slots, with each element or slot representing the value on each side of the Die.). For example: array position 0 represents or is associated to the side that contains just a single dimple or what we might call side 1; whereas array position 5 represents the side that contains 6 dimples or what would be called side 6 of the Die object. • The program must prompt for, obtain and validate keyboard entry of the number of times the Die objects are to be rolled. A valid number of rolls for a Die object must be 1 or more.

The basic functionality of the first task in this application program is as follows: • The program must have a single Die object complete the number of rolls that were entered by the user. Each roll iteration must output the side that resulted in being face up after the roll and it must also increment the correct counter in the array for the side that was rolled to face up. Here below is an example of the output: Die one: 5 • When all roll iterations have been completed, then the program will output a simple report of the number of times each side of the Die object was rolled to face up. Remember this is being determined by random number generation, so each execution of the program will produce just a little different statistics on the report. See simple statistics report in example execution output at the bottom of these instructions. The basic functionality of the second task in this application program is as follows: • The program must have both Die objects complete the number of rolls that were entered by the user. • Each roll iteration must have one of the Die objects determine if it holds the same value (is equal) to the second Die object. • If the two Die objects are equal, then a single variable that holds the count of a doubles roll must be incremented by 1. Additionally, the count of times this side has been rolled face up needs to be incremented by 1 (Hint: remember the array elements are serving as the counters for each of the sides of the Die object). • Each roll iteration must output the side of each Die object that resulted in being face up. Additionally, if the sides were equal then add to the output line that a double was rolled. Here below are examples of the two outputs: Die one: 5, Die Two: 6 Die one: 6, Die Two: 6 - Doubles rolled • After all of the second set of iterations has finished executing, there should be output statements to produce a simple report of the number of doubles rolled and the number of times/occurrences that each face value was the value rolled in a doubles roll. See statistics report in example execution output at the bottom of these instructions.

Here is the code for the die class I wrote

import java.util.Random;

public class Die {

private int sides;

private int value;

public Die(int numSides) {

if (numSides < 2) {

sides = 6;

} else {

sides = numSides;

}

}

int roll() {

Random r = new Random();

int gen = r.nextInt(6) + 1;

value = gen;

return value;

}

public int getSides() {

return sides;

}

public int getValue() {

return value;

}

public String toSting() {

String strValue = Integer.toString(value);

return strValue;

}

public boolean equals(Die object1, Die object2) {

if (object1== object2)

return true;

else

return false;

}

}

Explanation / Answer

//Code to copy

//Die.java
import java.util.Random;
public class Die {

   private int sides;
   private int value;

   public Die(int numSides) {
       if (numSides < 2) {
           sides = 6;
       } else {
           sides = numSides;
       }
   }

   int roll() {
       Random r = new Random();
       int gen = r.nextInt(6) + 1;
       value = gen;
       return value;
   }

   public int getSides() {
       return sides;
   }

   public int getValue() {
       return value;
   }

   public String toSting() {
       String strValue = Integer.toString(value);
       return strValue;
   }

   public boolean equals(Die object1, Die object2) {
       if (object1== object2)
           return true;
       else
           return false;
   }

}

//Part1:

//Part1 .Single dice roll and statistics
//for user given number of rolls
//DieRoll.java
import java.util.Scanner;
public class DieRoll1 {
  
   public static void main(String[] args) {
      
       int numRolls;
       final int SIDES=6;
       Scanner keyboard = new Scanner (System.in);
       System.out.print("Enter # of dice roll: ");
       numRolls = Integer.parseInt(keyboard.nextLine());
       String diestr[]={"One" ,"Two","Three","Four","Five","Six"};
       Die die=new Die(SIDES);
      
       //create an array of SIDES=6
       int[] diestats=new int[SIDES];
      
       for (int i = 0; i < numRolls; i++)
       {
           //calling roll
           die.roll();
           System.out.println("Face up: "+die.getValue());
           int side=die.getValue();
           //increment the corresponding side
           diestats[side-1]++;          
       }
       //print statistics
       System.out.printf("%-13s%-10s ","Dice","Count");
       for (int i = 0; i < diestr.length; i++)
       {
           System.out.printf("%s %-10s=%-2d ","Die",diestr[i],diestats[i]);
       }  
   }
}


Sample Output:
Enter # of dice roll: 10
Face up: 6
Face up: 1
Face up: 4
Face up: 1
Face up: 2
Face up: 6
Face up: 3
Face up: 4
Face up: 5
Face up: 6
Dice Count   
Die> Die Two =1
Die Three =1
Die Four =2
Die Five =1
Die Six =3

------------------------------------------------------------------------------------------------------
Part2:

//Part1 .Double rolls dice and statistics
//for user given number of rolls
//DieRoll.java
import java.util.Scanner;
public class DieRoll2 {

   public static void main(String[] args) {

       int numRolls;
       final int SIDES=6;
       Scanner keyboard = new Scanner (System.in);
       System.out.print("Enter # of dice roll: ");
       numRolls = Integer.parseInt(keyboard.nextLine());
       String diestr[]={"One" ,"Two","Three","Four","Five","Six"};

       //Create two die objects
       Die die1=new Die(SIDES);
       Die die2=new Die(SIDES);
       //create an array of SIDES=6
       int[] die1stats=new int[SIDES];
       int[] die2stats=new int[SIDES];

       int[] doublesRolled=new int[SIDES];

       for (int i = 0; i < numRolls; i++)
       {
           //calling roll
           die1.roll();
           die2.roll();
           System.out.println("Die1: "+die1.getValue());
           System.out.println("Die2: "+die2.getValue());
           int side=die1.getValue();
           //increment the corresponding side
           die1stats[side-1]++;
           die2stats[side-1]++;

           if(die1.getValue()==die2.getValue())
           {
               int pos=die1.getValue();
               doublesRolled[pos-1]++;
               System.out.println("Doubles rolled");
           }
       }
       //print statistics
       System.out.println("Die1");
       System.out.printf("%-13s%-10s ","Dice","Count");
       for (int i = 0; i < diestr.length; i++)
       {
           System.out.printf("%s %-10s=%-2d ","Die",diestr[i],die1stats[i]);
       }  
       //print statistics
       System.out.println("Die2");
       System.out.printf("%-13s%-10s ","Dice","Count");
       for (int i = 0; i < diestr.length; i++)
       {
           System.out.printf("%s %-10s=%-2d ","Die",diestr[i],die2stats[i]);
       }  

       //print statistics
       System.out.println("Double Rolls");
       System.out.printf("%-13s%-10s ","Dice","Count");
       for (int i = 0; i < diestr.length; i++)
       {
           System.out.printf("%s %-10s=%-2d ","Dice",diestr[i],doublesRolled[i]);
       }  
   }
}


Sample Output:
Enter # of dice roll: 20
Die1: 6
Die2: 3
Die1: 5
Die2: 6
Die1: 3
Die2: 3
Doubles rolled
Die1: 5
Die2: 6
Die1: 1
Die2: 1
Doubles rolled
Die1: 5
Die2: 4
Die1: 5
Die2: 5
Doubles rolled
Die1: 6
Die2: 1
Die1: 6
Die2: 4
Die1: 4
Die2: 3
Die1: 6
Die2: 1
Die1: 3
Die2: 3
Doubles rolled
Die1: 6
Die2: 2
Die1: 3
Die2: 1
Die1: 1
Die2: 4
Die1: 3
Die2: 6
Die1: 1
Die2: 5
Die1: 4
Die2: 4
Doubles rolled
Die1: 3
Die2: 1
Die1: 1
Die2: 5
Die1
Dice Count   
Die> Die Two =0
Die Three =5
Die Four =2
Die Five =4
Die Six =5
Die2
Dice Count   
Die> Die Two =0
Die Three =5
Die Four =2
Die Five =4
Die Six =5
Double Rolls
Dice Count   
Dice> Dice Two =0
Dice Three =2
Dice Four =1
Dice Five =1
Dice Six =0