• +91 9898989898
  • info@assignmentonlinehelp.com

Creating A Class To Represent Dice Using Java

Problem Description
In this assignment you will be creating a class to represent dice. 
Learning Goals
•       Learning how to create classes
•       Understanding public and private visibility
•       Specifying attributes / data members / class instance variables
•       Creating constructors
•       Creating getters and setters to access and mutate instance variables
•       Creating other instance methods
•       Validating objects
•       Testing object equality 
•       Providing a toString method

Problem Scenario
After hours and hours of audiophiliocrafting, you solved the puzzling sound with a quadrospacing-reversi-doubli-slowing-frasterization manipulation! In the end it was so simple!
What was contained in the sound file, you ask? Words, words that you could understand! It started with this awesome Shakespearean-level verse:
"Make the Dice, 
  Roll to rate,
  Avoid the fate,
  Of the wretched mice!"         // cue thunder in the background
Then there were instructions for making a Dice class (abridged non-poetic version contained below). The voice sounded like the Wanderer. Does that mean anything? 
Assignment Detailed Specifications:
 In this assignment, you will be creating a class for creating dice objects. You will be using this class in the next assignment too. 
Important (Really): None of the attributes or methods being created for this assignment are static. You will not use the keyword static anywhere in the Dice class. 
The Dice Class 
 First, create a new file in Atom and name it Dice.java. Next, import the Random class, as it will be needed later. Now write the basic code for your class, with the class name Dice. 
Add the required Javadoc comments for your class (short description, name, version/date). 
Do not include a main method for this assignment. For testing, you can create a new file that you will create Dice objects in. 
The method names mentioned below must exactly match what is listed here. The parameter types needed must also match what is expected, and the methods must return the expected type of values.
Dice Class UML Diagram 
 Important (Really): The visibility specified for attributes and methods must be followed. A plus indicates public visibility. A minus indicates private visibility. 
-  sides:   int 
-  value:   int 
+     Dice()    
+     Dice( sides:     int  )    
+     getSides(  ):    int 
+     getValue(  ):    int 
+     setSides(  sides:     int  ):    void
+     setValue(  value:     int  ):    void
+     rollDice(  ):    void
+     isValidSides(   ):    boolean   
+     isValidValue(   ):    boolean   
+     isValidDice(    ):    boolean   
+     equalsTargetValue(    targetValue:    int  ):    boolean   
+     equalsValue(    thatDice:  Dice ):    boolean   
+     equals(    thatDice:  Dice ):    boolean   
+     toString(  ):    String    
Attributes / Data Members / Instance Variables
Now create the attributes for Dice objects. The Dice class has two private data members: sides and value. 
The sides attribute is the number of sides that the Dice object has. 
The value attribute is the current value of the Dice object.
The Dice class will have two constructors. 
The default constructor creates a new Dice object with six sides and it has a starting of value one.
A value constructor will allow creating a new Dice object with a specific number of sides. It also has a starting value of one.
 The Dice class will have two getter methods: getSides and getValue. 
 Neither method has parameters. Both methods return the correct value related to the object.
 The Dice class will have two setter methods: setSides and setValue. *
 These methods each have a single parameter value. Neither method has return values.
Other Instance Methods
 You will create a rollDice method that randomly assigns the Dice value to a new number between 1 and the number of sides, both inclusive [1, sides]. Create the Random number generator object within this method. 
Object Validation Methods

 The Dice class has three methods that help determine if an object is valid. 
 This method determines if the number of sides for this object is considered valid. For the sides to be valid, the sides must equal 4, 6, 8, 10, 12 or 20. 
 This method determines if the value for this object is considered valid. For the value to be valid, it must be within the range [1, sides]. (Both inclusive.)
 This method determines if the entire object is considered valid. For a Dice object to be considered valid, it must have a valid number of sides and a valid value.

Equality Methods
 The Dice class has three equality methods.
 This method determines if this object's value is equal to a target value specified as a parameter. 
This method determines if two Dice objects each have the same values. 

 This method determines strict equality for two Dice objects. Both Dice objects are considered equal if they have the same number of sides and the same value.
The toString Method

 The toString method for a Dice object will create and return a properly formatted String object (i.e., reference). The format starts with the letter 'd' followed by the number of sides, then a colon, and finally the current value. Your solution will produce output that looks like these examples of displaying a six-sided and 20-sided Dice object

Expected Program Output 
 Your program submission will not produce any displayed results (print statements). 
Testing Your Class 

 I suggest creating a TestDice.java testing program that has a main method. Create numerous Dice objects and try out the methods on them (e.g., Dice    dice1 =     new  Dice(); ).
Commenting & Coding Standards
 Your Dice class needs Javadoc comments, including a description of the class, the author, and the version. 
All of your methods require Javadoc comments, which include a description of the method, description for each parameter, and description of the returned value. If a method does not have any parameters or a returned value, then those portions are omitted. 
Additionally, make sure to include at least one meaningful inline comment explaining an area in your program. 
Your code also needs to adhere to the course coding standards and requirements. 
Expert’s Corner - What Works and What Does Not
Using dice as a context for learning to create/design classes is a good choice because dice games are very common and fairly universal. The idea of a dice object is fairly easy to grasp. The data and operations related to dice make sense. 
* What might not make sense, is allowing someone to directly change the number of sides or the value for Dice objects that have been instantiated. Most likely, these would not be operations allowed for people using the Dice class. They are included here for testing purposes. Instead, the only mutator allowed would be rollDice, which only changes the value. 
There are some other design choices for this class that would change if it were being revised further. For instance, constantly creating a new Random object in rollDie might seem wasteful, because it is. Later you will learn about a nice way to have all objects created by the Dice class use the same Random object. 
 All of the methods required for this assignment are short and simplistic. There are no arrays or loops needed anywhere. This does not mean that this is an easy assignment, since there is a lot of new terminology and ideas that must be understood.
If you see error messages similar to this:
"error:    non-static variable   sides cannot     be referenced from a     static     context"  
Do not start adding "static" in different places attempting to fix things. Instead, make sure that you did not already use "static" and remove the offending word anywhere it is located. 
Program Submission
This programming assignment is due by 11:30 PM on the day indicated in the online course calendar.
Submit only your Dice.java file under the Assignments section in the online course shell. 
Warning: Projects that do not compile will not be accepted and will receive a grade of zero.
This program will be graded using the rubric that is linked under the Assignments section in the online course shell. Click on the specific assignment to see the correct rubric.
import java.util.Random;
// @author:
// @version: 04/042/2022 Assignment 8
// this class represent a dice with sides and value
public class Dice {
    // data members
    private int sides;
    private int value;
    //default constructor
    public Dice()
        sides = 6;
        value = 1;
    //parameterized constructor
    public Dice(int sides)
        this.sides = sides;
        value = 1;
    // Getters
    // return number of sides of dice
    public int getSides()
        return sides;
    //return the value of the dice
    public int getValue()
        return value;
    // set the numbers of sides for dice
    public void setSides(int sides)
        this.sides = sides;
    // set the value of the dice
    public void setValue(int value)
        this.value = value;
    // this method assign a random number to value
    public void rollDice()
        Random random = new Random();
        value = random.nextInt(sides) + 1;
    // this method validates if number of sides are right
    public boolean isValidSides()
        if(sides == 4 || sides == 6 || sides == 8 || sides == 10 || sides == 12 || sides == 20)
            return true;
        return false;
    // this method check if the value of the dice is correct
    public boolean isValidValue()
        if(value >= 1 && value <= sides)
            return true;
        return false;
    // this method check of the dice is valid or not
    public boolean isValidDice()
        return isValidSides() && isValidValue();
    // check if the target value equal to dice value
    public boolean equalsTargetValue(int targetValue)
        return value == targetValue;
    // check if the argument dice value equal to dice value
    public boolean equalsValue(Dice thatDice)
        return equalsTargetValue(thatDice.value);
    // check if both the dice are equal or not
    public boolean equals(Dice thatDice)
        return sides == thatDice.sides && value == thatDice.value;
    // this method display dice data members as a string
    public String toString() {
        return "d"+sides+" : "+value;

Related Posts