Question

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.

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

public class Die
{

private final int MAX = 6;
private int faceValue;
public Die()
{
faceValue = 1;
}
public int roll()
{
faceValue = (int)(Math.random() * MAX) + 1;
return faceValue;
}
public void setFaceValue (int value)
{
faceValue = value;
}
public int getFaceValue()
{
return faceValue;
}
public String toString()
{
String result = Integer.toString(faceValue);
return result;
}
}
// PairOfDice class begins
public class PairOfDice {
Die die1;
Die die2;
{
die1 = new Die ();
die2 = new Die ();// method to set and get individual die values
die1.setFaceValue(1);
die1.getFaceValue();
die2.setFaceValue(6);
die2.getFaceValue();// method to roll the dice
die1.roll();
die2.roll();
// method that returns current sum of the two die values
int sum = die1.getFaceValue() + die2.getFaceValue();
System.out.println("The sum of these two dice is: " + sum + ".");
// RollingDice2 class begins
class RollingDice2 {
public void main (String [] args)
{
PairOfDice dice = new PairOfDice ();

System.out.println(dice);
}
}
}
}

Add a comment
Know the answer?
Add Answer to:
Using the Die class in chapter 4, write a class called PairOfDice, composed of two Die...
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;...

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

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

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

  • C+++ Program Part A Create a class called die (the singular of dice). The die class...

    C+++ Program Part A Create a class called die (the singular of dice). The die class should have a single data member called value. The die constructor should initialize the die’s value to 1. Create a member function called roll() that gives the die a random value between 1 and 6. Create a member function called show() that displays value. In main, demonstrate the creation of a die object to validate it is working. Part B Revise main to count...

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

  • Write a class called Point that contains two doubles that represent its x- and y-coordinates. It...

    Write a class called Point that contains two doubles that represent its x- and y-coordinates. It should have get and set methods for both fields. It should have a constructor that takes two double parameters and initializes its coordinates with those values. It should have a default constructor that initializes both coordinates to zero. It should also contain a method called distanceTo that takes as a parameter another Point and returns the distance from the Point that was passed as...

  • Build a class called Account.java. This class should have the following properties: AcctNo, Owner and balance....

    Build a class called Account.java. This class should have the following properties: AcctNo, Owner and balance. Also add the appropriate set and get methods, display method and main method. Main() should be used to test this class. In main() instantiate an Account object, fill it with data using the set methods, then call display to display the data.

  • Write a class called Course_xxx that represents a course taken at a school. Represent each student...

    Write a class called Course_xxx that represents a course taken at a school. Represent each student using the Student class from the Chapter 7 source files, Student.java. Use an ArrayList in the Course to store the students taking that course. The constructor of the Course class should accept only the name of the course. Provide a method called addStudent that accepts one Student parameter. Provide a method called roll that prints all students in the course. Create a driver class...

  • ( Java Programming ) Write an application to simulate the rolling of two dice. The application...

    ( Java Programming ) Write an application to simulate the rolling of two dice. The application should use an object of class Random once to roll the first die and again to roll the second die. The sum of the two values should then be calculated. Each die can show an integer value from 1 to 6, so the sum of the values will vary from 2 to 12, with 7 being the most frequent sum, and 2 and 12...

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