Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write in Java, Problem 2 (13 pts) Write a Die (singular of \"dice\") class with

ID: 3711023 • Letter: W

Question

Write in Java,

Problem 2 (13 pts) Write a Die (singular of "dice") class with the following features. None of the methods in Die should print anything to the screen. (1 pt) Two instance variables: the number of sides, and a count of how many times the die has been rolled (2 pts) Getter methods for both instance variables (there are no setters for this class) (2 pts) A constructor that sets the number of sides when the Die object is first created. The constructor should also set the roll count to 0. (3 pts) A method roll that returns a random integer between 1 and the number of sides, inclusive. Each time roll is called, the roll count should increase by 1. Once you've finished your Die class, write a separate client program named Dieclient containing a main method (5 pts) that does the following actions: Create a Die object containing 20 sides. Repeatedly roll the die, printing the result each time, until a 20 is obtained. . . After the rolling stops, print the number of rolls it took to get a 20

Explanation / Answer

DieTest.java

public class DieTest {

public static void main(String[] args) {

Die d = new Die(20);

while(d.roll()!=20) {

}

System.out.println("Number of rolls: "+d.getCount());

}

}

Die.java

public class Die {

private int sides, count;

public Die(int n) {

sides =n;

count=0;

}

public int roll() {

count++;

int d = (int)(Math.random()*sides) + 1;

return d;

}

public int getSides() {

return sides;

}

public int getCount() {

return count;

}

}

Output:

Number of rolls: 3

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote