CIS 636 Spring 2005        

Assignment 5 – Exception Handling, plus OOP in Java – Bringing Together Some Pieces              

100 points

Assigned: 02/17/2005

Due: 02/24/2005 at the start of class 

 

You may work individually or in pairs for this assignment. But all work must be the work of the person/people whose name is on the code! If working in pairs, the individual contributions should be relatively equal. One possibility is to work together tonight, then finish separately (to avoid communication difficulties).

 

Main Assignment:

We are building more pieces of the simplified card game program. The next pieces to create are a Hand class, and a driver that provides a game of “War” (perhaps a slightly different version than you played as a child).  Also, some of your previous work will be adjusted.

                Your Hand class should have a data structure to hold the cards that a player holds in their hand (which we will do in this version of War). If you are finding work so far to be pretty easy, you might investigate data structures that are even more flexible than arrays (Vectors, ArrayLists, …), but arrays are fine.  The Hand class, beasies having constructors, should be able to report the number of cards in the hand, and perhaps for ease, whether the hand is empty, and allow adding a specified card to the hand. It should also help the Player class in playing a card from the hand (some divided responsibility may be necessary).

                Your Player class should be adjusted to include a Hand.  It may need additional constructors (or modification of existing constructors).  It may need to have a “mutator” to allow setting a Hand inside the Player in case the Hand is not available when the player is constructed.  It may participate in the playing of cards in the hand and replacing them (e.g. client may ask the player, which would then ask the Hand that it possesses.

                The Deck class’s deal method should be modified to throw an exception if it is asked to deal when the deck is empty. This defends the Deck class while not requiring the Deck class to determine what should be done in that situation (it may differ from game to game). Thus, any calls to deal should catch the exception and deal with it.  It seems reasonable that the empty deck exception should pass along the deck that is empty, so it can be modified if desirable (though we probably won’t use it for this game).

                The Card class needs a method to compare two cards and declare which one is better (or if it is a tie). If we gotCard class. Unfortunately, this is somewhat game specific (e.g. what if a game has “trump?”), so the most elegant solution is probably a little too complicated at this point.

                You driver class will operate as shown in the accompanying textual version of the sample interaction. It will need to instantiate two Players and a Deck (shuffle the deck and fill out the Player’s hands – 5 per Player until we start running out).  The most recent winner should go first each turn (since going second is an advantage since the players are choosing cards out of their hand). Each player will choose the Card to play and the winner will be declared (no wars for ties to keep this simple).  After the deck is exhausted, the players will use their remaining cards, and when those are exhausted the overall wined is determined and reported.  Since this is partially an exercise in exceptions, I encourage you to use the “robust” methods in RedmondMsgIn instead of using RedmondMsgInBasic (and catch exceptions). These should throw exceptions if the user types letters for numeric inputs or if they hit the cancel button. This file is available as the first link under IO under Review on my class www page.

Hand in:

 

To avoid needless long waits, I recommend working on this on the c: drive, then upon completion, copying it to the a: drive, testing it there, creating an extra backup for yourself, then deleting it from the c: drive.

 

Miscellaneous:

·         MAKE SURE YOUR PROGRAM WORKS! (i.e. more than just removing compile errors).  Your program needs to be able to handle any valid inputs, and catch invalid values. 

·         Put YOUR NAME, and e-mail address, date in comments at the beginning of the program.

·         Also, comment any significant code to document it (each method, each loop, each if, and each significant calculation should have a comment. In particular, each method needs to have a JAVADOC compatible comment).

·         Indent code following standard conventions (indent to show that something is “inside” or “part of” the preceding code (e.g. inside if’s or loops, or statements continued on a new line).  The IDE should handle this if you hit ENTER and move on to the next line (If you make changes, you may need to TAB to make things right).

·         Name your variables meaningfully, to describe their use in the program.

·         There is a fair amount to this assignment.  A good strategy for working on a challenging program is to build and test it in parts (e.g. get the setup working, then add next capability and test that, etc)

  

Sample Interaction:


 

George Bush playing next

Which Card do you want to Play?

   0)  ten of diamonds   1)  king of clubs   2)  king of hearts   3)  jack of clubs   4)  eight of spades

2

John Kerry playing next

Which Card do you want to Play?

   0)  two of hearts   1)  queen of hearts   2)  six of clubs   3)  jack of spades   4)  two of diamonds

0

George Bush takes this point

George Bush playing next

Which Card do you want to Play?

   0)  ten of diamonds   1)  king of clubs   2)  two of spades   3)  jack of clubs   4)  eight of spades

1

John Kerry playing next

Which Card do you want to Play?

   0)  nine of spades   1)  queen of hearts   2)  six of clubs   3)  jack of spades   4)  two of diamonds

4

George Bush takes this point

George Bush playing next

Which Card do you want to Play?

   0)  ten of diamonds   1)  nine of diamonds   2)  two of spades   3)  jack of clubs   4)  eight of spades

3

John Kerry playing next

Which Card do you want to Play?

   0)  nine of spades   1)  queen of hearts   2)  six of clubs   3)  jack of spades   4)  queen of clubs

1

John Kerry takes this point

John Kerry playing next

Which Card do you want to Play?

   0)  nine of spades   1)  eight of hearts   2)  six of clubs   3)  jack of spades   4)  queen of clubs

4

George Bush playing next

Which Card do you want to Play?

   0)  ten of diamonds   1)  nine of diamonds   2)  two of spades   3)  nine of clubs   4)  eight of spades

2

John Kerry takes this point

John Kerry playing next

Which Card do you want to Play?

   0)  nine of spades   1)  eight of hearts   2)  six of clubs   3)  jack of spades   4)  eight of diamonds

2

George Bush playing next

Which Card do you want to Play?

   0)  ten of diamonds   1)  nine of diamonds   2)  ten of clubs   3)  nine of clubs   4)  eight of spades

4

George Bush takes this point

George Bush playing next

Which Card do you want to Play?

   0)  ten of diamonds   1)  nine of diamonds   2)  ten of clubs   3)  nine of clubs   4)  six of hearts

4

John Kerry playing next

Which Card do you want to Play?

   0)  nine of spades   1)  eight of hearts   2)  three of spades   3)  jack of spades   4)  eight of diamonds

1

John Kerry takes this point

John Kerry playing next

Which Card do you want to Play?

   0)  nine of spades   1)  six of diamonds   2)  three of spades   3)  jack of spades   4)  eight of diamonds

3

George Bush playing next

Which Card do you want to Play?

   0)  ten of diamonds   1)  nine of diamonds   2)  ten of clubs   3)  nine of clubs   4)  seven of diamonds

4

John Kerry takes this point

John Kerry playing next

Which Card do you want to Play?

   0)  nine of spades   1)  six of diamonds   2)  three of spades   3)  ten of hearts   4)  eight of diamonds

2

George Bush playing next

Which Card do you want to Play?

   0)  ten of diamonds   1)  nine of diamonds   2)  ten of clubs   3)  nine of clubs   4)  jack of diamonds

1

George Bush takes this point

George Bush playing next

Which Card do you want to Play?

   0)  ten of diamonds   1)  seven of clubs   2)  ten of clubs   3)  nine of clubs   4)  jack of diamonds

4

John Kerry playing next

Which Card do you want to Play?

   0)  nine of spades   1)  six of diamonds   2)  jack of hearts   3)  ten of hearts   4)  eight of diamonds

1

George Bush takes this point

George Bush playing next

Which Card do you want to Play?

   0)  ten of diamonds   1)  seven of clubs   2)  ten of clubs   3)  nine of clubs   4)  five of spades

4

John Kerry playing next

Which Card do you want to Play?

   0)  nine of spades   1)  queen of diamonds   2)  jack of hearts   3)  ten of hearts   4)  eight of diamonds

4

John Kerry takes this point

John Kerry playing next

Which Card do you want to Play?

   0)  nine of spades   1)  queen of diamonds   2)  jack of hearts   3)  ten of hearts   4)  ace of clubs

4

George Bush playing next

Which Card do you want to Play?

   0)  ten of diamonds   1)  seven of clubs   2)  ten of clubs   3)  nine of clubs   4)  three of diamonds

4

John Kerry takes this point

John Kerry playing next

Which Card do you want to Play?

   0)  nine of spades   1)  queen of diamonds   2)  jack of hearts   3)  ten of hearts   4)  five of diamonds

1

George Bush playing next

Which Card do you want to Play?

   0)  ten of diamonds   1)  seven of clubs   2)  ten of clubs   3)  nine of clubs   4)  three of hearts

4

John Kerry takes this point

John Kerry playing next

Which Card do you want to Play?

   0)  nine of spades   1)  five of clubs   2)  jack of hearts   3)  ten of hearts   4)  five of diamonds

2

George Bush playing next

Which Card do you want to Play?

   0)  ten of diamonds   1)  seven of clubs   2)  ten of clubs   3)  nine of clubs   4)  seven of hearts

1

John Kerry takes this point

John Kerry playing next

Which Card do you want to Play?

   0)  nine of spades   1)  five of clubs   2)  ace of spades   3)  ten of hearts   4)  five of diamonds

2

George Bush playing next

Which Card do you want to Play?

   0)  ten of diamonds   1)  five of hearts   2)  ten of clubs   3)  nine of clubs   4)  seven of hearts

1

John Kerry takes this point

John Kerry playing next

Which Card do you want to Play?

   0)  nine of spades   1)  five of clubs   2)  king of diamonds   3)  ten of hearts   4)  five of diamonds

2

George Bush playing next

Which Card do you want to Play?

   0)  ten of diamonds   1)  nine of hearts   2)  ten of clubs   3)  nine of clubs   4)  seven of hearts

4

John Kerry takes this point

John Kerry playing next

Which Card do you want to Play?

   0)  nine of spades   1)  five of clubs   2)  king of spades   3)  ten of hearts   4)  five of diamonds

2

George Bush playing next

Which Card do you want to Play?

   0)  ten of diamonds   1)  nine of hearts   2)  ten of clubs   3)  nine of clubs   4)  four of clubs

4

John Kerry takes this point

John Kerry playing next

Which Card do you want to Play?

   0)  nine of spades   1)  five of clubs   2)  eight of clubs   3)  ten of hearts   4)  five of diamonds

1

George Bush playing next

Which Card do you want to Play?

   0)  ten of diamonds   1)  nine of hearts   2)  ten of clubs   3)  nine of clubs   4)  four of hearts

3

George Bush takes this point

George Bush playing next

Which Card do you want to Play?

   0)  ten of diamonds   1)  nine of hearts   2)  ten of clubs   3)  ten of spades   4)  four of hearts

4

John Kerry playing next

Which Card do you want to Play?

   0)  nine of spades   1)  queen of spades   2)  eight of clubs   3)  ten of hearts   4)  five of diamonds

4

John Kerry takes this point

John Kerry playing next

Which Card do you want to Play?

   0)  nine of spades   1)  queen of spades   2)  eight of clubs   3)  ten of hearts   4)  ace of hearts

4

George Bush playing next

Which Card do you want to Play?

   0)  ten of diamonds   1)  nine of hearts   2)  ten of clubs   3)  ten of spades   4)  four of diamonds

4

John Kerry takes this point

John Kerry playing next

Which Card do you want to Play?

   0)  nine of spades   1)  queen of spades   2)  eight of clubs   3)  ten of hearts   4)  seven of spades

1

George Bush playing next

Which Card do you want to Play?

   0)  ten of diamonds   1)  nine of hearts   2)  ten of clubs   3)  ten of spades   4)  six of spades

4

John Kerry takes this point

John Kerry playing next

Which Card do you want to Play?

   0)  nine of spades   1)  ace of diamonds   2)  eight of clubs   3)  ten of hearts   4)  seven of spades

1

George Bush playing next

Which Card do you want to Play?

   0)  ten of diamonds   1)  nine of hearts   2)  ten of clubs   3)  ten of spades   4)  four of spades

4

John Kerry takes this point

John Kerry playing next

Which Card do you want to Play?

   0)  nine of spades   1)  three of clubs   2)  eight of clubs   3)  ten of hearts   4)  seven of spades

4

George Bush playing next

Which Card do you want to Play?

   0)  ten of diamonds   1)  nine of hearts   2)  ten of clubs   3)  ten of spades   4)  two of clubs

1

George Bush takes this point

George Bush playing next

Which Card do you want to Play?

   0)  ten of diamonds   1)  ten of clubs   2)  ten of spades   3)  two of clubs

0

John Kerry playing next

Which Card do you want to Play?

   0)  nine of spades   1)  three of clubs   2)  eight of clubs   3)  ten of hearts

1

George Bush takes this point

George Bush playing next

Which Card do you want to Play?

   0)  ten of clubs   1)  ten of spades   2)  two of clubs

0

John Kerry playing next

Which Card do you want to Play?

   0)  nine of spades   1)  eight of clubs   2)  ten of hearts

1

George Bush takes this point

George Bush playing next

Which Card do you want to Play?

   0)  ten of spades   1)  two of clubs

0

John Kerry playing next

Which Card do you want to Play?

   0)  nine of spades   1)  ten of hearts

0

George Bush takes this point

George Bush playing next

Which Card do you want to Play?

   0)  two of clubs

0

John Kerry playing next

Which Card do you want to Play?

   0)  ten of hearts

0

John Kerry takes this point

George Bush has 10.0 points

John Kerry has 16.0 points

John Kerry wins!