import java.util.*; public class Die { private int faceValue; private final int
ID: 3592391 • Letter: I
Question
import java.util.*;
public class Die
{
private int faceValue;
private final int MAX = 6;
public Die()
{
faceValue = 0;
}
public void roll()
{
Random rand = new Random();
int n = rand.nextInt(MAX) + 1;
setFaceValue(n);
}
public void setFaceValue(int n)
{
faceValue = n;
}
public int getValue()
{
return faceValue;
}
public String toString()
{
return getValue() + "";
}
}
_______________________________________________________
public class PairOfDice {
private Die die1;
private Die die2;
public int value1, value2, total;
public PairOfDice()
{
die1=new Die();
die2=new Die();
}
public Die getDie1()
{
return die1;
}
public Die getDie2()
{
return die2;
}
public int sumDice()
{
return (value1+value2);
}
public int rollDice()
{
die1.roll();
die2.roll();
value1=die1.getValue();
value2=die2.getValue();
return value1+value2;
}
@Override
public String toString()
{
return "Die 1=" + die1 + ", Die 2=" + die2 ;
}
}
____________________________________________________
how to wirte a mian class to run these two class to show the output as below
Explanation / Answer
Hi,
please find modified class - PairOfDice including main method.
PairOdDice.java
import java.util.Scanner;
public class PairOfDice {
private Die die1;
private Die die2;
public int value1, value2, total;
public PairOfDice() {
die1 = new Die();
die2 = new Die();
}
public Die getDie1() {
return die1;
}
public Die getDie2() {
return die2;
}
public int sumDice() {
return (value1 + value2);
}
public int rollDice() {
die1.roll();
die2.roll();
value1 = die1.getValue();
value2 = die2.getValue();
return value1 + value2;
}
@Override
public String toString() {
return "Die 1=" + die1 + ", Die 2=" + die2;
}
public static void main(String[] args) { //Main method to call the provided classes
char x= 'Y';
int sum=0;
while(x=='Y' || x=='y'){ //while loop to check if user wants to continue or not
PairOfDice pd=new PairOfDice();
pd.rollDice();
System.out.println(pd.toString());
sum=sum+pd.sumDice();
System.out.println("Current Round: "+sum); //Print the current round value
System.out.println("Potential Round: "+sum); //Print potential round value
int d1=Integer.parseInt(pd.getDie1().toString()); //to get value of die1
int d2=Integer.parseInt(pd.getDie2().toString()); //to get value of die2
if(d1>d2){ //Comapre value of die1 and die2
System.out.println("Busted..!!");
break;
}
System.out.println("Take another turn (y/n)?"); //user input dialog
Scanner in = new Scanner(System.in); //Scanner used to accept user input from console
x = in.next().charAt(0); //read only 1st char
}
}
}
output:
Die 1=2, Die 2=4
Current Round: 6
Potential Round: 6
Take another turn (y/n)?
y
Die 1=1, Die 2=2
Current Round: 9
Potential Round: 9
Take another turn (y/n)?
y
Die 1=1, Die 2=4
Current Round: 14
Potential Round: 14
Take another turn (y/n)?
y
Die 1=4, Die 2=5
Current Round: 23
Potential Round: 23
Take another turn (y/n)?
y
Die 1=3, Die 2=4
Current Round: 30
Potential Round: 30
Take another turn (y/n)?
y
Die 1=1, Die 2=2
Current Round: 33
Potential Round: 33
Take another turn (y/n)?
y
Die 1=5, Die 2=6
Current Round: 44
Potential Round: 44
Take another turn (y/n)?
y
Die 1=4, Die 2=2
Current Round: 50
Potential Round: 50
Busted..!!
Please let me know in case anything required. Thanks..!!
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.