Results 1 to 3 of 3
  1. #1
    carlos123 is offline Member
    Join Date
    Nov 2007
    Rep Power

    Talking Card program , need help thanks.


    I am in need for some help with this assignment. I have started it, but did not get far. Heres my assignment. Any help is appreciated.

    Your assignment is to create a CardDrawer class that would simulate the drawing of cards for a card game.

    Although most card games require a standard 52 card deck, some games require a smaller deck. An example would be the card game Euchre. It only uses the cards 9,10,Jack,Queen,King, Ace of all 4 suites.

    Also some games may have multiple decks, meaning it is possible to draw the same card more than once.

    Therefore, you will need 4 constructors for this class.

    - a default constructor that has typical 52 card deck where each card can only be drawn once.
    - a second constructor that has a 52 card deck however it will allow the same card to be redrawn more than once.
    - a third constructor that allows a client class to pass the smallest and largest cards that are possible to be draw as parameters. You can use numbers for face cards. (ie jack = 11, queen = 12, etc)
    For example since Eucre requires the use of the cards 9,10,jack,queen,king, and ace.
    The client class would pass 9 and 14 (notice in this case the ace would have the value of 14)
    You will need to think about how to make the Ace work in the other examples as well.\
    This constructor will only allow a card to be drawn once.
    - a fourth constructor that is the same as #3 but where a card can be drawn more than once.

    You will then create 2 drawCard methods.
    - The first method will accept no parameters and draw a single card.
    - The second drawCard method will accept an integer parameter that will draw the number cards based on the passed parameter. Ie suppose the following method call was made. game.drawCard(5); This would draw 5 cards. These cards would be put into an array.

    In both of these methods you will also have to account for which constructor was used to create the class. For example, if the first constructor was used (the default were a typical 52 card deck is used and a card can only be drawn once) you will need to make sure all 52 cards can be drawn, but you also have to create an array or ArrayList that will be checked each time to be sure a card is not drawn more than once. (Hint: create a variable that determines if a card can be drawn more than once. If it can your method would ignore the check. If it canít be drawn more than once, your method will then run though the array and check to see if that card has already been drawn. You would set this variable in the constructor.

    Because methods can only return a single value, you canít return both the suite and card value. The way you will get around this is to return a Card object. This means a Card class must be created also. Therefore each time a card is drawn, a card object is created. The Card object will contain both the suite and card value. This single card object can then be passed back or entered into the array of cards that will be passed back.

    The Card class will actually be quite simple at this point. The constructor will accept two parameters for the suite and card value. It will also contain two methods that allow a client class to get the suite and card value returned.

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Rep Power


    Java Code:
    import java.util.*;
    public class CardDrawer {
        Card[] cards;
        Random seed = new Random();
        List<Card> cardsDrawn = new ArrayList<Card>();
        boolean drawOnce = false;
        CardDrawer(boolean drawOnce) {
            this.drawOnce = drawOnce;
        CardDrawer() {
            initCards(1, 14);
        CardDrawer(int low, int high, boolean drawOnce) {
            this(low, high);
            this.drawOnce = drawOnce;
        CardDrawer(int low, int high) {
            initCards(low, high);
        public Card drawCard() {
            return getCard();
        public Card[] drawCard(int n) {
            Card[] draws = new Card[n];
            for(int j = 0; j < n; j++) {
                draws[j] = getCard();
            return draws;
        private Card getCard() {
            Card card;
            boolean drawAgain = true;
            do {
                card = cards[seed.nextInt(cards.length)];
                drawAgain = drawOnce ? cardsDrawn.contains(card)
                                     :  false;
            } while(drawAgain);
            return card;
        private void initCards(int low, int high) {
            String[] faces = { "jack", "queen", "king", "ace" };
            String[] suits = { "spades", "hearts", "clubs", "diamonds" };
            int len = high - low + 1;
            cards = new Card[4*len];
            int count = 0;
            for(int j = 0; j < len; j++) {
                for(int k = 0; k < suits.length; k++) {
                    cards[count++] = new Card(low+j, suits[k]);
        /** Test this class. */
        public static void main(String[] args) {
            CardDrawer cd1 = new CardDrawer();
            Card[] cards = cd1.drawCard(5);
            System.out.printf("cd1 cards = %s%n", Arrays.toString(cards));
            CardDrawer cd2 = new CardDrawer(10, 14, true);
            cards = cd2.drawCard(5);
            System.out.printf("cd2 cards = %s%n", Arrays.toString(cards));
            CardDrawer cd3 = new CardDrawer(false);
            cards = cd3.drawCard(20);
            System.out.println("cd3 card duplicates:");
            Card[] uniqueCards = getUniqueCards(cards);
            List<Card> cardList = new ArrayList<Card>(Arrays.asList(cards));
            for(int j = 0; j < uniqueCards.length; j++) {
                int freq = Collections.frequency(cardList, uniqueCards[j]);
                if(freq > 1)
                    System.out.println(uniqueCards[j] + " occurs " +
                                       freq + " times");
            System.out.println("cardList = " + cardList);
        private static Card[] getUniqueCards(Card[] cards) {
            List<Card> list = new ArrayList<Card>();
            for(int j = 0; j < cards.length; j++) {
            return list.toArray(new Card[list.size()]);
    class Card {
        int value;
        String suit;
        Card(int value, String suit) {
            this.value = value;
            this.suit = suit;
        public String toString() {
            String[] vals = { "jack", "queen", "king", "ace" };
            String s;
            if(value > 10)
                s = vals[value-11];
                s = String.valueOf(value);
            return s + " of " + suit;

  3. #3
    CaptainMorgan's Avatar
    CaptainMorgan is offline Moderator
    Join Date
    Dec 2007
    NewEngland, US
    Rep Power


    I guess we all know who we can call on when we need an assignment done for us from scratch.. eh hardwired?

    carlos, typically this forum does not give homework answers. You as a programmer, need to try this for yourself and when you get stuck - that's when you come to these forums. I know you said you didn't get very far, but where's the code you actually wrote? This is all moot now, since a complete answer has been delivered to you, free of charge. carlos, one day you may realize as sweet as this answer was, it does not help you in the long term and when Joe Programmer gets the job because you were asked a question in the interview that you couldn't answer, that's your problem and then you'll know why you should really attempt problems like these. Even if you get that job, you'll be instantly called on by your fellow programmers and potentially fired in your position because you can't hold up your end of bargain that you signed on for the job.

    What I really think.. hardwired, like a lot of programmers enjoys programming. He completed this out of joy. Giving you the answer only secures him and others like him in their position as chief programmer, knowing that wannabes like you will never make the cut. Of if you do, it'll be a long time until you catch up. If this seems harsh, that's because it is. Sometimes the truth can be harsh.

Similar Threads

  1. Executing a program within a program
    By gibsonrocker800 in forum New To Java
    Replies: 5
    Last Post: 05-12-2008, 08:24 AM
  2. Replies: 0
    Last Post: 04-04-2008, 02:40 PM
  3. Getting network card address in Java 6
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 03-02-2008, 07:10 PM
  4. a little help needed for card drawing
    By carlos123 in forum New To Java
    Replies: 3
    Last Post: 01-09-2008, 03:40 AM
  5. Replies: 0
    Last Post: 10-04-2007, 09:33 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts