Write a class named Die with the attribute face that can have a value between 1
ID: 3765583 • Letter: W
Question
Write a class named Die with the attribute face that can have a value between 1 and 6(inclusive). Include a constructor that randomly assigns a value to a die object. You may use the Math.random( ) method to generate the random numbers. Add a method in the class to return a die’s value. Now, write an application that “randomly” throws five dice for the player and five dice for the computer. The application first displays the values; then it decides the winner based on the following hierarchy of die values. Any higher combination beats the lower one. For example five of a kind beats four of a kind; four of a kind beats three; three of a kind beats two. When both the player and the computer have the same combination, the higher value wins. Submit the class and the application.
Explanation / Answer
import java.util.*;
class Die
{
int die;
Die()
{
//int range = (max - min) + 1;
die= (int)(Math.random() * 6) + 1;
//System.out.println(die);
}
int get()
{
return die;
}
}
class Dies_game
{
public static void main(String args[])
{
int d[]=new int[5];
Die[] o={new Die(), new Die(), new
Die(), new Die(), new Die()};
int i,su=0,sc=0;
Scanner scan = new Scanner
(System.in);
System.out.println("Enter 5 Dies values
between 1 to 6");
for(i=0;i<5;i++)
{
d[i]=scan.nextInt();
}
System.out.println("Computer
generated values are:");
for(i=0;i<5;i++)
{
System.out.print(o[i].get()+"
");
}
for(i=0;i<5;i++)
{
su=su+d[i];
sc=sc+o[i].get();
}
if(su>sc)
{
System.out.println("USer
wins");
}
else
{
System.out.println
("Computer wins");
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.