Question: Could i get help on fixing this code? I am confused on overloading a constructor. Die.java import java.util.Random; /** * Die.java * * Represents one

Could i get help on fixing this code? I am confused on overloading a constructor.

Could i get help on fixing this code? I am confused on

Die.java

import java.util.Random;

/** * Die.java * * Represents one die (singular of dice) with faces showing values * between 1 and 6. * * @author Java Foundations * @author CS121 Instructors (modified a few things from book) */ public class Die { private final int MAX = 6; // maximum face value private int faceValue; // current value showing on the die private Random rand;

/** * Constructor: Sets the initial face value of this die. */ public Die() { rand = new Random(); faceValue = 1; }

/** * Computes a new face value for this die and returns the result. * @return The new face value. */ public int roll() { //faceValue = (int)(Math.random() * MAX) + 1; faceValue = rand.nextInt(MAX) + 1; return faceValue; }

/** * Face value mutator. The face value is not modified if the * specified value is not valid. * * @param value The new face value. Must be between 1 and max face * value. */ public void setFaceValue (int value) { if (value > 0 && value

/** * Face value accessor. * @return The current face value. */ public int getFaceValue() { return faceValue; }

/** * Returns a string representation of this die. */ public String toString() { String result = "Die [faceValue = " + faceValue + "]"; return result; }

}

SnakeEyes.java

import java.util.Scanner; public class SnakeEyes { /** * Creates two Die objects and rolls them several times, counting * the number of snake eyes that occur. * * @param args (unused) */ public static void main (String[] args) { Scanner in = new Scanner(System.in); final int ROLLS = 500; int num1, num2, count = 0; int numberOfSides; System.out.println("How many sides per die: "+ numberOfSides()); Die die1 = new Die(); Die die2 = new Die();

for (int roll = 1; roll

System.out.println ("Number of rolls: " + ROLLS); System.out.println ("Number of snake eyes: " + count); System.out.println ("Ratio: " + (double)count / ROLLS); } }

3. Add an overloaded constructor We want to be able to create Die objects that have any (positive) number of sides (a) Add a second, overloaded constructor that wil take an argument for the numbeir of sided of the Die class should have. The constructor will have public visibility modifier and the following header: Die (int numberOfSides) (b) The old MAX constant will no longer reflect the actual maximum face value for every Die we can create. It needs to be replaced by a new instance variable that will store the actual number of sides the Die object has. You will need to initialize this variable in the constructor and use it wherever MAX was originally used in other Die methods. The original default constructor (no arguments) should still initialize the number of sides to6 (c) Modify SnakeEyes to prompt the user to enter the number of sides the both die objects should have. Make sure to do it before creating the Die objects. (You only need to ask once - both die will have the same number of sides) How many sides per die? 6 Pass the input value to the new constructor to create two Die objects with the specified number of sides

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!