Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Answer needs to be in JAVA Using the Die class, design and implement a new class called PairOfDice, which uses two Die objects. Include methods
Answer needs to be in JAVA
Using the Die class, design and implement a new class called PairOfDice, which uses two Die objects. Include methods to set and get the individual die values, a method to roll the dice, and a method that returns the current sum of the two die values. Rewrite the SnakeEyes program below using a PairOfDice object.
public class ClassesNeeded { //Note: you should not need to change the Die class. public static class Die { private final int MAX = 6; private int faceValue; public Die() { faceValue = 1; } public int roll() { faceValue = (int)(Math.random() * MAX) + 1; return faceValue; } public void setFaceValue(int value) { if(value > 0 && value <= MAX) faceValue = value; } public int getFaceValue() { return faceValue; } public String toString() { String result = Integer.toString(faceValue); return result; } } public static class PairOfDice { //TODO: complete me. } public static void main(String[] args) { final int ROLLS = 500; int count = 0; //TODO: initialize the pair of dice method for(int roll = 1; roll <= ROLLS; roll++) { //TODO: roll the die pair and count the number of snake eyes. // snake eyes occur when both dice roll one. } System.out.println("Number of rolls: " + ROLLS); System.out.println("Number of snake eyes: " + count); System.out.println("Ratio: " + (double)count / ROLLS); } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started