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 Question about the Deck class in the Sun tutorial. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Question about the Deck class in the Sun tutorial." Watch "Question about the Deck class in the Sun tutorial." New topic
Author

Question about the Deck class in the Sun tutorial.

Brian Pianczk
Ranch Hand

Joined: Jan 26, 2009
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

Joined: Oct 13, 2005
Posts: 39869
    
  28
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

Joined: Dec 05, 2006
Posts: 98
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

Joined: Jan 26, 2009
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

Joined: Oct 13, 2005
Posts: 39869
    
  28
You're welcome
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39869
    
  28
If it is a private static field in the Card class, how can you access it from the Deck class?
Brian Pianczk
Ranch Hand

Joined: Jan 26, 2009
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

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Question about the Deck class in the Sun tutorial.