Question: Simple JAVA Program Please Answer the bold italicized parts. //-----------Start below here. To do: approximate lines of code = 1 // Write the class Die
Simple JAVA Program
Please Answer the bold italicized parts.
//-----------Start below here. To do: approximate lines of code = 1
// Write the "class Die ... " part so that Comparable is implemented, but leave out the public specifier since only one class in the file can be public.
//-----------------End here. Please do not remove this comment. Reminder: no changes outside the todo regions.
{
private Random random ;
private int sides ;
private int sideUp ;
/**
Constructs a die with a given number of sides
@param sides the number of sides, e.g., 6 for normal die
*/
public Die(int sides)
{
this.sides = sides ;
random = new Random() ;
sideUp = 0 ;
}
/**
Constructs a die with a given number of sides and gives
the random object
@param sides the number of sides, e.g., 6 for normal die
@param random the random number generator
*/
public Die(int sides, Random random)
{
this.sides = sides ;
this.random = random ;
sideUp = 0 ;
}
/**
Simulates a throw of a die
@return the face of the die
*/
public int cast()
{
sideUp = 1 + random.nextInt(sides) ;
return sideUp ;
}
/**
Gives a String representation of the Die
@return the sideUp as a string
*/
public String toString()
{
return "" + sideUp ;
}
/**
Compares this die with another in form of Comparable interface.
The sorting is from small values to larger values.
@return 1 if this one has a bigger value, -1 if smaller, 0 if same
*/
//-----------Start below here. To do: approximate lines of code = 4
// 1. public signature ;
//2. code for the -1 case ;
//3. code for the 1 case ;
//4. code for the 0 case.
//-----------------End here. Please do not remove this comment. Reminder: no changes outside the todo regions.
}
===============================================================================================
2.
public class DieReview
{
public static void main(String[] args)
{
Random random = new Random(1) ;
Die die = new Die(6, random) ;
//-----------Start below here. To do: approximate lines of code = 3
// 1. make an arraylist called values to hold Integer objects
//2. put into the arraylist 3, 2, 5, 1, 7 ;
values.add(2) ; //
values.add(5) ; //
values.add(1) ; //
values.add(7) ; //
//3. for each value in values
//-----------------End here. Please do not remove this comment. Reminder: no changes outside the todo regions.
if (value != 1)
System.out.print("Here are " + value + " throws: ") ;
else
System.out.print("Here is 1 throw: ") ;
//-----------Start below here. To do: approximate lines of code = 2
// 4. for i from 1 to value
//5. throw the die and save the number returned as n
//-----------------End here. Please do not remove this comment. Reminder: no changes outside the todo regions.
System.out.print(n + " ") ;
}
System.out.println() ;
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
