Win a copy of The Way of the Web Tester: A Beginner's Guide to Automating Tests this week in the Testing forum!

# Question about the Deck class in the Sun tutorial.

Brian Pianczk
Ranch Hand
Posts: 45

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
Posts: 50733
83
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
Not exactly... since you havent posted the Card class, I assume it would have final static ints defined as
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
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
Posts: 50733
83
You're welcome

Campbell Ritchie
Sheriff
Posts: 50733
83
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
Campbell Ritchie wrote:If it is a private static field in the Card class, how can you access it from the Deck class?

Oops