CMSI 186: Homework Assignment #3

Learning Outcomes: Students will (1) be familiar with Java classes at a basic level; (2) understand the purpose of constructors, fields, and methods; (3) understand and be able to explain the difference between instance (nonstatic) members and class (static) members of a class; (4) create, fill, and index Java arrays; (5) have a basic understanding of the differences between arrays as lists in Java; (6) be able to build and use applications with multiple classes; and (7) create simple textual user interfaces [TUI's].

Problems About Dice [Familiarity with Java Classes]

For this homework:
…you will create a dice rolling game called HighRoll. The game is to try to make the maximum possible score using a set of dice. Dice in the set can have any number of sides, with the requirement that all dice in the set have the same number of sides. There can be any number of dice in the set from one to an unlimited number. Users will play the game using an on-screen menu from which they will make a selection. After pressing ENTER the program will perform the user's desired operation. To do this, you must accomplish the following activities:

First, make public class Die which contains at the very least, the following methods:

  1. public Die( int nSides )
    // Constructor for a single die with sides numbered 1, 2, …, up to nSides
  2. public int roll()
    // Returns a value as a result of randomly rolling this die
  3. public int getValue()
    // Returns the curent value of this die which resulted from the last roll
  4. public String toString()
    // Instance method that returns a string-y representation of THIS die,
    // e.g., [11]
  5. public static String toString()
    // Classwide version of the preceding instance method
  6. public static void main( String[] args );
    // The built-in test program for this class
    // TRY TO TEST AT LEAST 10 DIFFERENT CONFIGURATIONS

Next, make public class DiceSet which contains at the very least, the following methods:

  1. public DiceSet( int k, int n )
    // Constructor for a set of k dice each with n-sides (k ≥ 1 and n ≥ four)
  2. public int sum()
    // Returns the present sum of this set of dice
  3. public void roll()
    // Randomly rolls all of the dice in this set; returns void since it just sets the values
    // Use either of the "toString()" methods to get the values in the set
  4. public void rollIndividual( int i )
    // Randomly rolls only the ith die in this set [indexed from zero]
  5. public int getIndividual( int i )
    // Gets the value of the ith die in this set
  6. public String toString()
    // Returns a string-y representation of this set of dice, e.g., [3][9][12][4]
  7. public static String toString( DiceSet ds )
    // Classwide version of the preceding instance method
  8. public boolean isIdentical( DiceSet ds )
    // Returns true iff this set is identical to the set ds passed as an argument
    // with the ordering of the dice in each set is not important
  9. public static void main( String[] args )
    // The built-in test program for this class

Finally, make public class file HighRoll.java which uses your dice set to play the game. You will need to have a main that constructs the dice set, has rolls, and displays scores from each roll. The rules are as follows:

Notes:

  1. There is a minimum number of sides that a die may have. When you think about it, a two-sided die is just a coin: heads or tails are the only values possible, corresponding to one or two, zero or one, on or off, light or dark, etc. Since this isn't a Coin class, don't use that.
  2. Likewise, there is no such thing as a three-sided die. Such an item is not possible in this 3D world we inhabit. A case could be made for a hollow prism to be a three-sided die, but we would then have to define the inside faces of each side as non-existent or not counted or some such lame excuse to prevent confusion with a six-sided die.
  3. A four-sided die would be a tetrahedron [also known as a pyramid]. You can use that, but since it will never end up on its point, it is a bit hard to define what it's value would be when rolled, without coming up with a convention to unequivocably pick a side to which you can refer as its value. If you can do that, or you can make a case in which it doesn't matter, go for it. There are ways of doing this; for example, the four-sided die in D&D…
  4. A five-sided die is possible, if you take a tetrahedron and chop off the top to make a frustum. This would look like a pyramid with the top flattened out, or you could have a prism with the top and bottom [or ends] being solid triangular shapes. You could also have a four-sided pyramid.
  5. All that being said, this IS a sort of thought problem so if you can figure out a way to use even a one-sided die [pretty boring rolling that one], feel free!
  6. You might be tempted to say You know, I'm only getting ONE value no matter what, so it really doesn't matter HOW many sides the die has as long as I have a random value when I roll it. However, by convention, each side of a die has a different number, with the default being that the top face on the die is the value of the roll. This also means if I have a six-sided die, there's no way I can get an 11 on that die unless something is drastically hinky…
  7. The final thought is, that for ANY DIE, the minimum value would be one, and the maximum value would be the number of sides that the die has. Think about a six-sided die: the minimum value of a roll is one, and the maximum value is six, the same as the number of sides.
  8. Note that you should only allow the user to quit the game by using the QUIT option from the menu. That means you will implement a good endless loop as discussed in class.

Submission Guidelines: Make a sub-directory in your repository as mentioned above, called homework02 and commit your source code into it. DON'T FORGET TO ADD A COMMIT COMMENT!