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

write a class called Dice which can be used to create dice for use in any applic

ID: 3537188 • Letter: W

Question

write a class called Dice which can be used to create dice for use in any applications


Here are descriptions of all the members that you need to implement in the Dice class:


private int die1    -stores one of the two dies

private int die2    -stores the other die

public Dice()    -constructor which returns a new pair of dice, initialized to 1 and 1

public void roll()    -rolls the dice (randomly) to change the values of the two dies

public int getTotal()    -Returns the total value of the two dies

public String toString()    -returns a string representing the current state of the dice like this: %u201C(die1, die2)%u201D

public int compareTo(Dice rhs)   -returns -1 if total of this Dice object is less than total of rhs, or +1 if total of this is greater than total of rhs, or 0 if totals are the same


Do not implement any other members in the Dice class. You have to exactly follow the above specifications.


Then write a test program (TestDice.java) of your own to make sure that all implemented methods as specified above are properly behaved before you use the Dice class for the NewCraps game next.


Below is the Craps program. Rename the Craps program as NewCraps and modify it to use the Dice class.


//----------------------------------------------------------------------

// File: Craps.java

//

// Purpose: Plays a single game of the craps dice game.

//----------------------------------------------------------------------

import java.util.Scanner;

public class Craps {


public static void main(String[] args) {

int die1, die2, total, point;

Scanner scan = new Scanner(System.in);


die1 = (int)(Math.random() * 6) + 1;

die2 = (int)(Math.random() * 6) + 1;

total = die1 + die2;

System.out.println("You rolled " + total + " (" + die1 + "," + die2 + ")");

if (total == 7 || total == 11)

   System.out.println("You win!");

else if (total == 2 || total == 3 || total == 12)

   System.out.println("You lose!");

else {

   System.out.println("The point is " + total);

   point = total;


do

{

System.out.print("Hit enter to roll:");

   scan.nextLine();

   die1 = (int)(Math.random() * 6) + 1;

   die2 = (int)(Math.random() * 6) + 1;

   total = die1 + die2;

   System.out.println("You rolled " + total + " (" + die1 + "," + die2 + ")");

} while (total != point && total != 7);


   if (total == 7)

System.out.println("You lose!");

   else

System.out.println("You win!");

  }

} // main

} // class Craps


Here are some samples of how it should run:


Sample Run #1:


You rolled 7 (4,3)

You win!


Sample Run #2:


You rolled 3 (2,1)

You lose!


Sample Run #3:


You rolled 8 (3,5)

The point is 8

Hit Enter key to roll again:

You rolled 9 (3,6)

Hit Enter key to roll again:

You rolled 10 (5,5)

Hit Enter key to roll again:

You rolled 10 (4,6)

Hit Enter key to roll again:

You rolled 7 (2,5)

You lose!


Sample Run #4:


You rolled 4 (2,2)

The point is 4

Hit Enter key to roll again:

You rolled 12 (6,6)

Hit Enter key to roll again:

You rolled 4 (1,3)

You Win!

Explanation / Answer

import java.util.Scanner;

class Dice
{
private int die1; //             -stores one of the two dies
private int die2;   //           -stores the other die
public Dice() //                -constructor which returns a new pair of dice, initialized to 1 and 1
{
die1 = 1;
die2 = 1;
}
public void roll()        //   -rolls the dice (randomly) to change the values of the two dies
{
die1 = (int)(Math.random() * 6) + 1;
die2 = (int)(Math.random() * 6) + 1;
}
public int getTotal() //   -Returns the total value of the two dies
{
return (die1+die2);
}
public String toString()   //   -returns a string representing the current state of the dice like this: %u201C(die1, die2)%u201D
{
return "(" + die1 + "," + die2 + ")";
}

public int compareTo(Dice rhs)   //-returns -1 if total of this Dice object is less than total of rhs, or +1 if total of this is greater than  
{
if(getTotal() < rhs.getTotal())
return -1;
else if(getTotal() < rhs.getTotal())
return 1;
return 0;
}
}

public class NewCraps
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
Dice D1 = new Dice();
Dice D2 = new Dice();
D1.roll();
int point, total;
total = D1.getTotal();
System.out.println("You rolled " + total + D1.toString());
if (total == 7 || total == 11)
    System.out.println("You win!");
else if (total == 2 || total == 3 || total == 12)
    System.out.println("You lose!");
else {
    System.out.println("The point is " + total);
    point = total;
        do
        {
        System.out.print("Hit enter to roll:");
    scan.nextLine();
    D2.roll();
   System.out.println("You rolled " + D2.getTotal() + D2.toString());
        } while (D2.compareTo(D1)!=0 && D2.getTotal() != 7);
    if (D2.getTotal() == 7)
System.out.println("You lose!");
    else
System.out.println("You win!");
}
    } // main
} // class Craps