Results 1 to 1 of 1
Thread: Java monster is back!
-
- Join Date
- 11-26-06
- Location
- Anywhere you want to be.
- Posts
- 3,946
- Post Thanks / Like
- Blog Entries
- 1
01-31-10, 08:59 PM #1Java monster is back!
*So, I have not asked any java questions in a while so here I go....*
Create a Driver program called DeckOfCardTest that contains a 2-dimensional array of 4 decks of cards. Randomly initialize each of the four decks and print them from your driver program.
I already have the class for a Card and DeckOfCards. I just don't fully understand how to make the 2-dimensional array for the 4 decks of cards.
this is what I do when I try...
Code:DeckOfCards[][] decks = new DeckOfCards[4][52];
Code:for(int i = 0; i < decks.length; i++) decks[i].shuffle();
Here are the other classes for reference
this is class CARD
Code:public class Card { private String face; // face of card ("Ace", "Deuce", ...) private String suit; // suit of card ("Hearts", "Diamonds", ...) // two-argument constructor initializes card's face and suit public Card( String cardFace, String cardSuit ) { face = cardFace; // initialize face of card suit = cardSuit; // initialize suit of card } // end two-argument Card constructor // return String representation of Card public String toString() { return face + " of " + suit; } // end method toString } // end class Card
Code:import java.util.Random; public class DeckOfCards { private Card deck[]; // array of Card objects private int currentCard; // index of next Card to be dealt private final int NUMBER_OF_CARDS = 52; // constant number of Cards private Random randomNumbers; // random number generator // constructor fills deck of Cards public DeckOfCards() { String faces[] = { "Ace", "Deuce", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King" }; String suits[] = { "Hearts", "Diamonds", "Clubs", "Spades" }; deck = new Card[ NUMBER_OF_CARDS ]; // create array of Card objects currentCard = 0; // set currentCard so first Card dealt is deck[ 0 ] randomNumbers = new Random(); // create random number generator // populate deck with Card objects for ( int count = 0; count < deck.length; count++ ) deck[ count ] = new Card( faces[ count % 13 ], suits[ count / 13 ] ); } // end DeckOfCards constructor // shuffle deck of Cards with one-pass algorithm public void shuffle() { // after shuffling, dealing should start at deck[ 0 ] again currentCard = 0; // reinitialize currentCard // for each Card, pick another random Card and swap them for ( int first = 0; first < deck.length; first++ ) { // select a random number between 0 and 51 int second = randomNumbers.nextInt( NUMBER_OF_CARDS ); // swap current Card with randomly selected Card Card temp = deck[ first ]; deck[ first ] = deck[ second ]; deck[ second ] = temp; } // end for } // end method shuffle // deal one Card public Card dealCard() { // determine whether Cards remain to be dealt if ( currentCard < deck.length ) return deck[ currentCard++ ]; // return current Card in array else return null; // return null to indicate that all Cards were dealt } // end method dealCard } // end class DeckOfCards
Thread Information
Users Browsing this Thread
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks