CPSC 170 Lab 3: Polymorphism and Interfaces

Polymorphism

Consider the game of Monopoly. The game consists of a sequential set of 40 squares containing 28 properties, 6 Action squares (3 "Chance", 3 "Community Chest"), 2 Tax squares, "GO", "Jail", "Free Parking", and "Go To Jail.". The game is played by moving through the set of squares. Every time a player lands of a square, they take a different action, depending on the type of a square. For example, if you land on a "Tax Square", you must pay the bank a certain amount of money, but if you land on an "Action Square" you must draw a card from the appropriate pile and follow the instructions on the card.

Note that this is a perfect example of polymorphism. Take a deep breath and relax, you are not implementing Monopoly during this lab. However, you are going to use polymorphism to describe the gameplay. To accomplish this you should:

Comparable Interface

The files Card.java and PileOfCards.java contain classes that are similar to the Card and Deck classes that you created last semester. The card class utilizes the images in the tar file GPLCards.tgz in order to create image icons. Untar the directory of images and put it in the same directory as your java files. The PileOfCards class utilizes the Array.java class that we started in class. There is still an error with the sort method in the Array class. Before proceeding you should create a test program to help you debug the method. This is a good time to try using the debugger instead of print statements to determine where the method is failing.

Notice that there is an error in the PileOfCards sort method. This is because the Card class does not implement the Comparable interface. So, add "implements Comparable" to the header for the Card class. This produces another error, "Comparable is a raw type". This is because the interface Comparable requires the type that is being compared to be specified. So, put "<Card>" immediately after "Comparable" in the class header. Your header should now contain "implements Comparable<Card>". This will generate yet other error because implementing Comparable requires the definition of the compareTo method. Read through the Javadoc for an explanation of how the compareTo method should be implemented. Note, it is easy to decide whether a two of clubs is less than or greater than a three of clubs, but what about a two of clubs and a two of spades? It is up to you to decide what the compareTo method should return in cases like this.

The main difference between the Card and PileOfCards classes in this lab and the classes you created last semester (besides using the Array class to simplify code) is that they are designed to be drawn to a window. However, the paint methods for both classes are unfinished. Complete these two methods. The paint method in the card class is very simple, it just paints the card's image icon at the specified location. The PileOfCards class, on the other hand, paints all cards in the pile each separated by a specified number of pixels (the offset). If the offset is large enough it should be possible to see the rank and suit of all cards in the pile when it is painted.

Finally, create a program that visually demonstrates the shuffle and sort methods. The program should contain a window that displays a pile of cards. The program should also have two buttons, "Sort" and "Shuffle", that when pressed perform the action on the pile and update the display.

To submit your code: Tar the files in your lab3 directory and copy the tgz file to the directory /home/staff/bouchard/CPSC170A/lab3. Be sure to name the tar file with your names, not lab3.tgz.