Name(s): _______________________
COSC 110 A, Activity 1
Trying to Read Source Code
Tom Linton, January 21, 2005
You may works in groups of 2 or 3 (or alone) and you only need to submit one paper for each group. Our goal today will be to attempt to make some sense out of actual Java source code. While we know way too little Java to understand most code, it is written in English (at least partially), so we might be able to copy code and make a few small changes. Working with at least one other person should help, as it is almost always the case that the group is smarter than the sum of the individuals. Be sure to ask your partner(s) questions and don't be afraid to speak up within your group. You are also encouraged to ask Tom or other groups questions!
  1. One of you should login to your Central account. After doing so, navigate (through your "My Computer") to the Lintont folder on the G: drive. Go into the introcs subfolder and grab a copy of the Activity1 project (its a folder) and save it to your H: drive (inside your chapter1 folder, if you made one of those).

  2. Start BlueJ (press Start -> All Programs -> Programs for Computer Science -> BlueJ) and open up the Activity1 project from your H: drive. Compile the project if any of the Classes are striped. Right click on the Card class and select the constructor without any parameters. Once your card object appears on the object bench, right click it and select "Inspect". An inspector window should open that shows the 2 fields of your newly created Card. Both fields are labeled "private" which we'll ignore for today. What "type" is the first field, and what is the name of that field? What value is stored in that field?








  3. What "type" is the second field, and what is the name of this second field? What value is stored in the second field?








  4. Now right click on the Class Card once again (not the red card object, but the tan Card Class) and select the other constructor. This constructor requires 2 parameters and both are Strings. The first is a String for the suit of the Card (like "clubs" or "hearts"), and the second is a String for the face value of the Card (like "ace" or "7"). Type in values for these 2 parameters (don't forget the double quotes) and click OK. You should now have 2 instances of the Card class on your object bench.

    Every Card object has a method whose signature is

         String makeString( )

    Does this method have any parameters? How can you tell?





    Does this method return anything? If so, what type of thing does it return?




  5. Right click on the second instance of the Card class (the one where you typed in values for its fields) and invoke its makeString( ) method. What does this method do (just explain what happened in common terms)?





  6. Create a Hand object (use the constructor with no parameters). Once your Hand object appears on the object bench, right click it and select the addCard( ) method. This method requires an object (namely a Card) as its only parameter. There are two ways to enter an object into the parameter entry box that appears when you invoke the addCard( ) method. You can just type in the name of some object (no quotes since this is a name, not a String), but that object needs to be in the object bench (the object has to exist), or you can click on some object in the object bench, and BlueJ will insert that object's name into the parameter entry box for you. Add your 2 cards to your hand (try adding one by typing and one by clicking), and then invoke the printHand( ) method of your Hand object. What does the printHand( ) method do?







  7. The printHand( ) method should have opened up a "terminal window" (a somewhat primitive "text display" area). That terminal window has an "options" menu that you can click to erase the contents of the terminal window (if it gets to cluttered with old printings). Invoke the numberOfCards( ) method of your hand object. What result did it return? What does the result mean?




  8. Use the addRandomCard( ) method of your hand object to add more random cards to your hand until you have a total of five cards. Call the numberOfCards( ) method to make sure your total is five, and then call the printHand( ) method of your hand object. This allows you to see all the cards in your hand via the terminal window. The cards are printed out in the order they appear in the hand, but Java uses an odd (but very useful, once you get used to it) indexing scheme called "zero-based indexing". That means that the "first" card is at index 0, the second card is at index 1, and so on, up to the last card, which is at index 4 (not 5 as you might expect). Every instance of the Hand class has a method to "draw" new cards, and this method, named replaceCardAt( ), requires the index (from 0 to n - 1, if your hand has n cards in it) of the card you would like to replace. Pretend you're playing poker and can "draw" up to three new cards (but you can't replace any card more than once). Use the replaceCardAt( ) method (up to 3 times) to try and improve your poker hand. Use the printHand( ) method to see your final hand at write it down in the space below.




  9. While adding random cards to your hand, you probably noticed that the return value (a String telling you what card was added) and the fact that you can only add one card at time, are somewhat annoying properties of the addRandomCard( ) method. Invoke your hand object's emptyHand( ) method (which is supposed to remove all of the cards in the hand), and then call the addRandomCards( ) method (it requires a parameter of type int), to add 5 random cards to your hand. What parameter value is required if you want to add 5 cards to your hand?




  10. Now let's move on. Right click each object on your object bench and remove them all, then clear the terminal window. Create an object of the BlackJack class and invoke its deal( ) method. Use BlueJ to investigate the various methods of your blackjack object. When you figure out what each method does, double click the BlackJack class to open the source code editor. While the Hand class and even some parts of the Card class are very complex, the BlackJack class is relatively simple (because it more or less just uses the methods you've been investigating earlier). Some of the keywords won't mean much to you (private, public, etc.), and the printing method has some very strange language (which you really don't need to understand now), but your goal is to mimic this source code and write a Poker class that does for a 5 card poker hand what the BlackJack class does for the game of 21.
  11. In the main BlueJ window (the one with the object bench and Class diagram) click on the New Class button, type in Poker (with a capital P) for your new class name and make sure the class type is set to "Class".
  12. Double click on the Poker Class to open the editor.
  13. Highlight all of the text in this new editor window and delete it.
  14. Now move to the editor window for the BlackJack class, highlight all of the text and copy and paste it over to your Poker class editor window. The Poker class will have lots of similarities to the BlackJack class, so we may as well save some typing time.
  15. At the top of the Poker class, edit the comments so that they describe a Poker game, instead of a BlackJack game. Be sure to add the names of everyone in your group to the author line. You will eventually hand this file in, so you must record everyone's name.
  16. The Poker class will have exactly the same fields (no need to change the names of the fields) as the BlackJack class, but the line that defines the class (it looks something like
         public class BlackJack
    needs to have "BlackJack" changed to "Poker". Make this change.
  17. Likewise, the constructor for the Poker class can contain exactly the same "body" or code as the constructor for the BlackJack class, but once again the name has to change from BlackJack( ) to Poker( ). Make this change.
  18. The deal( ) method needs only the following change: Instead of adding 2 random cards to each hand, you need to add 5 random cards for a Poker hand. You have two options here, just call the addRandomCard( ) method of each hand 5 times instead of 2, or replace the 2 calls to the addRandomCard( ) method with a single call to the addRandomCards(5) method for each hand. Make these changes now.
  19. We've now made several changes to our Poker class, so it would be a very good idea to try and compile this class now. In the editor window of the Poker class, click the compile button. If you get an error, call Tom over. At this point you need well trained eyes to spot errors, and many times, the error message isn't helpful, until we know much more about programming in Java. Once it compiles, create a Poker object and call its deal method to make sure things are working as they should.
  20. The printHand( ) method can stay as it is, letter for letter, no changes needed at all.
  21. The two methods dealerHit( ) and playerHit( ) don't make any sense for Poker (but don't delete them, the form of some of the code may be useful). We really need methods to allow each player to replace certain cards in their hand. This is a bit tougher than taking a hit in blackjack, since we need to know the index of each card we want to replace. This means that our dealerDraw( ) and playerDraw( ) methods require additional information (what is the index of the card we want to replace), so they'll need parameters. Furthermore, in the game of poker, you normally receive all of your drawn cards at once (not one at time, where you might be allowed to change your mind after seeing the first of 3 drawn cards). Thus, we should really have three drawCard( ) methods for each player. The first allows the player to draw a single card, and the index of that card would be the only parameter. The second would allow the player to draw 2 cards, and this would require 2 parameters, one for the index of the first card to replace, and another for the second card's index. The last draw method would be designed for drawing 3 cards and that would require three parameters. In Java, when methods perform similar tasks, but require a different number of parameters, you can actually give them all the same name (and this a convention that we should adopt). Thus, instead of naming our methods something like dealerDraw1( ), dealerDraw2( ), and dealerDraw3( ), it is probably better to name them all dealerDraw( ) (the number of parameters in each version of dealerDraw( ) will indicate how many cards you want to draw). Here is correct code for the dealer's draw method that allows them to draw 2 cards. Carefully type this into your Poker class editing window, compile your Poker class and create an instance of the Poker class to make sure things work as desired. Be sure to try out the new dealerDraw( ) method.

       /**
         * Allow the dealer to draw 2 cards at the given indexes
         *
         * @param index1  The index of the first card to replace.
         * @param index2  The index of the second card to replace.
         */
        public void dealerDraw(int index1, int index2)
        {
            // Replace the 2 cards
            dealer.replaceCardAt(index1);
            dealer.replaceCardAt(index2);
            // Print the dealers new hand.
            System.out.print("\nThe dealer's new hand is: ");
            dealer.printHand();
        }
       
  22. Once you have your dealerDraw( ) method for 2 cards working well, add a playerDraw( ) method for 2 cards. The copy-paste-edit cycle will be handy here, as the only changes required should be changing all occurrences of "dealer" to "player". Compile your Poker class again and make sure things are still working as desired.
  23. Finally, see if you can't figure out how to add 2 more copies of these draw card methods for each person (the dealer and the player), one that draws 1 card, and one that allows people to draw three cards. Your new methods can have the same names as the last 2 methods you created, but need to have different numbers of parameters.
  24. Once you have a working Poker class, close down BlueJ and open a web browser. Go to the Blackboard site (http://blackboard.central.edu/webapps/portal/frameset.jsp) for this class and click on the assignments tab. You should see an Activity 1 assignment. Click on the "View\Complete Assignment: Activity1" link. Enter the names of everyone in your group in the comment area, then click your mouse in the "File To Attach" field and finally click on the "Browse" button. Navigate to your H: drive and then into your Activity1 folder. Find the file named "Poker.java" and attach that file (by double clicking it). Back in the Blackboard-View/Complete window, click on the "Submit" button. NOTE: the "Save" button does NOT turn your assignment in, it only allows you (not me) to save your work and return to it later. The "Save" button is irrelevant for us, so don't use it.