CMSC331 Programming Languages, Fall 2003

Homework 6
Java II
It's in the Cards

out: 10/16, due: 10/23

This assignment asks you to write some more Java and to use the BlueJ Java Integrated Development Environment. It will give you some practice with objects and methods. The assignment asks you to develop some simple classes to use in card games. The next assignment will build on this by using these classes to simulate playing a game much like blackjack. When you write the classes for HW6, you are required to use the BlueJ IDE and to submit your project as a BlueJ project.

BlueJ

BlueJ (http://www.bluej.org/) is a simple integrated development environment (IDE) for Java. It was developed to use in teaching object-oriented programming and Java. As IDEs go, it's rather basic, which is one of it's advantages (IDEs can be very complex and take a long time to master). BlueJ is implemented in Java and distributed as an open source system. You should either download and install it on your own computer or learn how to use it on the UMBC OIT Lab machines. For instructions, see here.

Language specific IDEs started appearing in the 1980's. They are very common for Java, partly because Java makes them easy to implement in Java because of reflection (more on this later). Here's a nice list of Java IDEs and here is another.

Read the BlueJ tutorial from http://www.bluej.org/tutorial/tutorial.pdf. Start up BlueJ and try opening some of the example projects that are installed with the BlueJ system. Note -- inspecting these files reveals that they comprise some ordinary Java files plus some extra information used by BlueJ. If you are running BlueJ on one of the OIT machines, then you should copy this directory so someplace to which you can write, otherwise you will not be able to compile any of the examples. Try compiling the examples and running them. Exercise the debugger by setting some breakpoints, single stepping, etc.

The Classes

You have to define five classes: Game, Player, Deck, Card and Hand. While you are free to come up with a design of your own, we recommend the following.

A Game is really just a stub for this assignment, and consists of some code to add players to the game, create a new deck and deal one cards to each player. A Game should several instance variables: a collection of players and a deck of cards.

A Player should have a name (a string) and a hand of cards.

A deck is a collection of cards. There are several important methods on a deck, including shuffle and deal. When the shuffle method is called, the deck is initialized to include the 52 initial cards of cards, one for each rank and suit combination. Calling the deal method returns a random card from the deck and also has the side effect of removing that card from the deck.

A Hand is a collection of cards. Methods a hand should have include addCard which takes a card and inserts it into the Hand.

A card has a rank which is an integer from 1 to 13 and a suit which one of Spades, Hearts, Diamonds, and Clubs. Cards have a name which is a string of the form "rankname of suit" where rankname is Ace for 1, Jack for 11, Queen for 12, King for 13, and the corresponding integer for ranks 2-10.

Here's how your classes should look in BlueJ. The connections represent dependencies and hold between a class A and class B when A refers to or uses B. The "page" icon in the upper left corner is the README file for the project.

Your assignment

Figure out what the various classes and objects should be, and what their instance variables and their methods should be. There is no single "correct" design; any reasonable design will do. However, we may take off a lot of points for really bad design, such as (for instance) putting everything in a single class. The better your design, the easier your programming will be for this assignment and the next one.

The Game class should have a main method that accepts command line arguments that are the names of players. For each name, create a player. Once all of the players are at the table, the play begins and consists of dealing one card to each player. You should generate output to describe what is going on like the following:

% java Game Bart
Bart joins the game.
The game begins.
Bart gets 3 of diamonds.
The game ends.

% java Game The game begins. The game ends.
% java Game Matt Kitty Doc Chester Matt joins the game. Kitty joins the game. Doc joins the game. Chester joins the game. The game begins. Matt gets Queen of spades. Kitty gets Jack of hearts. Doc gets King of spades. Chester gets 5 of hearts. The game ends.

In finding a representation for a deck or a hand, you will need to represent a collection of cards. The size of the collection will change over time as, for example, cards are removed from the deck via the deal method and subsequently added to some player's hand. The deal method of a deck can be easily implemented by generating a random integer between one and the number of cards currently in the deck, and then removing and returning the card at that position. Java has two classes that are good for representing dynamic arrays of things -- ArrayList and Vector. These are actually very similar and either one will serve you well. Here's a page that addresses the question of which is better. If you use either of these in a class file, be sure to put the appropriate import statement (i.e., import java.util.*) at the top of the file.

When randomly select a card from a deck or a Hand, you will find either Math.random or the Java Random class will be useful here. Again, if you use one of these you will have to put the appropriate import statement in the class file.

Follow the java coding standards described in Sun's Code Conventions for the Java Programming Language. Include appropriate javadoc compatible comments for all of the significant classes and methods. Use the project documentation command in BlueJ's Tools pull down menue to generate the documentation pages.

What to hand in

Deprecated: Your program is due before midnight, Thursday 23 October. Zip or tar up all files (.java, .class, and any extra files produced by BlueJ, such as .pkg and .pkh files), and submit the result.

Important change as of 19 October: Your program is due before midnight, Thursday 23 October. Create an executable jar file (e.g., hw6.jar) from your project in BlueJ using the export command). Submit this file using the unix submit project. When doing so, be sure to check the option to include your source files. If you don't we will not be able to grade your homework. We suggest you use the Unix jar command to check your jar file when you submit it to ensure that it is executable and includes your source code -- do a "jar tf" on your file to see the table of contents. Verify that the file is executable by executing it, e.g. java -jar hw6.jar. Some information on jar files is available.