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);
}
}
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;
}
}