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

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

ID: 3853709 • Letter: C

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 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 <= MAX) {
faceValue = 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 <= ROLLS; roll++)
{
num1 = die1.roll();   
num2 = die2.roll();

//print the value of die1 and die2
System.out.println("roll " + roll);

System.out.println("die1 value: " + die1.getFaceValue());
System.out.println("die2 value: " + die2.getFaceValue());

System.out.println();

if (num1 == 1 && num2 == 1) // check for snake eyes
count++;
}

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

Explanation / Answer

Modified file 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 int MAX; // face value

private int faceValue; // current value showing on the die

private Random rand;

/**

* Constructor: Sets the initial face value and MAX of this die.

*/

public Die()

{

rand = new Random();

faceValue = 1;

MAX = 6;

}

/**

* Overloaded Constructor

*/

public Die(int numberOfSides)

{

rand = new Random();

faceValue = 1;

MAX = numberOfSides;

}

/**

* 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 <= MAX) {

faceValue = 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;

}

}

Modified file SnakeEyes.java

package die.demo;

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.print("How many sides per die: ");
numberOfSides = Integer.parseInt(in.nextLine());
  
Die die1 = new Die(numberOfSides);
Die die2 = new Die();
for (int roll = 1; roll <= ROLLS; roll++)
{
num1 = die1.roll();   
num2 = die2.roll();

//print the value of die1 and die2
System.out.println("roll " + roll);

System.out.println("die1 value: " + die1.getFaceValue());
System.out.println("die2 value: " + die2.getFaceValue());

System.out.println();

if (num1 == 1 && num2 == 1) // check for snake eyes
count++;
}
System.out.println ("Number of rolls: " + ROLLS);
System.out.println ("Number of snake eyes: " + count);
System.out.println ("Ratio: " + (double)count / ROLLS);
}
}

Modified file 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 int MAX; // face value

private int faceValue; // current value showing on the die

private Random rand;

/**

* Constructor: Sets the initial face value and MAX of this die.

*/

public Die()

{

rand = new Random();

faceValue = 1;

MAX = 6;

}

/**

* Overloaded Constructor

*/

public Die(int numberOfSides)

{

rand = new Random();

faceValue = 1;

MAX = numberOfSides;

}

/**

* 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 <= MAX) {

faceValue = 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;

}

}