Question

Using the PairOfDice class from PP 4.7, design and implement an application that rolls a pair...

Using the PairOfDice class from PP 4.7, design and implement an application that rolls a pair of dice 1000 times, counting the number of box cars (two sixes) that occur.

You're given -

//********************************************************************

// PairOfDice.java Author: Lewis/Loftus

//

// Solution to Programming Project 4.8

//********************************************************************

public class PairOfDice

{

private Die die1, die2;

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

// Creates two six-sided Die objects, both with an initial

// face value of one.

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

public PairOfDice()

{

die1 = new Die();

die2 = new Die();

}

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

// Rolls both dice and returns the combined result.

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

public int roll()

{

return die1.roll() + die2.roll();

}

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

// Returns the current combined dice total.

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

public int getTotalFaceValue()

{

return die1.getFaceValue() + die2.getFaceValue();

}

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

// Returns the current value of the first die.

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

public int getDie1FaceValue()

{

return die1.getFaceValue();

}

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

// Returns the current value of the second die.

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

public int getDie2FaceValue()

{

return die2.getFaceValue();

}

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

// Returns the string representation of this pair of dice.

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

public String toString()

{

return "Die 1: " + die1.getFaceValue() + " Die 2: " +

die2.getFaceValue();

}

}

AND

//********************************************************************

// Die.java Author: Lewis/Loftus

//

// Solution to Programming Project 5.10 and 5.17 and 6.5

//

// Represents one die (singular of dice) with faces showing values

// between 1 and the number of faces on the die.

//********************************************************************

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;

}

}

I'm just really confused on what to do. Please be descriptive in the response it would be much appreciated. Thank you so much in advance!!

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

Note: Could you plz go through this code and let me know if u need any changes in this.Thank You
=================================

// Die.java

//********************************************************************

//Die.java Author: Lewis/Loftus

//

//Solution to Programming Project 5.10 and 5.17 and 6.5

//

//Represents one die (singular of dice) with faces showing values

//between 1 and the number of faces on the die.

//********************************************************************

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;

   }

}

=========================

// PairOfDice.java

//********************************************************************

//PairOfDice.java Author: Lewis/Loftus

//

//Solution to Programming Project 4.8

//********************************************************************

public class PairOfDice

{

   private Die die1, die2;

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

   // Creates two six-sided Die objects, both with an initial

   // face value of one.

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

   public PairOfDice()

   {

       die1 = new Die();

       die2 = new Die();

   }

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

   // Rolls both dice and returns the combined result.

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

   public int roll()

   {

       return die1.roll() + die2.roll();

   }

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

   // Returns the current combined dice total.

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

   public int getTotalFaceValue()

   {

       return die1.getFaceValue() + die2.getFaceValue();

   }

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

   // Returns the current value of the first die.

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

   public int getDie1FaceValue()

   {

       return die1.getFaceValue();

   }

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

   // Returns the current value of the second die.

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

   public int getDie2FaceValue()

   {

       return die2.getFaceValue();

   }

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

   // Returns the string representation of this pair of dice.

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

   public String toString()

   {

       return "Die 1: " + die1.getFaceValue() + " Die 2: " +

       die2.getFaceValue();

   }

}

=================================

// Test.java

public class Test {

   public static void main(String[] args) {
       int cntBoxCars = 0;

       PairOfDice pd = new PairOfDice();

       for (int i = 0; i < 1000; i++) {
           pd.roll();
           if (pd.getDie1FaceValue() == 6 && pd.getDie2FaceValue() == 6) {
               cntBoxCars++;
           }
       }

       System.out.println("No of Box Cars :" + cntBoxCars);

   }

}

==============================

Output:

No of Box Cars :31

=====================Could you plz rate me well.Thank You

Add a comment
Know the answer?
Add Answer to:
Using the PairOfDice class from PP 4.7, design and implement an application that rolls a pair...
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
  • 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;...

  • 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...

  • 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...

  • 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();    }      ...

  • 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...

  • 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() {          ...

  • 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 am currently doing homework for my java class and i am getting an error in...

    I am currently doing homework for my java class and i am getting an error in my code. import java.util.Scanner; import java.util.Random; public class contact {       public static void main(String[] args) {        Random Rand = new Random();        Scanner sc = new Scanner(System.in);        System.out.println("welcome to the contact application");        System.out.println();               int die1;        int die2;        int Total;               String choice = "y";...

  • hey, struggling with this assignment, wondering if if I could get some help. Here is the...

    hey, struggling with this assignment, wondering if if I could get some help. Here is the project details The following lists a Dice class that simulates rolling a die with a different number of sides. The default is a standard die with six sides. The rollTwoDice function simulates rolling two dice objects and returns the sum of their values. The srand function requires including cstdlib. my class ------------------------------------ class Dice { public: Dice(); DIce(int numSides); virtual int rollDice()const; protected: int...

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