Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

doubts in enum and generalized code

 
AshutoshP Patil
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ganesh Gowtham
Ranch Hand
Posts: 225
IBM DB2 Eclipse IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HI

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

 
AshutoshP Patil
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you traced the program through by hand? Where did you get stuck?
 
AshutoshP Patil
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just because a method is defined doesn't mean it's called.
 
AshutoshP Patil
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks david.
i thought i am missing the call to these two methods.ok
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic