• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

doubts in enum and generalized code

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 225
IBM DB2 Eclipse IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks david.
i thought i am missing the call to these two methods.ok
 
You would be much easier to understand if you took that bucket off of your head. And that goes for the tiny ad too!
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic