Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question about the Deck class in the Sun tutorial.

 
Brian Pianczk
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



Having just gone through the section on Arrays, again is this how this bit of code is working?



does this loop first make the DIAMONDS column then add the 13 cards to it, then create SPADES then add cards etc...?

 
Campbell Ritchie
Sheriff
Pie
Posts: 49405
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Difficult to be sure without seeing the whole of the code; where can you find the Card class?

You are declaring an array of arrays (not a two-dimensional array), then you instantiate it as a 4-member array, each member being a 13-member array. Then you fill in each of the 4 arrays with Cards, using the 13 ranks. Note the ranks appear to begin at 1, and arrays begin at 0, so you have to write - 1 in the array indices. There are obviously numbers in the Card class, DIAMONDS = 1 . . . CLUBS = 4, and ACE = 1 . . . KING = 13. There are probably also arrays in the Card class whereby suits[0] = "Diamonds" and ranks[0] = "Ace".

I don't think you have a "Diamonds" row anywhere, you have an array where every member is marked as "Diamonds". Very subtle difference, but you cannot get "Diamonds" out of the 4-member array. You can get "Diamonds" out of any of the Cards in the 1st 13-member array.
 
amitabh mehra
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not exactly... since you havent posted the Card class, I assume it would have final static ints defined as
DIAMONDS=1, ..., SPADES=4
ACE=1..KING=13

So now when you are creating an array of array of Card objects,think of it as a table, cards[row][column]
So cards[diamonds-1][ace-1] means cards[0][0] and this will now hold a card object that corresponds to the Ace of Diamond.

I hope this makes things a tad clear for you.
 
Brian Pianczk
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks to both of you. I understand now, and yes the Card class contains a private static variable for each suit, and rank.

 
Campbell Ritchie
Sheriff
Pie
Posts: 49405
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome
 
Campbell Ritchie
Sheriff
Pie
Posts: 49405
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If it is a private static field in the Card class, how can you access it from the Deck class?
 
Brian Pianczk
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:If it is a private static field in the Card class, how can you access it from the Deck class?


Oops

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic