Advanced
Programming - CS239
LAB 17: Array Lists and other Structures
NOTE: It is not
necessary to build your ArrayList to a particular size. The ArrayList
methods keep the size to the specific number of elements in the list,
regardless of what you use as a starting number.
Getting Ready: Before
going any further you should:
- Make a directory on your N:
drive for this lab.
- Setup your development
environment.
- Copy the Card.class file to your directory for this lab.
- Download the file named PlayingCard.java
- Download the file named Driver.java
- Download the file named Deck.java
- Download the incomplete file named Hand.java
- Familiarize yourself with the Comparable interface.
Submission – Print this
worksheet and your Hand.java and Driver.java programs. Turn in all pieces. You do not need to submit your actual code.
Part I: This part of the lab will help you understand how a derived
class can implement an interface that the base class does not. REVIEW
1.
Compile and execute Driver. What is output?
(Copy your output here)
2.
Explain (in a few
sentences) the algorithm used in the compareTo() method
in the
PlayingCard class.
Part II: This part of the lab will introduce you to the ArrayList
class.
- Most
card games require “hands” or groups of cards held by a player. These hands can be of varying sizes,
depending on the game and sometimes on the state of the game.
- Complete
the Hand class which will implement the Hand as an ArrayList of Card.
- The
Hand class must have the following methods (NOTE: Be sure to use the
ArrayList methods to help you):
- void
addCard(Card newCard) - Adds a Card to this hand at the end.
- int
removeCard(Card awayCard) – Checks the value of each card and compares it
to the awayCard. If it exists,
remove it and return a 1. If it
does not exist, return a -1.
- int
removeCard(int whichCard) –Checks to make sure we have this card. Used if we know the position of the
card to remove. If it exists,
remove it and return a 1. If it
does not exist, return a -1.
- void
displayHand() – displays the value of each card in the hand.
- Add
code to the Driver.java program (see notes) that will:
- Simulate
the action of a single player.
i.
Deal (from the deck) 5 cards.
ii.
Display the hand.
iii.
Remove a specified card.
iv.
Deal a new card to replace it.
- What
is output by your driver? Cut and paste your output here.
Don’t forget to print your code and attach it to this
document.