For this assignment, we will be creating at least two classes. The first will be the Die
class. The second will be the DiceTower class. The specifications for the classes are
below.
You will also be expected to create and deliver a test plan that demonstrates how
you will prove your classes fulfill requirements and are bug-free.
Use expected naming conventions and commenting.
Part I:
Die:
Die(int sides): creates a die with sides quantity of sides with values from 1 to the
quantity of sides; sets the current facing to 1
Die(int sides, int min): creates a die with sides quantity of sides with values from
min to (the quantity of sides + min – 1); sets the current facing to min
Die(ArrayList<Integer> valuesArray): creates a die with values as enumerated
from valuesArray with the sides quantity as the size of the ArrayList; sets the
current facing to the facing represented by the first value occurrence within
ArrayList
Public Die(): creates a die with 1 side with a value of 1; set the current facing to the
existing side
String toString(): returns a representation of the Die as <quantity of sides> + “ “ +
<first value > + “:” + <second value> + ...
int getQuantityOfSides(): returns the quantity of sides of the Die
int getValueAt(int sideLocation) returns value of the side at sideLocation. If
sideLocation is not within the range of the quantity of sides, print an error message.
(After we learned exception handling, you will need to handle this error by
exception handling technique)
int getCurrentValue(): returns the value of the current facing
int rollDie(): randomly sets the current facing to a valid facing and returns the
value of that facing
void setFacing(int sideLocation)sets the current location to the facing at
sideLocation. If sideLocation is not within the range of the quantity of sides, print an
error message. (After we learned exception handling, you will need to handle this
error by exception handling technique)
boolean compareDieValue(Die): returns true if both dice have the same value
regardless of quantity of sides
boolean compareQuantityOfSides(Die): returns true if both dice have the same
quantity of sides regardless of values and regardless of current value
boolean compareDice(Die): returns true if both dice have the same quantity of
sides with the same values regardless of current value
boolean compareTo(Die): returns true if both dice have the same quantity of
sides, same values, and same current facing value
When you submit this part I, please submit both Die.java and DieTest.java.
DieTest is a separate class containing method
main to test Die class we create.
What to deliver in Part I:
Java source files (10 points: 10 points each letter)
a.Die.java
b.DieTest.java
Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks
// Die.java
import java.util.ArrayList;
public class Die {
// array list to store each side value
private ArrayList<Integer> sideValues;
// index of current face
private int indexOfCurrentFace;
// constructor taking number of sides
public Die(int sides) {
// setting up list to store values between 1 and side
sideValues = new ArrayList<Integer>();
for (int i = 1; i <= sides; i++) {
sideValues.add(i);
}
// using first value as the current face
indexOfCurrentFace = 0;
}
// constructor taking number of sides and min value
public Die(int sides, int min) {
// setting up list to store values between min and min+side-1
sideValues = new ArrayList<Integer>();
for (int i = 0; i < sides; i++) {
sideValues.add(i + min);
}
indexOfCurrentFace = 0;
}
// constructor taking an array list to initialize sides
public Die(ArrayList<Integer> valuesArray) {
// copying the list
sideValues = new ArrayList<Integer>(valuesArray);
indexOfCurrentFace = 0;
}
// default constructor setting up a die with 1 side
public Die() {
// passing 1 as side value to the constructor taking number of sides
this(1);
}
@Override
public String toString() {
// appending quantity to a String variable, followed by a space
String data = sideValues.size() + " ";
// appending side values to the variable, separated by :
for (int i = 0; i < sideValues.size(); i++) {
data += sideValues.get(i);
if (i != sideValues.size() - 1) {
// appending : if this is not last side
data += ":";
}
}
return data;
}
// returns the quantity of sides
public int getQuantityOfSides() {
return sideValues.size();
}
// returns the value at given sideLocation, sideLocation starts from 0
public int getValueAt(int sideLocation) {
if (sideLocation < 0 || sideLocation >= sideValues.size()) {
// invalid
System.out.println("Invalid sideLocation!");
return -1;
}
return sideValues.get(sideLocation);
}
// returns the current value
public int getCurrentValue() {
return sideValues.get(indexOfCurrentFace);
}
// rolls the die and return new face
public int rollDie() {
// generating a random index between 0 and sideValues.size()-1
indexOfCurrentFace = (int) (Math.random() * sideValues.size());
return getCurrentValue();
}
// sets current face to a given index, sideLocation starts from 0
public void setFacing(int sideLocation) {
if (sideLocation < 0 || sideLocation >= sideValues.size()) {
System.out.println("Invalid sideLocation!");
return;
}
indexOfCurrentFace = sideLocation;
}
// returns true if two dice have same current value
public boolean compareDieValue(Die other) {
return this.getCurrentValue() == other.getCurrentValue();
}
// returns true if two dice have same number of sides
public boolean compareQuantityOfSides(Die other) {
return this.getQuantityOfSides() == other.getQuantityOfSides();
}
// returns true if two dice have same number of sides and same sides
public boolean compareDice(Die other) {
if (this.compareQuantityOfSides(other)) {
for (int i = 0; i < sideValues.size(); i++) {
if (this.sideValues.get(i) != other.sideValues.get(i)) {
return false;
}
}
return true;
}
return false;
}
// returns true if two dice have same size, same sides and same current
// value
public boolean compareTo(Die other) {
return this.compareQuantityOfSides(other) && this.compareDice(other)
&& this.compareDieValue(other);
}
}
// DieTest.java
import java.util.ArrayList;
import java.util.Arrays;
public class DieTest {
public static void main(String[] args) {
// using constructors of each type, creating 4 Dice, printing each one
// after creation
Die d1 = new Die();
System.out.println("d1: " + d1);
Die d2 = new Die(6);
System.out.println("d2: " + d2);
Die d3 = new Die(6, 10);
System.out.println("d3: " + d3);
Die d4 = new Die(new ArrayList<Integer>(Arrays.asList(1, 3, 5, 7, 9)));
System.out.println("d4: " + d4);
//rolling dice
System.out.println("\nRolling all dice");
d1.rollDie();
d2.rollDie();
d3.rollDie();
d4.rollDie();
//displaying current values
System.out.println("\nd1 current value: " + d1.getCurrentValue());
System.out.println("d2 current value: " + d2.getCurrentValue());
System.out.println("d3 current value: " + d3.getCurrentValue());
System.out.println("d4 current value: " + d4.getCurrentValue());
//demonstrating comparison methods
System.out
.println("\nd2 compareDieValue d3: " + d2.compareDieValue(d3));
System.out.println("d2 compareQuantityOfSides d3: "
+ d2.compareQuantityOfSides(d3));
System.out.println("d2 compareDice d3: " + d2.compareDice(d3));
System.out.println("d2 compareTo d3: " + d2.compareTo(d3));
System.out.println("d2 compareTo d2: " + d2.compareTo(d2));
}
}
/*OUTPUT*/
d1: 1 1
d2: 6 1:2:3:4:5:6
d3: 6 10:11:12:13:14:15
d4: 5 1:3:5:7:9
Rolling all dice
d1 current value: 1
d2 current value: 6
d3 current value: 11
d4 current value: 9
d2 compareDieValue d3: false
d2 compareQuantityOfSides d3: true
d2 compareDice d3: false
d2 compareTo d3: false
d2 compareTo d2: true
Get Answers For Free
Most questions answered within 1 hours.