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...
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...
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...
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:...
HI, I hope you are doing well. I really don't understand this question and don't know...
HI, I hope you are doing well. I really don't understand this question and don't know how to solve it at all because I am completely new to this c++ programming. can you please explain each line of code with long and clear comments? please think of me as someone who doesn't know to code at all. and I want this code to be written in c++ thank you very much and I will make sure to leave thumbs up....
I've seen this question answered for C but I don't know how to translate to Java....
I've seen this question answered for C but I don't know how to translate to Java. Write a program that requests the hours worked in a week and the basic pay rate, and then prints the gross pay (before tax), the taxes, and the net pay (after tax). Assume the following: Basic pay rate: from user's choice Overtime (in excess of 40 hours) = time and a half (i.e., if the hours worked is 45, the hours for pay will...
please I don't really know how to start answering this question I really need to understand...
please I don't really know how to start answering this question I really need to understand it please show the work with a clear handwriting A collision in one dimension A mass m1 = 2 kg moving at v1i = 3 ms−1 collides with another mass m2 = 4 kg moving at v2i = −2 ms−1. After the collision the mass m1 moves at v1f = −3.66 ms−1. (a) Calculate the final velocity of the mass m2. (b) After the...
I know the what the answers are but I don't know how to get them. Can...
I know the what the answers are but I don't know how to get them. Can you please explain the process? Thank you. Part VII. Discontinued Operations and Earnings per Share (11 points) Todd Corporation had pre-tax income for 2017 of $2,500,000. On December 31, 2017, Boyd disposed of a component of its business that represented a strategic shift in operation. That component had a Loss on Discontinued Operations of $450,000 (pre-tax). Boyd received $1,000,000 net cash proceeds from the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT