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.
Read (and interact with) my notes on Java Basics from the section on “Classes” to the end.
You’ve become really good at video games but have started to wonder what games were like before Pong. Did people play games on...the...command line? What was it like to play a game just with text? You decide to try writing such a game in Java, complete with old-fashioned text menus and words you type in to make things happen. And what a great idea this is! You get to practice with Java classes.
As in the previous labs, go to GitHub, select Import Repository. Then:
The import might take a few minutes. When done, clone the repo and get ready to work.
The starter code has three classes for you to fill in.
This class represents a single die, with an immutable number of sides (at least 4) and a mutable current value (between 1 and the number of sides). The interface of the class is to be as follows:
• SIX_SIDED_DIE_EMOJI • Die(int sides, int value) • int roll() • int getSides() • int getValue() • String toString()
See the comments in the starter code, and the tests, for the complete specification of each of these constructors and methods.
A collection of at least two Die objects, all with the same number of sides. You are not allowed to add or remove die objects from a DiceSet once constructed. But you are allowed to roll the dice in the set. The interface is to be:
• DiceSet(int sidesOnEachDie, int numberOfDice) • DiceSet(int sidesOnEachDie, int... values) • String descriptor() • int sum() • void rollAll() • void rollIndividual(int i) • int getIndividual(int i) • List<Integer> values() • boolean isIdenticalTo(DiceSet diceSet) • String toString()
See the comments in the starter code, and the tests, for the complete specification of each of these constructors and methods.
This is to be a console-based application run from the command line. The program should begin by printing a welcome message. Then it repeatedly asks the user to enter a command and carries it out. Each command will either print a response or an error message, followed by a blank line. If the command is q
, the program will cleanly exit.
The commands to support are:
Note that not every method form the Die and DiceSet class are necessarily used in this game. That’s perfectly fine. When you write classes like Die and DiceSet, you want them to be used widely by many people, not just by you.
Here is an example run of the program, to give you a better feel for what you are to write:
$ javac HighRollerGame.java && java HighRollerGame Welcome 🎲🎲🎲🎲🎲 Enter a command (h for help): high No high score registered yet Enter a command (h for help): roll all You don't have any dice yet Enter a command (h for help): asfhsdkjlfh I don't understand Enter a command (h for help): use 5 6 You are now using a 6d5 [1][1][1][1][1][1] Enter a command (h for help): high Highest score so far is 6 Enter a command (h for help): roll 3 [1][1][1][5][1][1] Enter a command (h for help): high Highest score so far is 10 Enter a command (h for help): roll all [3][1][1][2][5][5] Enter a command (h for help): highest Highest score so far is 17 Enter a command (h for help): use 20 5 You are now using a 5d20 [1][1][1][1][1] Enter a command (h for help): roll all [5][18][6][7][20] Enter a command (h for help): high Highest score so far is 56 Enter a command (h for help): use 1 20 Dice must have at least four sides Enter a command (h for help): use 5 1 At least two dice required Enter a command (h for help): h h or help : Prints this message q or quit : Quits the program use <n> <s> : Get a new dice set with n dice of s sides each roll all : Roll all the dice in your current dice set roll <i> : Roll the ith die of your current dice set high or highest : Prints the highest roll so far Enter a command (h for help): use 5 2 You are now using a 2d5 [1][1] Enter a command (h for help): roll I don't understand Enter a command (h for help): roll all [1][3] Enter a command (h for help): q I'm glad you played today. You look great!
Notice from the output above, and make sure to implement in your solution, that:
Unit test suites for Die and DiceSet have been provided for you. While developing your code, run these as follows:
$ javac DieTest.java && java DieTest $ javac DiceSetTest.java && java DiceSetTest
No tests have been provided for the actual Game class. You are encouraged to try your hand at writing some, though.
Online:
HighRoller
class, as well as the test files from the starter code unchanged. Please make sure the code is beautiful, with well-chosen names, perfect formatting, and appropriate comments (if called for). Don’t forget to update the README.On hardcopy or email:
use
command in the HighRoller game, did you allow an arbitrary number of spaces after the word use
and between the two operands? If so, how did you do this?roll
command in the HighRoller game, did you allow an arbitrary number of spaces after the word roll
? If so, how did you do this?