Question

In: Computer Science

I don't know how to build my last code for TestPairOfDice...below the question is in bold....

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();

Solutions

Expert Solution

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); 

    }
}

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


Related Solutions

This is a critical thinking question. I know the answers. My question is in bold below...
This is a critical thinking question. I know the answers. My question is in bold below the textbook question. Ch 13, #57PE - A deep-sea diver should breath a gas mixture that has rthe same oxygen partial pressure as at sea level, where dry air contains 20.9% oxygen and has a total pressure of 1.01x105 N/m2. When developing a strategy for solving this problem, how can the given information and the principles that govern this context lead me to what...
PHP Language I have XAMPP and I don't know how to display my PHP code. For...
PHP Language I have XAMPP and I don't know how to display my PHP code. For instance, when I create a basic HTML webpage I just open the code into a web browser and it shows me a basic web page. When I try to show a PHP calculation it doesn't show. What are the steps in displaying my PHP doc?
I don't know why my java code is not running this error code pops up -->...
I don't know why my java code is not running this error code pops up --> Error: Could not find or load main class DieRoll Caused by: java.lang.ClassNotFoundException: DieRoll. Code below:    import java.util.Random;    import java.util.Scanner;    public class Assignment {    public static void combineStrings() {    String school = "Harvard";    String city = "Boston, MA";    int stringLen;       String upper, changeChar, combine;       stringLen = school.length();    System.out.println(school+" contains "+stringLen+" characters." );   ...
I'm getting an error message with this code and I don't know how to fix it...
I'm getting an error message with this code and I don't know how to fix it The ones highlighted give me error message both having to deal Scanner input string being converted to an int. I tried changing the input variable to inputText because the user will input a number and not a character or any words. So what can I do to deal with this import java.util.Scanner; public class Project4 { /** * @param args the command line arguments...
I know how to do this with arrays, but I have trouble moving my code to...
I know how to do this with arrays, but I have trouble moving my code to use with linked lists Write a C program that will deal with reservations for a single night in a hotel with 3 rooms, numbered 1 to 3. It must use an infinite loop to read commands from the keyboard and quit the program (return) when a quit command is entered. Use a switch statement to choose the code to execute for a valid command....
In Python I have a code: here's my problem, and below it is my code. Below...
In Python I have a code: here's my problem, and below it is my code. Below that is the error I received. Please assist. Complete the swapCaps() function to change all lowercase letters in string to uppercase letters and all uppercase letters to lowercase letters. Anything else remains the same. Examples: swapCaps( 'Hope you are all enjoying October' ) returns 'hOPE YOU ARE ALL ENJOYING oCTOBER' swapCaps( 'i hope my caps lock does not get stuck on' ) returns 'I...
Python problem I don't know how to arrive to the answer of this question: "Rounded to...
Python problem I don't know how to arrive to the answer of this question: "Rounded to the nearest integer, how much higher is the average sum of all six stats among Mega Pokemon than their non-Mega versions? Note that Mega Pokemon share the same Number (the first column) as their non-Mega versions, which will allow you to find all Pokemon that have a Mega version." For this problem below. The format of the file is included at the bottom but...
JAVA: How do I fix the last "if" statement in my code so that outputs the...
JAVA: How do I fix the last "if" statement in my code so that outputs the SECOND HIGHEST/MAXIMUM GPA out of the given classes? public class app { private static Object minStudent; private static Object maxStudent; public static void main(String args[ ]) { student st1 = new student("Rebecca", "Collins", 22, 3.3); student st2 = new student("Alex", "White", 19, 2.8); student st3 = new student("Jordan", "Anderson", 22, 3.1); student[] studentArray; studentArray = new student[3]; studentArray[0] = st1; studentArray[1] = st2; studentArray[2]...
//Trying to get this code with JavaScript. I could partition the subarrays, but I don't know...
//Trying to get this code with JavaScript. I could partition the subarrays, but I don't know how to check for unique elements Given an array of integers check if it is possible to partition the array into some number of subsequences of length k each, such that: Each element in the array occurs in exactly one subsequence For each subsequence, all numbers are distinct. Elements in the array having the same value must be in different subsequences If it is...
The Question I am needing assistance with is in BOLD BELOW. I will include the other...
The Question I am needing assistance with is in BOLD BELOW. I will include the other aspects of this assignment for reference, as well as the table used/needed for these calculations. Option #1: Critical Thinking: Quality at A1 Hotels A1 Hotels operates luxury hotels throughout the world. Recently, motivated by some incidents that appeared in the news, they have been concerned about the quality of service. The company has been giving the following survey to its clients after their stay:...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT