Question

In: Computer Science

USING JAVA, PLEASE MAKE THIS AS SIMPLE AS POSSIBLE, THIS IS AN INTRODUCTORY JAVA COURSE 1....

USING JAVA, PLEASE MAKE THIS AS SIMPLE AS POSSIBLE, THIS IS AN INTRODUCTORY JAVA COURSE

1. Design and implement a class called PairOfDice.

The class should include:

a. A non-default constructor, i.e. with parameters

b. A setter method for each instance data

c. A getter method for each instance data

d. A method roll() that rolls both dice

e. A toString() method that returns a string containing the colors of both dice, eg "Colors of both dice: Red,Blue "

f. A method pairSum that returns the current sum of the two die values. (in PairOfDice.java)

2. Write an application TestPairDice 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 and the colors of both dice. (TestPairOfDice.java)

SAMPLE OUTPUT: Sum of face values before roll: 7

Sum of face values after roll: 6

Color of both dice: Red,Blue

Solutions

Expert Solution

Below is the solution:

public class PairOfDice {
   // declare the variable
   private int faceValue1; // die1 faceValue
   private int faceValue2; // die1 faceValue
   public String color; // string showing the color of the die

   // getter method
   public int getFaceValue1() {
       return faceValue1;
   }

   public int getFaceValue2() {
       return faceValue2;
   }

   public String getColor() {
       return color;
   }

   // setter method
   public void setFaceValue1(int faceValue1) {
       this.faceValue1 = faceValue1;
   }

   public void setFaceValue2(int faceValue2) {
       this.faceValue2 = faceValue2;
   }

   public void setColor(String color) {
       this.color = color;
   }

   // roll two die
   public void roll() { // Rolls the die and returns the result
       faceValue1 = (int) (Math.random() * 6) + 1;
       faceValue2 = (int) (Math.random() * 6) + 1;
   }

   // return the pair of sum two die
   public int pairSum() {
       return faceValue1 + faceValue2;
   }

   // toString method
   @Override
   public String toString() {
       return color;
   }

   // default constructor
   public PairOfDice() {
       // set the default value of the die1 and die2
       this.faceValue1 = 4;
       this.faceValue2 = 3;
   }

   // non-default constructor, i.e. with parameters
   public PairOfDice(int faceValue1, int faceValue2) {
       super();
       this.faceValue1 = faceValue1;
       this.faceValue2 = faceValue2;
   }
}

public class TestPairOfDice {

   public static void main(String[] args) {
       PairOfDice die1 = new PairOfDice(); // create an object
       die1.setColor("Red"); // set the first die color to red
       System.out.println("Sum of face values before roll: " + die1.pairSum()); // print sum of the die
       PairOfDice die2 = new PairOfDice();
       die2.setColor("Blue"); // set the second die color to red
       die2.roll();
       System.out.println("Sum of face values after roll: " + die2.pairSum());
       System.out.println("Color of both dice:" + die1.toString() + "," + die2.toString());
   }
}

sample output 1:

Sum of face values before roll: 7
Sum of face values after roll: 6
Color of both dice:Red,Blue

sample output 2:

Sum of face values before roll: 7
Sum of face values after roll: 10
Color of both dice:Red,Blue


Related Solutions

Please use the Java Programming language. This is the introductory course, chapter two. Please only use...
Please use the Java Programming language. This is the introductory course, chapter two. Please only use if/else if, else and while loop. We have not touch base with do and while do(I don't know if while do exist in Java). Create an application that converts number grades to letter grades. Console Welcome to the Letter Grade Converter Enter numerical grade: 90 Letter grade: A Continue? (y/n): y Enter numerical grade: 88 Letter grade: A Continue? (y/n): y Enter numerical grade:...
I am in beginners java course so using the most simple/basic JAVA please complete the following...
I am in beginners java course so using the most simple/basic JAVA please complete the following CODE SEGMENTS. (2 parts) FIRST PART: Using ITERATIVE create a METHOD MAX that returns the max element in an ArrayList of ints and prints all of the elements. *you have to use an iterative in the method.* (just need to write the METHOD ONLY not a whole program(will do in 2nd part), assume you are given any numbers/integers. SECOND PART: Now write a class...
How Maxwell introduced the concept of a “displacement current”? please make it as simple as possible.
How Maxwell introduced the concept of a “displacement current”? please make it as simple as possible.
Question 1: A student in an introductory statistics course investigated if there is evidence that the...
Question 1: A student in an introductory statistics course investigated if there is evidence that the proportion of milk chocolate M&M's that are green differs from the proportion of dark chocolate M&M's that are green. She purchased a bag of each variety, and her data are summarized in the following table. Green Not Green Total Milk Chocolate 8 33 41 Dark Chocolate 4 38 42 Total 12 71 83                          c. Estimate the p-value using the randomization distribution and show...
Some things that need to be added to it are: Make it as simple as possible...
Some things that need to be added to it are: Make it as simple as possible colors background colors fonts borders position everything horizontally Heres the code I have so far: <html>    <head>    <title> TheWebpage</title>               <meta charset="utf-8" />           </head>        <body>            <header>                <h1>Welcome to my first web page.</h1>            </header>                   <article>        <h1>Favorite dream vacation</h1>...
Please code the following, using the language java! Build a simple calculator that ignores order of...
Please code the following, using the language java! Build a simple calculator that ignores order of operations. This “infix” calculator will read in a String from the user and calculate the results of that String from left to right. Consider the following left-to-right calculations: "4 + 4 / 2" "Answer is 4" //not 6, since the addition occurs first when reading from left to right “1 * -3 + 6 / 3” “Answer is 1” //and not -1Start by copying...
Please create using only For loops, as simple as possible as I am attending Java1 Create...
Please create using only For loops, as simple as possible as I am attending Java1 Create two Java programs that do the following: a. Use a for loop to print the numbers below on a single line as shown. 1 2 4 8 16 32 64 128. b. Ask the user to enter numbers using the for loop.  First ask the user how many numbers they will be entering in. Print the sum and average of the entered numbers. c. Write...
Make a simple game using C++ which implements all about Object Oriented Programming (Please make an...
Make a simple game using C++ which implements all about Object Oriented Programming (Please make an explanation which of each part in it)
Use JAVA to Design a simple registration system that allows Student to register in a course....
Use JAVA to Design a simple registration system that allows Student to register in a course. Requirements using 2 classes: class Student & class Course. Implement the scenarios in class Test’s main method. Each student has a name and an id variable. Each object of class Student is initialized using values of name and id passed to constructor. Class Student has get methods for its instance variables.(getters and setters) Each Course has a name, and a variable numberOfStudent representing the...
Please explain how these figures are generated as simple as possible
Source    SS    df    MS    F    p-value A    5.0139    1    5.0139    100.28    0 B    2.1811    2    1.0906    21.81    .0001 AB    0.1344    2    0.0672    1.34    .298 Error    0.6000    12    0.0500        Please explain how these figures are generated as simple as possible
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT