In: Computer Science
JAVA PRACTICE PROBLEMS [ Thumbs up and good review guaranteed if code is correct! ]
PART I: Implement a method comboDie that takes two dice parameters. The method returns a die with color (the combination of each die’s colors) and face value (the integer average of each die’s face values).
PART II: Using the Die class defined in PART I, design and implement a class called PairOfDice,composed of two Die objects as instance data. The class should include the following methods:
•A non-default constructor, i.e. with parameters
•A setter method for each instance data
•A getter method for each instance data
•A method roll() that rolls both dice
•A toString()method that returns a string containing the colors of both dice, eg “Colors of both dice: Red, Blue”
•A method pairSum that returns the current sum of the two die values. (PairOfDice.java)
PART III: Write an application TestPairOfDice that uses the PairOfDice class to create a pair of dice. The application prints to the screen the sum of their face values. The program then rolls the pair of dice and prints to the screen the new sum of their face values and the colors of both dice. (TestPairOfDice.java)
Edit: Die class is defined in PART 1 of the question where you do it.
/******** Die.java *********/
public class Die {
private final int MAX = 6; // maximum face value
private int faceValue; // current value showing on the
die
private String color;
//
-----------------------------------------------------------------
// Constructor: sets the initial face value.
//
-----------------------------------------------------------------
public Die() {
this.color="Green";
faceValue = 1;
}
public Die(String color) {
this.color=color;
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;
}
/**
* @return the color
*/
public String getColor() {
return color;
}
/**
* @param color the color to set
*/
public void setColor(String color) {
this.color = color;
}
//
-----------------------------------------------------------------
// Returns a string representation of this die.
//
-----------------------------------------------------------------
public String toString() {
String result = "The Die with color
"+color+" and face value "+faceValue;
return result;
}
}
/********************************************/
/*********** PairOfDice.java ********/
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();
}
public PairOfDice(Die d1,Die d2) {
die1 = d1;
die2 = d2;
}
//
-----------------------------------------------------------------
// Returns the current combined dice total.
//
-----------------------------------------------------------------
public int getTotalFaceValue()
{
return die1.getFaceValue() + die2.getFaceValue();
}
//
-----------------------------------------------------------------
// Rolls both dice
//
-----------------------------------------------------------------
public void roll() {
die1.roll();
die2.roll();
}
//
-----------------------------------------------------------------
// 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();
}
public String comboDie(Die d1, Die d2)
{
String str = "";
str += "A die with color " +
d1.getColor() + "" + d2.getColor()
+ " and face value of "
+ (d1.getFaceValue() + d2.getFaceValue()) /
2.0;
return str;
}
//
-----------------------------------------------------------------
// Returns the string representation of this pair of
dice.
//
-----------------------------------------------------------------
public String toString() {
return "Die 1: " + die1 + " Die 2:
" + die2;
}
}
/********************************************/
/******** TestPairOfDice.java **********/
public class TestPairOfDice {
public static void main(String[] args)
{
Die d1=new Die("Green");
Die d2=new Die("Blue");
PairOfDice p=new PairOfDice(d1, d2);
p.roll();
System.out.println("Color of Die#1
:"+d1.getColor());
System.out.println("Color of Die#1
:"+d2.getColor());
System.out.println("Sum of their
face values :"+p.getTotalFaceValue());
p.roll();
System.out.println("\nColor of
Die#1 :"+d1.getColor());
System.out.println("Color of Die#1
:"+d2.getColor());
System.out.println("Sum of their
face values :"+p.getTotalFaceValue());
String combo=p.comboDie(d1,
d2);
System.out.println(combo);
}
}
/********************************************/
/********************************************/
Output:
/********************************************/