Question

Using the Die class defined in this chapter, design and implement a class called PairOfDice, composed...

Using the Die class defined in this chapter, design and implement a class called PairOfDice, composed of two Die objects. Include methods to set and get the individual die values, a method to roll the dice, and a method that returns the current sum of the two die values. Create a driver class called RollingDice2 to instantiate and use a PairOfDice object.

I need the main method or test class

public class Die
{
private final int MAX = 6; // maximum face value

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

//-----------------------------------------------------------------
// Constructor: Sets the initial face value.
//-----------------------------------------------------------------
public Die()
{
faceValue = 1;
}

//-----------------------------------------------------------------
// Rolls the die and returns the result.
//-----------------------------------------------------------------
public int roll()
{
faceValue = (int)(Math.random() * MAX) + 1;

return faceValue;
}

//-----------------------------------------------------------------
// Face value mutator.
//-----------------------------------------------------------------
public void setFaceValue (int value)
{
faceValue = value;
}

//-----------------------------------------------------------------
// Face value accessor.
//-----------------------------------------------------------------
public int getFaceValue()
{
return faceValue;
}

//-----------------------------------------------------------------
// Returns a string representation of this die.
//-----------------------------------------------------------------
public String toString()
{
String result = Integer.toString(faceValue);

return result;
}
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Code: public class PairOfDice //creating the objects private Die d1 new Die (); private Die d2 new Die () //gettina first diepublic String toStrina) String result = Integer. t String getValue()); return result; public class RollingDice2 //main methodcode:

public class PairOfDice

{

     //creating the objects

     private Die d1 = new Die();

     private Die d2 = new Die();

     //getting first die value

     public void initial_value(int value)

     {

          d1.setFaceValue(value);

     }

    

     //getting second die value

     public void nextl_value(int value)

     {

          d2.setFaceValue(value);

     }

    

     //returns first die value

     public int Get_First_Value()

     {

          return d1.getFaceValue();

     }

    

     //returns second die value

     public int Get_Second_Value()

     {

          return d2.getFaceValue();

     }

    

     //sum up the vlue of first die and second die

     public int getValue()

     {

          return Get_First_Value() + Get_Second_Value();

     }

    

     //method to roll

     public int RollTheDie()

     {

          return d1.RollTheDie() + d2.RollTheDie();

     }

    

    

public String toString()

     {

          String result = Integer.toString(getValue());

         

          return result;

     }

}

public class RollingDice2

{

     //main method

     public static void main(String[] args)

     {

          //to create an object

         PairOfDice myDice = new PairOfDice();

         System.out.println("Rolling the dice...");

         myDice.RollTheDie();

        

         System.out.print("getting first die value " + myDice.Get_First_Value());

         System.out.println(" getting second die value " + myDice.Get_Second_Value() + ".");

         System.out.println("sum of value " + myDice.toString() + ".");

        

         System.out.println("setting values to maximum number of die.");

         myDice.initial_value(6);

         myDice.nextl_value(6);

         System.out.println("value is " + myDice.toString() + "!");

        

     }

}

Add a comment
Know the answer?
Add Answer to:
Using the Die class defined in this chapter, design and implement a class called PairOfDice, composed...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I need help with these Java programming assignements. public class Die { //here you declare your...

    I need help with these Java programming assignements. public class Die { //here you declare your attributes private int faceValue; //operations //constructor - public Die() { //body of constructor faceValue=(int)(Math.random()*6)+1;//instead of 1, use random approach to generate it } //roll operation public void roll() { faceValue=(int)(Math.random()*6)+1; }    //add a getter method public int getFaceValue() { return faceValue; }    //add a setter method public void setFaceValue(int value) { faceValue=value; } //add a toString() method public String toString() { String...

  • Hello, Could you please help me code this program in Java? It is important that all...

    Hello, Could you please help me code this program in Java? It is important that all rules are carefully followed. Using the PairOfDice class from PP 4.9, design and implement a class to play a game called Pig. In this game, the user competes against the computer. On each turn, the current player rolls a pair of dice and accumulates points. The goal is to reach 100 points before your opponent does. If, on any turn, the player rolls a...

  • Using the Die class in chapter 4, write a class called PairOfDice, composed of two Die...

    Using the Die class in chapter 4, write a class called PairOfDice, composed of two Die object. Include methods to set and get the individual die values, a method to roll the dice, and a method that returns the current sum of the two die values. Create a driver class called RollingDice2 to instantiate and use a PairOfDice object.

  • Please Help! I need to update the code below to meet these criteria! 1. The constructor...

    Please Help! I need to update the code below to meet these criteria! 1. The constructor of die class initializes face of Die to 3 2. roll method updates face of die to value from 1-6 3. getFace() method provides current face to the main program 4. main create a Die object 5. main create a Die object 6. main rolls die 5 times 7. main computes sum of rolls 8. main computes actual average of the rolls <--------- Code...

  • WRITING METHODS 1. Implement a method named surface that accepts 3 integer parameters named width, length,...

    WRITING METHODS 1. Implement a method named surface that accepts 3 integer parameters named width, length, and depth. It will return the total surface area (6 sides) of the rectangular box it represents. 2. Implement a method named rightTriangle that accepts 2 double arameters named sideA and hypotenuseB. The method will return the length of the third side. NOTE To test, you should put all of these methods into a ‘MyMethods’ class and then write an application that will instantiate...

  • I've created a Card class and I'm asked to implement a class called DeckOfCards that stores...

    I've created a Card class and I'm asked to implement a class called DeckOfCards that stores 52 objects of the Card class. It says to include methods to shuffle the deck, deal a card, and report the number of cards left in the deck, and a toString to show the contents of the deck. The shuffle methods should assume a full deck. I also need to create a separate driver class that first outputs the populated deck to prove it...

  • Include an unfair or biased dice. dice doesnt have equal opportunities (in regular dice it is...

    Include an unfair or biased dice. dice doesnt have equal opportunities (in regular dice it is 1/6) DONT implement unbiased roll, reorganize the program to include BiasedDicePair class, add the new class to this project. PairOfDiceDriver.java public class PairOfDiceDriver {    public static void main(String[] args) {        PairOfDice obj = new PairOfDice();        obj.roll();        System.out.println(obj);    } } PairOfDice.java public class PairOfDice {    private int face1, face2;    public PairOfDice() {          ...

  • Make a public class called ManyExamples and in the class... Write a function named isEven which...

    Make a public class called ManyExamples and in the class... Write a function named isEven which takes in a single int parameter and returns a boolean. This function should return true if the given int parameter is an even number and false if the parameter is odd. Write a function named close10 which takes two int parameters and returns an int. This function should return whichever parameter value is nearest to the value 10. It should return 0 in the...

  • In this practical task, you need to implement a class called MyTime, which models a time...

    In this practical task, you need to implement a class called MyTime, which models a time instance. The class must contain three private instance variables: hour, with the domain of values between 0 to 23. minute, with the domain of values between 0 to 59. second, with the domain of values between 0 to 59. For the three variables you are required to perform input validation. The class must provide the following public methods to a user: MyTime() Constructor. Initializes...

  • Create Junit tests for the following classes public class Dice {    private Die die1;    private Die die2;    public Dice()    {        this.die1 = new Die();        this.die2 = new Die();    }    pu...

    Create Junit tests for the following classes public class Dice {    private Die die1;    private Die die2;    public Dice()    {        this.die1 = new Die();        this.die2 = new Die();    }    public Dice(int[] programmedRolls)    {        int[] programmableroll = programmedRolls;        this.die1 = new Die(programmableroll);        this.die1 = new Die(programmableroll);           }       public Dice(Die die1, Die die2)    {        this.die1 = die1;        this.die2 = die2;    }       public void roll() {        die1.roll();        die2.roll();    }       public int getDie1Value() {        return die1.getLastRoll();    }       public int getDie2Value() {        return die2.getLastRoll();    }      ...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT