CPSC 170 Lab 4: Inheritance and Polymorphism

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:

Generics and Inheritance

The files Card.java and PileOfCards.java are the classes that you created last week in lab. The pile of cards class has errors and warnings that can be fixed using generics. Recall that generics can be used to specify the type of an ArrayList. For more information, generics are covered in your book on page 574. Use generics to eliminate all of the errors and warnings in the pile of cards class.

Next, complete the shuffle and sort methods in the pile of cards class. The shuffle method can be implemented by creating a temporary pile of cards that is filled by repeatedly removing cards from the pile and adding them to the temporary pile. When the deck is empty it can be re-filled with the cards in the temporary deck. The most simple way to sort a pile of cards is by repeatedly finding the minimum valued card in the pile, removing it, and adding it to a temporary pile of cards. After all of the cards have been removed, all of the cards in the temporary pile can be moved back to the original pile. This of course depends on being able to compare the value of two cards and determine which has a lower value. This is exactly what the comparable interface is for. So, have the card class implement the comparable interface. Implementing comparable requires the definition of the compareTo method. Read through the javadoc for an explination 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. Befor moving on, create program that textually tests the shuffle and sort methods to make sure that you have no errors.

Finally, create a program that visually demonstrates the shuffle and sort methods. The program should contain a window that displays all of the cards in a pile with the cards overlapping each other from left to right (with enough of each card visible to ascertain its suit and value). The program should also have two buttons, sort and shuffle, that when pressed perform the action on the pile and update the display. The card and pile of cards classes have stubs for paint methods that you should complete and use in your program's panel class to draw the pile of cards.


Hand In: Tar your lab directory and e-mail it to your instructor with cpsc170 lab4 in the subject.