In: Computer Science
I don't know how to build my last code for TestPairOfDice...below the question is in bold. I'm including my code for the previous problems which are all needed for number 6
1. Implement a method named surface that accepts 3 integer
parameters named width, length, and height as user input. It will
return the total surface area (6 sides) of the rectangular box it
represents. The formula for Surface Area is 2(length * width) +
2(length * height) + 2(height * width)
Sample input and output:
Enter width: 2
Enter length: 3
Enter height: 4
The total surface area is: 52
2. Implement a method named right Triangle that accepts 2 double
parameters named sideA and hypotenuseB as user input. The method
will return the length of the third side.
Sample input and output:
Enter side: 3.0
Enter hypotenuse: 5.0
The other side is: 4.0
Class Die (The modification of the class Die should be saved as
Die.java)
3. Modify the class Die presented in class to include another instance data (String), called color, to represent the color of a die. Add a getter/setter for this data. comboDie (This method is part of the MyMethods.java class)
4. 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).
Example: if the first die is “blue” with faceValue=3 and the second
die is “red” with faceValue=5, the method returns a “blue-red” die
with facevalue = 4.
Use the Die class presented in class, that you modified in Problem
3.
DEFINING CLASSES (PairOfDice.java and TestPairOfDice.java.)
5. Using the Die class defined in class, design and implement a
class called
PairOfDice, composed of two Die objects. Include methods to set and
get each individual die, a method to roll the dice, a toString
method that returns colors of both dice and a method pairSum that
returns the current sum of the two die values.
(PairOfDice.java)
6. 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. Then, use the
PairOfDice class to rollthe dice. The application prints to the
screen the new sum of their face values and the colors of
both
dice. (TestPairOfDice.java)
Sample output:
Sum of face values before roll: 7
Sum of face values after roll: 6
Colors of both dice: Red, Blue
_________________________________________________________
import java.util.*;
public class MyMethods
{
public int surface(int width, int height, int
length)
{
return 2*(width*length + width*height + length*height);
}
public double rightTriangle(double sideA, double
hypotenuseB)
{
double sideC;
sideC = Math.sqrt
(hypotenuseB*hypotenuseB + sideA*sideA);
return sideC;
}
public static void main (String [] args)
{
Scanner scan = new Scanner(System.in);
System.out.println("Enter a width: ");
int width = scan.nextInt();
System.out.println("Enter a height: ");
int height = scan.nextInt();
System.out.println("Enter a length: ");
int length = scan.nextInt();
}
}
_________________________________________________________
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;
}
//-----------------------------------------------------------------
// Sets new color of this die.
//-----------------------------------------------------------------
public void setColor(String newColor)
{
color = new Color();
}
//-----------------------------------------------------------------
// Returns new color of this die.
//-----------------------------------------------------------------
public void getColor()
{
return color;
}
public static String comboDie(Die d1, Die d2)
{
int avg = ((d1.faceValue + d2.faceValue)/2);
return "\""+d1.color+"-"+d2.color+"\""+" die with facevalue = "+
avg;
}
}
_________________________________________________________
public class PairOfDice
{
private Die die1;
private Die die2;
public PairOfDice()
{
die1 = new Die();
die2 = new Die();
}
public PairOfDice(Die die1, Die die2)
{
this.die1 = die1;
this.die2 = die2;
}
public Die getDie1()
{
return die1;
}
public void setDie1(Die die1)
{
this.die1 = die1;
}
public Die getDie2()
{
return die2;
}
public void setDie2(Die die2)
{
this.die2 = die2;
}
public void roll()
{
die1.roll();
die2.roll();
}
public int pairSum()
{
return die1.getFaceValue() + die2.getFaceValue();
}
public String toString()
{
return "Color of Dice 1: "+die1.getColor()+", Color of Dice 2:
"+die2.getColor();
}
}
_________________________________________________________
public class TestPairOfDice
{
public static void main(String[] args)
{
PairOfDice dice = new PairOfDice();
Thanks for the question, I have modified the Die.java class to add a String color. It was giving compilation errors also, I have fixed them as well. The class also assigns a random color to each Die object in the constructor.
I have also modified the PairOfDice.java class have changed the toString() method.
Lastly I have implemented the TestPairOfDice.java. Here are all the updated classes. Let me know for any changes. No changes made in the MyMethods.java class.
======================================================================
public class Die { private final int MAX = 6; // maximum face value private int faceValue; // current value showing on the die // include another instance data (String), called color private String color; //----------------------------------------------------------------- // Constructor: Sets the initial face value. //----------------------------------------------------------------- public Die() { faceValue = 1; final String colors[] = new String[]{"Red", "Blue", "Green"}; color = colors[(int) (Math.random() * colors.length)]; } //----------------------------------------------------------------- // 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; } //----------------------------------------------------------------- // Sets new color of this die. //----------------------------------------------------------------- public void setColor(String newColor) { color = newColor; } //----------------------------------------------------------------- // Returns new color of this die. //----------------------------------------------------------------- public String getColor() { return color; } public static String comboDie(Die d1, Die d2) { int avg = ((d1.faceValue + d2.faceValue) / 2); return "\"" + d1.color + "-" + d2.color + "\"" + " die with facevalue = " + avg; } }
====================================================================
public class PairOfDice { private Die die1; private Die die2; public PairOfDice() { die1 = new Die(); die2 = new Die(); } public PairOfDice(Die die1, Die die2) { this.die1 = die1; this.die2 = die2; } public Die getDie1() { return die1; } public void setDie1(Die die1) { this.die1 = die1; } public Die getDie2() { return die2; } public void setDie2(Die die2) { this.die2 = die2; } public void roll() { die1.roll(); die2.roll(); } public int pairSum() { return die1.getFaceValue() + die2.getFaceValue(); } public String toString() { return die1.getColor()+","+die2.getColor(); } }
====================================================================
public class TestPairOfDice { public static void main(String[] args) { PairOfDice dice = new PairOfDice(); System.out.println("Sum of face values before roll: "+ dice.pairSum()); dice.roll();; System.out.println("Sum of face values after roll: "+ dice.pairSum()); System.out.println("Colors of both dice:"+dice); } }
====================================================================