File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Can you explain why my getter and methods work? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Can you explain why my getter and methods work?" Watch "Can you explain why my getter and methods work?" New topic
Author

Can you explain why my getter and methods work?

Gary Charles
Ranch Hand

Joined: Oct 29, 2012
Posts: 32
I'm self-studying from Java How to Program 8e and doing end-of-chapter(7) exercises. The chapter provides three classes for creating and testing a deck of cards. My job as student is to "Modify the application of Fig. 7.11 to deal a five-card poker hand. Then modify class DeckOfCards of Fig. 7.10 to include methods that determine whether a hand contains
  • a) a pair
  • b) two pairs

  • .
    .
    .
    [Hint: Add methods getFace and getSuit to class Card of Fig. 7.9]"


    I've done the first two parts Modify the application of Fig. 7.11 to deal a five-card poker hand. Then modify class DeckOfCards of Fig. 7.10 to include methods that determine whether a hand contains ... add methods getFace and getSuit , and now after enough trial and error it works.

    My question is I don't understand how my getter in Card.java is able to determine the face and suit? I know that I'm calling the getFace and getSuit method on the hand array(?) of type Card that I created. I guess that getFace and getSuit are using the instance variables in Card.java to return what I've told it to return "face and suit". But there's still a disconnect for me somewhere and I'm not sure where. Here's my theory but I'm not sure how to verify it.
    The Card object is created with the constructor and during instantiation in DeckOfCards.java on line 26 the suit and face are sent along. So the object contains the suit and face. Then when I make a new array on line 40 and assign it on line 41, the fields(?) suit and face are sent along with it as well. Since the fields suit and face are of type Card the getter has the ability to retrieve them. Kind of fuzzy. Is that close to accurate?


    Here are my .java files. I've taken out the method where the cards get shuffled and the method that deals 52 cards and displays them. My additions are methods oneHand and getFaceAndSuit in DeckOfCards.java and getFace and getSuit in Card.java.









    Here's my output demonstrating that it works.




    Thank-you if you read this far.
    Gary
    Jayesh A Lalwani
    Bartender

    Joined: Jan 17, 2008
    Posts: 2052
        
      22

    Gary Charles wrote:

    The Card object is created with the constructor and during instantiation in DeckOfCards.java on line 26 the suit and face are sent along. So the object contains the suit and face. Then when I make a new array on line 40 and assign it on line 41, the fields(?) suit and face are sent along with it as well. Since the fields suit and face are of type Card the getter has the ability to retrieve them. Kind of fuzzy. Is that close to accurate?
    Gary


    The suite and face aren't "sent along".

    Remember that in Java, variables hold references to Object, not the objects themselves. Also, Arrays contains references to objects too, not the object themselves. On line 21, you create an Array, and what that means is that an empty array is creates, and the reference to the array is stored in deck. In line 26, you create a Card object and store it's reference in the array

    Now in line 40, you create another array of size 1. In line 41, you copy the reference of the first card in the deck to the first element in the hand array. Remember that the reference is copied, not the Object.. both references point to the same Card object. so, doing hand[0].getFace() is doing the same thing as deck[0].getFace()

    I know it can get confusing. It will help if you make a little drawing that shows which variables contain references to which objects

    Edited: You might want to read Cup Size campfire story
    Gary Charles
    Ranch Hand

    Joined: Oct 29, 2012
    Posts: 32
    Thank-you Jayesh. This part of your explanation really helped. Plus I forgot about references. Most of my study has been with primitives or using references and not really being aware of the differences(e.g. when using arrays).

    Gary
    Jayesh A Lalwani
    Bartender

    Joined: Jan 17, 2008
    Posts: 2052
        
      22

    You are very welcome. It really really helps to draw diagrams until you get the hang of it.
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Can you explain why my getter and methods work?
     
    Similar Threads
    can't split card shuffle method into a separate class
    Error
    class question
    cards game
    Help With 5 card stud program