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:

  1. Make a directory on your N: drive for this lab.
  2. Setup your development environment.
  3. Copy the Card.class file to your directory for this lab.
  4. Download the file named PlayingCard.java
  5. Download the file named Driver.java
  6. Download the file named Deck.java
  7. Download the incomplete file named Hand.java
  8. 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.        1.      Compile and execute Driver. What is output?  (Copy your output here)

   «Ï ----jGRASP exec: java Driver
ÏϧÏ
ÏϧÏAce Clubs
ÏϧÏ4 Clubs
ÏϧÏQueen Diamonds
ÏϧÏAce Spades
ÏϧÏ4 Hearts
ÏϧÏ
ÏϧÏ
ÏϧÏ
ÏϧÏ4 Hearts
ÏϧÏ4 Clubs
ÏϧÏQueen Diamonds
ÏϧÏAce Clubs
ÏϧÏAce Spades
ÏϧÏ
ÏÏ©Ï ----jGRASP: operation complete.

 

 

 

2.      Explain (in a few sentences) the algorithm used in the compareTo() method in the

PlayingCard class.

 

The suits are ordered as are the rank (value) and points are awarded based on suit and rank.  The points are retrieved for both cards based on their ranks (values) .  If the points are different, either 1 or -1 is returned (based on whether this.card’s rank points are greater than or less than other.card’s rank points.  If the points are the same, the points are retrieved for both cards based on their suits.  These can not be the same (since their ranks were) and there are no duplicate cards in the deck.  The value returned is again either 1 or -1 based on whether this.card’s suit points are greater than or less than other.card’s suit points. 

 

 Part II: This part of the lab will introduce you to the ArrayList class. 

 

  1. 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.
  2. Complete the Hand class which will implement the Hand as an ArrayList of Card.
  3. The Hand class must have the following methods (NOTE:  Be sure to use the ArrayList methods to help you):
    1. void addCard(Card newCard) - Adds a Card to this hand at the end. 
    2. 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. 
    3. 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. 
    4. void displayHand() – displays the value of each card in the hand.
  4. Add code to the Driver.java program (see notes) that will:
    1. 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.

  1. What is output by your driver? Cut and paste your output here  (see below). 

Here are some reminders about coding style:

§       Separate declarations and instantiations

§       Remove unused code

§       Don't do more work than necessary

§       Print headers

§       Only have 1 return statement per method unless the method is recursive.

Produce output to show your code works (i.e. test it)

 ÏÏ
ÏÏ«Ï ----jGRASP exec: java Driver
ÏϧÏ
ÏϧÏAce Clubs
ÏϧÏ4 Clubs
ÏϧÏQueen Diamonds
ÏϧÏAce Spades
ÏϧÏ4 Hearts
ÏϧÏ
ÏϧÏ4 Hearts
ÏϧÏ4 Clubs
ÏϧÏQueen Diamonds
ÏϧÏAce Clubs
ÏϧÏAce Spades
ÏϧÏ
ÏÏ§Ï Part II output follows
ÏÏ§Ï Original hand
ÏϧÏ[Ace Diamonds, 8 Spades, 7 Hearts, Jack Diamonds, 4 Hearts]
ÏϧÏAce Diamonds
ÏϧÏ8 Spades
ÏϧÏ7 Hearts
ÏϧÏJack Diamonds
ÏϧÏ4 Hearts
ÏϧÏ
ÏÏ§Ï Modified hand
ÏϧÏ[Ace Diamonds, 8 Spades, 7 Hearts, 4 Hearts]
ÏϧÏAce Diamonds
ÏϧÏ8 Spades
ÏϧÏ7 Hearts
ÏϧÏ4 Hearts
ÏϧÏ
ÏÏ§Ï Re-Modified hand
ÏϧÏ[Ace Diamonds, 8 Spades, 7 Hearts, 4 Hearts, 8 Clubs]
ÏϧÏAce Diamonds
ÏϧÏ8 Spades
ÏϧÏ7 Hearts
ÏϧÏ4 Hearts
ÏϧÏ8 Clubs
ÏϧÏ
ÏϧÏ
ÏÏ©Ï ----jGRASP: operation complete.
Ï 

 

Don’t forget to print your code and attach it to this document.