Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Please please help me

  1. #1
    Junior Member
    Join Date
    Sep 2009
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Please please help me

    Hello all!!

    Please please help me..Iīve got stuck, and really bad to. I have tried so many different ways now and really donīt know what to do anymore..

    I have an assigment for school, the deadline is very very close, that I need some help with. Here is a copy of it:


    Write a class called deckOfCards. The class shall be able to create a deck of cards (52 different cards), shuffle the deck and pull the top card from the deck.

    Then write a program that uses the class deckOfCards, for example create an object of the class, shuffle the cards and print out the top card with its suit (spades, hearts, diamonds, clubs) and value (1-13).



    Can anyone please provide me with a code for it. I know I will not learn anything by not doing it by myself , it is just that I have run out of ideas...and have stared at the screen now for what seams an eternity.


    So if there is anyone more clever than me out there, please help..

  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Please please help me

    I'd rather not give you completely working code, but rather here's some pointers and pseudo-code that should help:

    For a computer, the most efficient method of shuffling cards is to draw a random card and put that on the top. Then, repeat until the deck you're drawing from is empty. When you remove a card, depending on what kind of data structure you have you can mark it as null, or completely get rid of it from the drawing deck.

    Here's some pseudo-code:
    for every card in deckToShuffle:
         pick one at random
         put it in shuffledDeck
         remove it from deckToShuffle
    end

  3. #3
    Member
    Join Date
    Aug 2009
    Posts
    53
    Thanks
    2
    Thanked 3 Times in 2 Posts

    Default Re: Please please help me

    Here is how I would attack a problem like this.

    First consider the structure of the program. I love uml, so I nearly always make uml models before starting on implementation. (Google uml if you dont know it)

    I would suggest a structure like the following.

    A Card class, which contains info about a specific card. This class should contain atleast
    -Instance vars of a the cards value and the color of the card (spade, diamond etc).
    -An overwritten toString() to print these variables.

    A deck class, which contains a composition of cards. Contains info and operations about the deck as a whole.
    To hold the cards I would suggest looking in the Collections framework for somthing suitable. ArrayList could be an option.
    The deck class should contain atleast:
    -A shuffle method (Math.Random could be used)
    -A draw method

    A driver class. This could be contained in the deck class, but my personal preference is to keep it isolated.
    This class should instantiate your other classes and call the sutable methods.

    Hope this helps. If implementation gives you a concrete problem, feel free to post i here, and I will answer it. I will however not write the whole program for you

  4. The Following User Says Thank You to Johannes For This Useful Post:

    JavaPF (January 4th, 2010)

  5. #4
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default Re: Please please help me

    Knuth / Fisher-Yates Shuffle, google it. You will get the best shuffling algorithm there is for a real time solution.

  6. #5
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Please please help me

    that's what i described

  7. #6
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default Re: Please please help me

    I know, but he should know what its called and do a bit more reading on it so he isnt a dimwit like myself (y)

    Chris