• 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
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Quick question about creating objects

 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Could someone please explain to me why we have to create the deck object twice in order for this piece of code to work? From what I can tell, it looks as if the first Card object is only used for the 'for' condition, and is replaced by the new card object?
This is an example from my textbook. The book doesn't talk about this portion. I am not sure how to phrase this question to use google, either.

Any help would be greatly appreciated!

 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jeong Ryu wrote:
Could someone please explain to me why we have to create the deck object twice



You don't. You're only creating it once. Right here:

 
lowercase baba
Posts: 13091
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
your deck appears to be an array of cards. You create the deck on line 1, as Jeff said - but at that point, there is nothing in it.

The for loop goes through and on each iteration, creates a Card with a face and suit, and inserts it into the deck in each position.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

fred rosenberger wrote:your deck appears to be an array of cards. You create the deck on line 1, as Jeff said - but at that point, there is nothing in it.



There are NUMBER_OF_CARDS null references. That's something. (Okay, I guess semantically it's nothing.)
 
Jeong Ryu
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the replies everyone!

Sorry, I should have been more specific.


I know that line 1 for sure creates the deck object with quantity NUMBER_OF_CARDS.

On line 5, I moved my cursor on 'Card' and it was referring to my Card class. But, I am not sure why you would need to use 'new' or have to access the Card class.


What exactly is line 5 stating?
As Fred Rosenberger said, it seems that the for loop is creating, or giving values, to each elements in the deck array (0-51). However, how can you have two columns (face and suit) when the deck object is first initialized with NUMBER_OF_CARDS, which is a final value of 52? Wouldn't you have to have two different arrays for faces and suits?
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This line creates an array that holds NUMBER_OF_CARDS references to card objects. All the references are initially null.



This line creates one new Card object, passing values for its face and suit to the constructor, and then assigns a reference to that Card to an element of the Card array.


So each array element holds a single value--a reference that points to a Card object. That Card object in turn has to attributes: rank and suit.
 
Jeong Ryu
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jeff Verdegan wrote:This line creates an array that holds NUMBER_OF_CARDS references to card objects. All the references are initially null.



This line creates one new Card object, passing values for its face and suit to the constructor, and then assigns a reference to that Card to an element of the Card array.


So each array element holds a single value--a reference that points to a Card object. That Card object in turn has to attributes: rank and suit.



Ahh, I get it. That makes sense. I got confused with the syntax for objects and for arrays.
Thank you!
 
I claim this furniture in the name of The Ottoman Empire! You can keep this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic