Implement a class used to simulate a die singular of dice Yo

Implement a class used to simulate a die (singular of dice). Your class should have two data fields, one to store the number of sides on your die and another to store which side (value) is currently facing up. Your class should have two constructors - one that takes zero arguments and creates a standard 6-sided die and a second constructor that takes a single argument The argument should allow the user to create a die that has 4, 6, 8, 10, 12, 20 or 100 sides; if the user enters any other value then the die should default to 6 sides. Both contractors should leave the side-up data field with a random (but valid) value. Your class should have a single accessor (get Valued) that return the current value facing up. Your class should have a single void mutator method (roll()) that randomly rolls the die - make use of your number of sides data field here. Finally, add a toString() (see chapter 8!)method to make it easy to print out the state of your die objects. Write a complete tester program to show that your die class is fully functional. In particular you should create at least two dice, one with each constructor (get user input for the constructor that takes an argument, don\'t do any input checking). Then roll them independently singly and using a loop. Also, since many games use more than one die show the sum of all your dice objects. ***Write JavaDoc comments for this class.*** Add the following to your tester class. Create five more six-sided dice and use nested loops (for loop and while loop?) to determine the average number of rolls it take to get a Yahtzee (all 5 dice with the same value). Keep rolling the dice until you achieve a Yahtzee keep track of the number of rolls it took to get it. Once you have done this, wrap it in a loop that repeats this process 1000 times Display the maximum minimum and average number of rolls it takes lo gel a Yahtzee The output below should be in addition to the output shown above.

Solution

Die.java

package com.chegg.dice;

import java.util.Random;
import java.util.Scanner;

public class Die {
   int numberOfSides;
   int valueShowingUp;
  
  
  
   Die(){
       Random random1=new Random();
       numberOfSides=6;
       valueShowingUp=random1.nextInt(numberOfSides);
       return;
   }
   Die(int number){
       Random random2=new Random();
       numberOfSides=number;
       valueShowingUp=random2.nextInt(number);
       return;
   }
   public int getValue(){
          
       return valueShowingUp;
   }
   public void roll(){
      
       getValue();
   }
}

********************************************************************************************************

DiceTester.java

package com.chegg.dice;

import java.util.Scanner;

public class DiceTester{
   static int sides;
   static Die die1=null;
   static Die die2=null;
  
   public static void main(String[] args) {
       // TODO Auto-generated method stub
       int side_die1;
       int side_die2;
       int sum;
       Scanner scn=new Scanner(System.in);
       System.out.println(\"Creating two dice: One with 6 sides. Enter the number of sides for the second one: \");
       sides=scn.nextInt();

       if(sides!=4&&sides!=6&&sides!=8&sides!=10&&sides!=12&&sides!=20&&sides!=100){
           sides=6;
       }
       die1=new Die();
       die2=new Die(sides);
       System.out.println(\"Die[6 sides, value=\"+die1+\"]\");
       System.out.println(\"Die[\"+sides+\" sides, value=\"+die2+\"]\");
      
       System.out.println(\"Rolling each separately and printing the values:\");
       side_die1=die1.getValue();
       side_die2=die2.getValue();
       sum=die1.getValue()+die2.getValue();
       System.out.println(\"Die 1 showing:\"+side_die1);
       System.out.println(\"Die 2 showing:\"+side_die2);
       System.out.println(\"Sum of the dice:\"+sum);
       System.out.println(\"for loop to roll the two dice 10 times and show the values and sum:\");
       System.out.println(\"Die1   Die2   Sum\");
       for(int i=1;i<=10;i++){
           Die d1=new Die();
           Die d2=new Die(sides);
           int x=d1.getValue();
           int y=d2.getValue();
           int z=x+y;
           System.out.print(x+\"\\t\");
           System.out.print(y+\"\\t\");
           System.out.print(z);
           System.out.println();
          
       }
  
      
      
      
   }

}

 Implement a class used to simulate a die (singular of dice). Your class should have two data fields, one to store the number of sides on your die and another t
 Implement a class used to simulate a die (singular of dice). Your class should have two data fields, one to store the number of sides on your die and another t

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site