I need help with these Java programming assignements.
public class Die
{
//here you declare your attributes
private int faceValue;
//operations
//constructor -
public Die()
{
//body of constructor
faceValue=(int)(Math.random()*6)+1;//instead of 1, use random
approach to generate it
}
//roll operation
public void roll()
{
faceValue=(int)(Math.random()*6)+1;
}
//add a getter method
public int getFaceValue()
{
return faceValue;
}
//add a setter method
public void setFaceValue(int value)
{
faceValue=value;
}
//add a toString() method
public String toString()
{
String result = Integer.toString(faceValue);
return result;
}
}
1.Modify the class Die presented above to include another instance data (string) called color to represent the color of a die.Add a getter/setter for this data.
2.. Implement a method comboDie that takes two dice parameters. The method returns a die with color the combination of both dice’ colors and face value the average of the dice’ facevalues. (Add this method to your MyMethods class)
Example: if first die is “blue” with facevalue=3 and second die is “red”with facevalue=5, the method returns a “blue-red” die with facevalue=4. Note.
Use the Die class presented above.
3. Using the Die class defined above, 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.
4.Write an application TestPairOfDice that uses the PairOfDice class to create and roll a pair of dice 1000 times, counting the number of box cars (two sixes) that occur..
//Java code
public class Die { //here you declare your attributes private int faceValue; private String color; //operations //constructor - public Die() { //body of constructor faceValue=(int)(Math.random()*6)+1;//instead of 1, use random approach to generate it color = "none"; } //roll operation public void roll() { faceValue=(int)(Math.random()*6)+1; } //add a getter method public String getColor() { return color; } public int getFaceValue() { return faceValue; } //add a setter method public void setFaceValue(int value) { faceValue=value; } public void setColor(String color) { this.color = 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; } //add a toString() method public String toString() { String result = Integer.toString(faceValue); return result; } }
//=========================
public class PairOfDice { private Die die1; private Die die2; //Constructor public PairOfDice() { die1 = new Die(); die2 = new Die(); } //Overload Constructor public PairOfDice(Die die1, Die die2) { this.die1 = die1; this.die2 = die2; } //getters and setters 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(); } @Override public String toString() { return "Color of Dice1: "+die1.getColor()+", Color of Dice2: "+die2.getColor(); } }
//=========================
public class TestPairOfDice { public static void main(String[] args) { PairOfDice pairOfDice = new PairOfDice(); Die die1 =new Die(); die1.setColor("Red"); Die die2 = new Die(); die2.setColor("Blue"); pairOfDice.setDie1(die1); pairOfDice.setDie2(die2); System.out.println(pairOfDice); int count =0; for (int i = 1; i <=1000 ; i++) { pairOfDice.roll(); System.out.println("Die1: "+pairOfDice.getDie1()+", Die2: "+pairOfDice.getDie2()); if(pairOfDice.getDie1().getFaceValue()==6 && pairOfDice.getDie2().getFaceValue()==6) count++; } System.out.println("Number of box cars: "+count); } }
//Output
//If you need any help regarding this solution...... please leave a comment....... thanks
I need help with these Java programming assignements. public class Die { //here you declare your...
Using the Die class defined in this chapter, design and implement a class called PairOfDice, composed of two Die objects. Include methods to set and get the individual die values, a method to roll the dice, and a method that returns the current sum of the two die values. Create a driver class called RollingDice2 to instantiate and use a PairOfDice object. I need the main method or test class public class Die { private final int MAX = 6;...
Hello, Could you please help me code this program in Java? It is important that all rules are carefully followed. Using the PairOfDice class from PP 4.9, design and implement a class to play a game called Pig. In this game, the user competes against the computer. On each turn, the current player rolls a pair of dice and accumulates points. The goal is to reach 100 points before your opponent does. If, on any turn, the player rolls a...
WRITING METHODS 1. Implement a method named surface that accepts 3 integer parameters named width, length, and depth. It will return the total surface area (6 sides) of the rectangular box it represents. 2. Implement a method named rightTriangle that accepts 2 double arameters named sideA and hypotenuseB. The method will return the length of the third side. NOTE To test, you should put all of these methods into a ‘MyMethods’ class and then write an application that will instantiate...
Please Help! I need to update the code below to meet these criteria! 1. The constructor of die class initializes face of Die to 3 2. roll method updates face of die to value from 1-6 3. getFace() method provides current face to the main program 4. main create a Die object 5. main create a Die object 6. main rolls die 5 times 7. main computes sum of rolls 8. main computes actual average of the rolls <--------- Code...
Include an unfair or biased dice. dice doesnt have equal opportunities (in regular dice it is 1/6) DONT implement unbiased roll, reorganize the program to include BiasedDicePair class, add the new class to this project. PairOfDiceDriver.java public class PairOfDiceDriver { public static void main(String[] args) { PairOfDice obj = new PairOfDice(); obj.roll(); System.out.println(obj); } } PairOfDice.java public class PairOfDice { private int face1, face2; public PairOfDice() { ...
In Java* Please implement a class called "MyPet". It is designed as shown in the following class diagram. Four private instance variables: name (of the type String), color (of the type String), gender (of the type char) and weight(of the type double). Three overloaded constructors: a default constructor with no argument a constructor which takes a string argument for name, and a constructor with take two strings, a char and a double for name, color, gender and weight respectively. public...
Here is a sample run of the tester program: Make sure your program follows the Java Coding Guidelines. Consider the following class: /** * A store superclass with a Name and Sales Tax Rate */ public class Store { public final double SALES_TAX_RATE = 0.06; private String name; /** * Constructor to create a new store * @param newName */ public Store(String newName) { name = newName; } ...
Hello. I need help writing the following Java Program. Thank you Develop a class encapsulating the concept of a college course, assuming that a course has following attributers: code (for instance COSC1337), a description, and a number of credits (for instance 3). Include a constructor, the accessors, mutators and methods ‘toString’, ‘equals’, and ‘finalize’. Write a client class to test the behavior of the class and its methods. The outline of the class is given as follows: public class Course...
I Need Help with this using Java Programming : Class name fname lname Class variable total Number Constructor (no arguments) Constructor (takes two arguments, fname and lname) One getter method to return the fname and lname One setter method for fname and lname Inside Main: Create three objects with the following names Jill Doe John James Jack Smith When creating the first object (Should not be an anonymous object) use the argument-less constructor Then use the setter method to assign...
Please help me with this code. Thank you Implement the following Java class: Vehicle Class should contain next instance variables: Integer numberOfWheels; Double engineCapacity; Boolean isElectric, String manufacturer; Array of integers productionYears; Supply your class with: Default constructor (which sets all variables to their respective default values) Constructor which accepts all the variables All the appropriate getters and setters (you may skip comments for this methods. Also, make sure that for engineCapacity setter method you check first if the vehicle...