using java • Write a class Die which has members & methods: •int sides : # of si
ID: 3705577 • Letter: U
Question
using java
• Write a class Die which has members & methods:
•int sides: # of sides for the die
•int value: value of the die
•String owner: Name of player holding the die
•Methods:
•Appropriate constructors.
•Set/get for sides, value and owner.
•roll(): creates a random object and uses it to set value to a random number.
•Write a main method that has the computer play against the user.
•Ask the user for their name. Create a die for the user and one for the computer.
•Ask the user how many sides they want the die to have.
•Ask the user how many iterations they want to play with.
•Create a loop that runs for the iterations specified by the user. In each iteration roll both die and check
which one has a larger value. The die with the larger value gets a point. In case of a tie, no one gets a
point. At the end, print the score and the name of the winner.
Explanation / Answer
Hi,
As per you requirement i wrote a class in java..........just run the code give the proper inputs for desired output.........if any queries or doubts juss ping in comment box.......Happy Coding!!
////////////////////////////////////////////////////////////Die.java////////////////////////////////////////////////////////////////////////////
import java.util.Random;
import java.util.Scanner;
import java.util.concurrent.TimeUnit;
public class Die {
int sides;
int value;
String owner;
public int getSides() {
return sides;
}
public void setSides(int sides) {
this.sides = sides;
}
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
public String getOwner() {
return owner;
}
public void setOwner(String owner) {
this.owner = owner;
}
public static Die roll(int sides,String owner){
Random rand = new Random();
Die die = new Die();
die.setOwner(owner);
die.setSides(sides);
int value = rand.nextInt(sides);
if(value==0){value=value+1;}
die.setValue(value);
return die;
}
public static void main(String[] args) throws InterruptedException {
int usercount=0,computercount=0;
Scanner sc=new Scanner(System.in);
System.out.println("Name of the User: ");
String username = sc.nextLine();
System.out.println("No.of Sides of the die: ");
int sides = sc.nextInt();
System.out.println("Enter the no.of iterations you want to play with computer??");
int iterations = sc.nextInt();
sc.nextLine();
for(int i=0;i<iterations;i++){
int key=i+1;
System.out.println("It's User Turn....");
System.out.println("Press Any key to play the "+key+"th chance :");
sc.nextLine();
Die die1 = roll(sides,username);
System.out.println("Your Die Value :"+die1.getValue());
System.out.println("It's Computer Turn....");
TimeUnit.SECONDS.sleep(1);
System.out.println("Rolling.....");
TimeUnit.SECONDS.sleep(1);
Die die2 = roll(sides,"Computer");
System.out.println("Computer Die Value : "+die2.getValue());
if(die1.getValue()>die2.getValue()){
usercount++;
System.out.println("you Won the This Iteration......:)");
}
else if(die1.getValue()<die2.getValue()){
computercount++;
System.out.println("Computer Won the This Iteration......:(");
}
else if(die1.getValue()==die2.getValue()){
System.out.println("This iteration got Tied......");
}
}
if(usercount>computercount){
System.out.println("Hurrayy..........You won the game :)");
}
else if(usercount<computercount){
System.out.println("Ooppss........You Lost the Game :(");
}
else if(usercount==computercount){
System.out.println("Game Tied.........");
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.