• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Can you explain why my getter and methods work?

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
     
    Rancher
    Posts: 2759
    32
    Eclipse IDE Spring Tomcat Server
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    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
    Posts: 32
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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
    Rancher
    Posts: 2759
    32
    Eclipse IDE Spring Tomcat Server
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    You are very welcome. It really really helps to draw diagrams until you get the hang of it.
     
    Are you okay? You look a little big. Maybe this tiny ad will help:
    Smokeless wood heat with a rocket mass heater
    https://woodheat.net
    reply
      Bookmark Topic Watch Topic
    • New Topic