aspose file tools*
The moose likes Java in General and the fly likes doubts in enum and generalized code Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "doubts in enum and generalized code" Watch "doubts in enum and generalized code" New topic
Author

doubts in enum and generalized code

AshutoshP Patil
Ranch Hand

Joined: May 04, 2010
Posts: 32
hi i am not unable to understand the below code which uses enum with generalized code
can anybody please make me understand the below code?

List<Card> prototypeDeck = new ArrayList<Card>(52);
what is the significance of 52 here and where it is passed ?


kindly explain, thanks

Card.javaDeal.java


AshutoshP
Ganesh Gowtham
Ranch Hand

Joined: Mar 30, 2005
Posts: 225

HI

This 52 is initialCapacity sent to ArrayList(int initialCapacity) .



Thanks, Ganesh Gowtham
http://ganesh.gowtham.googlepages.com
AshutoshP Patil
Ranch Hand

Joined: May 04, 2010
Posts: 32
@ganesh thanks
i got it is one more constructor which takes int argument

can you explain me the flow of this code when we run the class Deal
by java Deal 4 5
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Have you traced the program through by hand? Where did you get stuck?
AshutoshP Patil
Ranch Hand

Joined: May 04, 2010
Posts: 32
hi
@david
i am explaining what i have understood . Please rectify me where i am wrong.

1) when there is a call to Card.newDeck() the Card class is initialized in the following order

static initializations
(a) each enum constants are constructed by default constructor of Rank,Suit respectively
(b)Then prototypeDeck is constructed from ArrayList of 52
(c)then the static block is run which adds card object to prototypeDeck in add() by getting the enum values from Rank,Suit

2)after the call to Card.newDeck(), deck gets a list of card objects then it randomizes the list by shuffle()

3)After that dealHand(deck,cardsPerHand) is evaluated for each iteration before passing it to System.out.println()
inside the body of dealHand, it returns the sorted array list (hand) which is made out using the sublist got out of deck ,
the sorting is defined in the compareTo() of Card class which first prioritizes suit then based on rank
(if they are not same suit type )

4)since the card class uses toString() , the output is printed in the manner " rank of suit "


but i am some where missing the call to

public Rank rank() { return rank;}
and
public Suit suit() { return suit;}


thanks in advance for reading patiently.


David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Just because a method is defined doesn't mean it's called.
AshutoshP Patil
Ranch Hand

Joined: May 04, 2010
Posts: 32
thanks david.
i thought i am missing the call to these two methods.ok
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: doubts in enum and generalized code