Question

For this assignment, we will be creating at least two classes. The first will be the...

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

Homework Answers

Answer #1

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

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
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...
Currently stuck on this part of a project. I am struggling with creating the String for...
Currently stuck on this part of a project. I am struggling with creating the String for creating the dice values in the boxes for the "showDice" string. I have already completed the first part which was creating the class for the Die. This is Javascript. For this part, we will create the class that allows you to roll and score the dice used in a game of Yahtzee. This will be the longest part of the project, so I suggest...
[Java] I'm not sure how to implement the code. Please check my code at the bottom....
[Java] I'm not sure how to implement the code. Please check my code at the bottom. In this problem you will write several static methods to work with arrays and ArrayLists. Remember that a static method does not work on the instance variables of the class. All the data needed is provided in the parameters. Call the class Util. Notice how the methods are invoked in UtilTester. public static int min(int[] array) gets the minimum value in the array public...
4. Programming Problem: In this problem we will keep the Mechanism interface and the two classes...
4. Programming Problem: In this problem we will keep the Mechanism interface and the two classes Car and Computer from the previous problem. In addition, we will add a new class called Mechanic. We will also create a new TestMechanic class that will contain the main method. Create a class called Mechanic with following instance variable and constants: private int cost; public static int TEST_PRICE = 10; public static int REPAIR_PRICE = 50; Have a default constructor that will set...
Using Java, please implement the Poker and PokerHand classes to provide the expected output using the...
Using Java, please implement the Poker and PokerHand classes to provide the expected output using the CardDemo class. Rules are the following: - Deck and PockerHand should be Iterable (note that you may just reuse one of the underlying iterators to provide this functionality) - Poker should implement 2 methods checking for two possible poker hands , see the comments inside the classes for details. Hint: remember how one can count the frequency of words in text? - whenever you...
Assignment 1 - ITSC 2214 I have to complete the array list for a shopping list...
Assignment 1 - ITSC 2214 I have to complete the array list for a shopping list code and can't figure out how to complete it: Below is the code Complete the three empty methods (remove(), find(), and contains()) in the ShoppingListArrayList.java file. These methods are already implemented in the ShoppingListArray class. /////////////////////////////////////////////////////////////////////////////////////////////////////////// Grocery Class (If this helps) package Shopping; public class Grocery implements Comparable<Grocery> { private String name; private String category; private int aisle; private float price; private int quantity;...
Objectives:The focus of this assignment is to create and use a recursive method given a moderately...
Objectives:The focus of this assignment is to create and use a recursive method given a moderately difficult problem. Program Description: This project will alter the EmployeeManager to add a search feature, allowing the user to find an Employee by a substring of their name. This will be done by implementing the Rabin-Karp algorithm. A total of seven classes are required. Employee (From previous assignment) HourlyEmployee (From previous assignment) SalaryEmployee (From previous assignment) CommissionEmployee (From previous assignment) EmployeeManager (Altered from previous...
IN JAVA Iterative Linear Search, Recursive Binary Search, and Recursive Selection Sort: <-- (I need the...
IN JAVA Iterative Linear Search, Recursive Binary Search, and Recursive Selection Sort: <-- (I need the code to be written with these) I need Class river, Class CTRiver and Class Driver with comments so I can learn and better understand the code I also need a UML Diagram HELP Please! Class River describes river’s name and its length in miles. It provides accessor methods (getters) for both variables, toString() method that returns String representation of the river, and method isLong()...
Hi, I'm writing a Java program that prints a grid with circles and squares that have...
Hi, I'm writing a Java program that prints a grid with circles and squares that have letters in them and it is also supposed to print the toString() function to the console window each time the application runs. This toString() function is supposed to show the tile's shape, letter, and color component (or components). Could someone please review and debug my code to help me figure out why my toString() function is not working? import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JButton;...
TO BE DONE IN JAVA Your task is to complete the AddTime program so that it...
TO BE DONE IN JAVA Your task is to complete the AddTime program so that it takes multiple sets of hours, minutes and seconds from the user in hh:mm:ss format and then computes the total time elapsed in hours, minutes and seconds. This can be done using the Time object already given to you. Tasks: Complete addTimeUnit() in AddTime so that it processes a string input in hh:mm:ss format and adds a new Time unit to the ArrayList member variable....
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT