Question

Design a class that can be used to simulate rolling a multi-faced die. 2. Write a...

Design a class that can be used to simulate rolling a multi-faced die.

2. Write a Java class called Die in a class file called Die.java.   

3. The Die class will have three private class attributes: a. The first attribute, called numSides, is of type int and represents the number of sides on the die.

b. The second attribute, called sumRolls, is static of type int and will keep track of the sum of multiple die rolls by multiple dice.

c. The third attribute, called r is of type Random and is used to generate random integer values within your program.   

4. The Die class contains a constructor, three non-static methods, and one static method:
a. The constructor takes one argument, called sides, of type int. The constructor should initialize the numSides attribute. If sides is less than 2 or larger than 100, then numSides should be set to a default value of six (6). Otherwise, numSides should be set to the value of sides. Also, the constructor should instantiate a new Random object and assign it to attribute r.

b. The public method called getNumSides() is an accessor for the numSides attribute.

c. The public method called setNumSides() is an mutator for the numSides attribute. It should have a single parameter called sides of type int.

d. A public method called getRollSum() that is also static and is an accessor for the sumRolls attribute.

e. A public method called rollDie() that generates and return a random integer value between one (1) and the value of numSides.

5. You will need to write a separate class containing a main method to test your code. The example below is provided along with the expected output to assist you. The provided example does NOT sufficiently test the Die class (but, it is a start).

public class TestDie { public static void main(String[] args) { Die d1 = new Die(6); Die d2 = new Die(10); System.out.println("Die 1 has " + d1.getNumSides() + " sides."); System.out.println("Die 2 has " + d2.getNumSides() + " sides."); d1.setNumSides(10); System.out.print("Die 1 now has "); System.out.println(d1.getNumSides() + " sides."); System.out.println("Roll on Die 1: " + d1.rollDie()); System.out.println("Roll on Die 2: " + d2.rollDie()); System.out.print("The sum of the two rolls: "); System.out.println(Die.getRollSum()); } }


Assuming that your Die class is correct, the above code should produce the following result (Hint: the values of the rolls and sum will vary for each run):

Die 1 has 6 sides. Die 2 has 10 sides. Die 1 now has 10 sides. Roll on Die 1: 9 Roll on Die 2: 8 The sum of the two rolls: 17

Homework Answers

Answer #1

// Screenshot of the code & output

// code to copy

Die.java

import java.util.Random;
public class Die {
   int numSides;
   static int sumRolls;
   Random r;
   // constructor
   public Die(int sides) {
       if(sides<2 || sides>100) {
           this.numSides=6;
       }else {
           this.numSides=sides;
       }
   this.r=new Random();     
   }
   public int getNumSides() {      
       return numSides;
   }
   public void setNumSides(int i) {
       this.numSides=i;      
   }
   public static int getRollSum() {      
       return sumRolls;
   }

   public int rollDie() {  
       int rolls= (int) (Math.random() * numSides) + 1;
       sumRolls+=rolls;
       return rolls;
   }
  
}

TestDie.java

public class TestDie {

   public static void main(String[] args) {
       Die d1 = new Die(6);
       Die d2 = new Die(10);
       System.out.println("Die 1 has " + d1.getNumSides() + " sides.");
       System.out.println("Die 2 has " + d2.getNumSides() + " sides.");
       d1.setNumSides(10); System.out.print("Die 1 now has ");
       System.out.println(d1.getNumSides() + " sides.");
       System.out.println("Roll on Die 1: " + d1.rollDie());
       System.out.println("Roll on Die 2: " + d2.rollDie());
       System.out.print("The sum of the two rolls: ");
       System.out.println(Die.getRollSum());

   }

}

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
I need to make some modifications to the code below. Numbers 1 through 3 are the...
I need to make some modifications to the code below. Numbers 1 through 3 are the required modifications. 1. Write a static method to have the players guess what was rolled 2. If guessed what was rolled you get 10 points 3. You win if you reach 30 points before the end of the round otherwise the person with the points wins at the end of five rounds The code is below import java.util.Scanner; public class ChoHan { public static...
import java.util.Scanner; import java.util.Random; public class DiceRoll {    public static final int SIDES = 6;...
import java.util.Scanner; import java.util.Random; public class DiceRoll {    public static final int SIDES = 6;    public static void main(String[] args) {        // TODO Auto-generated method stub        System.out.println("Enter the number of times a 6 sided die should be rolled ");        Scanner keyboard = new Scanner(System.in);        Random r = new Random();               int times = keyboard.nextInt();        boolean valid = false;        while(!valid) {           ...
For this assignment, design and implement a class to represent a die (singular of "dice"). A...
For this assignment, design and implement a class to represent a die (singular of "dice"). A normal bag of dice for playing Dungeons and Dragons, for example, contains dice with the following numbers of sides: 4, 6, 8, 10, 12, 20, and100. In this program, the sides (or faces) of the die are numbered, starting with one. The current face value of the die corresponds to the side that is currently facing upward. By default, the face value is set...
For this assignment, design and implement a class to represent a die (singular of "dice"). A...
For this assignment, design and implement a class to represent a die (singular of "dice"). A normal bag of dice for playing Dungeons and Dragons, for example, contains dice with the following numbers of sides: 4, 6, 8, 10, 12, 20, and 100. In this program, the sides (or faces) of the die are numbered, starting with one. The current face value of the die corresponds to the side that is currently facing upward. By default, the face value is...
In java create a dice game called sequences also known as straight shooter. Each player in...
In java create a dice game called sequences also known as straight shooter. Each player in turn rolls SIX dice and scores points for any sequence of CONSECUTIVE numbers thrown beginning with 1. In the event of two or more of the same number being rolled only one counts. However, a throw that contains three 1's cancels out player's score and they mst start from 0. A total of scores is kept and the first player to reach 100 points,...
Please explain code 1 and code 2 for each lines code 1 public class MyQueue {...
Please explain code 1 and code 2 for each lines code 1 public class MyQueue {    public static final int DEFAULT_SIZE = 10;    private Object data[];    private int index; code 2 package test; import java.util.*; /* Class Node */ class Node { protected Object data; protected Node link; /* Constructor */ public Node() { link = null; data = 0; } /* Constructor */ public Node(Object d,Node n) { data = d; link = n; } /*...
Dice Rolling) Write an application to simulate the rolling of two dice. The application should use...
Dice Rolling) Write an application to simulate the rolling of two dice. The application should use an object of class Random once to roll the first die and again to roll the second die. The sum of the two values should then be calculated. Each die can show an integer value from 1 to 6, so the sum of the values will vary from 2 to 12, with 7 being the most frequent sum and 2 and 12 being the...
1) Consider the following Java program, which one of the following best describes "setFlavor"? public class...
1) Consider the following Java program, which one of the following best describes "setFlavor"? public class Food {     static int count;     private String flavor = "sweet";     Food() { count++; }     void setFlavor(String s) { flavor = s; }     String getFlavor() { return flavor; }     static public void main(String[] args) {         Food pepper = new Food();         System.out.println(pepper.getFlavor());     } } a. a class variable b. a constructor c. a local object variable d....
Question: Squares. Write a program class named SquareDisplay that asks the user for a positive integer...
Question: Squares. Write a program class named SquareDisplay that asks the user for a positive integer no greater than 15. The program should then display a square on the screen using the character ‘X’. The number entered by the user will be the length of each side of the square. For example, if the user enters 5, the program should display the following:       XXXXX       XXXXX       XXXXX       XXXXX       XXXXX INPUT and PROMPTS. The program prompts for an integer as follows: "Enter...
Write a method that returns the sum of all the elements in a specified column in...
Write a method that returns the sum of all the elements in a specified column in a 3 x 4 matrix using the following header: public static double sumColumn(double[][] m, int columnIndex) The program should be broken down into methods, menu-driven, and check for proper input, etc. The problem I'm having is I'm trying to get my menu to execute the runProgram method. I'm not sure what should be in the parentheses to direct choice "1" to the method. I'm...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT