Question

In: Computer Science

JAVA PRACTICE PROBLEMS [ Thumbs up and good review guaranteed if code is correct! ] PART...

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.

Solutions

Expert Solution

/******** 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:

/********************************************/


Related Solutions

***JAVA PLEASE*** All correct and complete responses will receive a thumbs up and positive comment! Thank...
***JAVA PLEASE*** All correct and complete responses will receive a thumbs up and positive comment! Thank you!! Create a simple class that emulates some mathematical functions in relation to complex numbers. A complex number is a number of the form a + bi, where a is a real number and bi is an imaginary number. Create a class Complex having two private data members real and imag of type double. The class has two constructors, one default (no parameters) and...
Create a new Java project, name it “Quiz02”. (This is a JAVA Programming question, thumbs up...
Create a new Java project, name it “Quiz02”. (This is a JAVA Programming question, thumbs up quick and correct answer) 1) Create a java class Traveler that has four data members, String name, Date bDate, int id and int noOfDependens. Generate the following: 1- Getters and Setters. 2- Four parametrize constructor to initialize the four data members above. 3- toString() method. Create a java class Flight that has four data members, String number, String destination, int capacity, and ArrayList travelers....
Please select ALL the correct answers for a thumbs up. Thanks :) 1. MMs’ Proposition II...
Please select ALL the correct answers for a thumbs up. Thanks :) 1. MMs’ Proposition II states that the value of the firm depends on all of the following except, Required rate of return on the firm’s assets Cost of debt of the firm Debt to equity ratio of the firm Cost of equity of the firm 2. which of the following types of risk is most difficult to manage? a. interest rate risk b. exchange rate risk c. price...
how to correct this java code so that i get the correct day of week? and...
how to correct this java code so that i get the correct day of week? and test the year public static void main(String[] args) {        //               Scanner s = new Scanner(System.in); //needed info //year month, day int year, month, dayOfMonth; // add day of week , century yr int dayOfWeek, century, yearOfCentury;    //user inputs year System.out.print("Enter year: (example, 2020):"); year = s.nextInt(); //user inputs month by number System.out.print("Enter month: 1-12:"); month = s.nextInt();...
2 part question. Thumbs up promised. A) A block with mass m1 = 9.3 kg is...
2 part question. Thumbs up promised. A) A block with mass m1 = 9.3 kg is on an incline with an angle θ = 23° with respect to the horizontal. For the first question there is no friction, but for the rest of this problem the coefficients of friction are: μk = 0.2 and μs = 0.22. When there is no friction, what is the magnitude of the acceleration of the block? Now with friction, what is the magnitude of...
Given the below Java code snippet, assuming the code is correct, and the names are meaningful,...
Given the below Java code snippet, assuming the code is correct, and the names are meaningful, select the best answer for the below questions: public static void main(String[] args) { int numStations = 10; int enter = 0; int exit = 0; Train train = new Train(numStations); for (int station = 0; station < numStations; station++) {      enter = enter + train.enterPassengers(station);      exit = exit + train.exitPassengers(station); } System.out.println("Number of passengers entered the train: " + enter); System.out.println("Number...
PLEASE SOLVE I WILL RATE AND THUMBS UP Assembly code question Write an LC3 program to...
PLEASE SOLVE I WILL RATE AND THUMBS UP Assembly code question Write an LC3 program to compute the XOR (exclusive OR) operation. The program computes the XOR of two numbers stored in registers R0 and R1 and returns the result in register R3. To test your program, before computing the XOR load the two values from memory locations x4000 (into R0) and x4001 (into R1).
Thumbs up for all correct answers. Question 1 5 pts (TCO 1) _____ service delivers the...
Thumbs up for all correct answers. Question 1 5 pts (TCO 1) _____ service delivers the fastest connections available at an economical megabit-per-second price. Dedicated leased line DSL Fiber-optic Ethernet Satellite Flag this Question Question 2 5 pts (TCO 1) Which of the following best defines a scalable network? A scalable network is built to support high-altitude environments, including skyscrapers and satellite dishes on mountains. A scalable network supports redundant connections so that alternative paths are available when a device...
2 part question, Thumbs up promised A) A block with mass m = 17 kg rests...
2 part question, Thumbs up promised A) A block with mass m = 17 kg rests on a frictionless table and is accelerated by a spring with spring constant k = 4112 N/m after being compressed a distance x1 = 0.526 m from the spring’s unstretched length. The floor is frictionless except for a rough patch a distance d = 2.2 m long. For this rough path, the coefficient of friction is μk = 0.43. How much work is done...
java Goal: This lab will give you practice writing code that uses inheritance and Java interfaces....
java Goal: This lab will give you practice writing code that uses inheritance and Java interfaces. Please make sure you have a partner for this lab. No code is provided with this lab. Write code to experimentally resolve the following questions about Java. You will need to show your code to the TA or lab assistant. Part I: Assignments and Casting (1 point) ------------------------------------------ Let Y be a subclass of X, and let y and x be variables of classes...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT