Using the Die class defined in Chapter 4, write a class called TwoDice, 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. Include a static main method in TwoDice so that it can instantiate and use a TwoDice object to illustrate all the features in the class.
Input:
TwoDice.java
import java.util.Scanner;
public class TwoDice { //Define a class TwoDice
private Die die1 = new Die(); //Create an object of class Die
private Die die2 = new Die();
void setDie1(int value) //Set value for Die
{
if(!die1.setValue(value)) //Check if value is set
System.out.println("Value should be between 1-6");
}
void setDie2(int value)
{
if(!die2.setValue(value))
System.out.println("Value should be between 1-6");
}
int getDie1() //Get value for die
{
return die1.getValue();
}
int getDie2()
{
return die2.getValue();
}
void rollDice() //Roll the dice
{
setDie1((int) (Math.random() * 6) + 1); //Set a random value between 1 - 6
setDie2((int) (Math.random() * 6) + 1);
}
int getSum(){
return getDie1() + getDie2(); //Return addition of die faces
}
public static void main(String[] args)
{
TwoDice dice = new TwoDice(); //Create an object of TwoDice
Scanner scanner = new Scanner(System.in); //Initialize scanner for accepting input
String ans = "y"; //Initialize a string for loop
while (ans.equals("y"))
{
System.out.println("1.Roll Dice\n2.Set Values\n3.Get Values"); //Display Menu
int choice = scanner.nextInt(); //Ask user to enter choice
switch (choice) //Switch case for choice
{
case 1:
dice.rollDice();
System.out.println("Die 1: "+dice.getDie1()+" | Die 2: "+dice.getDie2());
System.out.println("The sum is "+dice.getSum());
break;
case 2:
System.out.println("Enter value for Die 1");
dice.setDie1(scanner.nextInt());
System.out.println("Enter value for Die 2");
dice.setDie2(scanner.nextInt());
break;
case 3:
System.out.println("Die 1: "+dice.getDie1()+" | Die 2: "+dice.getDie2());
break;
default:
System.out.println("Wrong choice!");
break;
}
System.out.println("Do you want to continue?(y/n)");
ans = scanner.next();
}
}
}
class Die{ //Data class for Die
private int currentFace = 1; //Attribute
boolean setValue(int newValue) //Set face value for Die
{
if (newValue < 1 || newValue > 6) //Check if value is between 1 - 6
return false;
this.currentFace = newValue;
return true;
}
int getValue(){ //Return value for current face
return currentFace;
}
}
Output:

Explanation:
Using the Die class defined in Chapter 4, write a class called TwoDice, composed of two...
Using the Die class in chapter 4, write a class called PairOfDice, composed of two Die object. 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.
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;...
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...
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...
C+++ Program Part A Create a class called die (the singular of dice). The die class should have a single data member called value. The die constructor should initialize the die’s value to 1. Create a member function called roll() that gives the die a random value between 1 and 6. Create a member function called show() that displays value. In main, demonstrate the creation of a die object to validate it is working. Part B Revise main to count...
Make a public class called ManyExamples and in the class... Write a function named isEven which takes in a single int parameter and returns a boolean. This function should return true if the given int parameter is an even number and false if the parameter is odd. Write a function named close10 which takes two int parameters and returns an int. This function should return whichever parameter value is nearest to the value 10. It should return 0 in the...
Build a class called Account.java. This class should have the following properties: AcctNo, Owner and balance. Also add the appropriate set and get methods, display method and main method. Main() should be used to test this class. In main() instantiate an Account object, fill it with data using the set methods, then call display to display the data.
Create a class Circle with one instance variable of type double called radius. Then define an appropriate constructor that takes an initial value for the radius, get and set methods for the radius, and methods getArea and getPerimeter. Create a class RightTriangle with three instance variables of type double called base, height, and hypotenuse. Then define an appropriate constructor that takes initial values for the base and height and calculates the hypotenuse, a single set method which takes new values...
Write a class called Point that contains two doubles that represent its x- and y-coordinates. It should have get and set methods for both fields. It should have a constructor that takes two double parameters and initializes its coordinates with those values. It should have a default constructor that initializes both coordinates to zero. It should also contain a method called distanceTo that takes as a parameter another Point and returns the distance from the Point that was passed as...
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...