Ask your own question, for FREE!
Computer Science 13 Online
OpenStudy (anonymous):

can somebody help me write deck of cards in java?

OpenStudy (e.mccormick):

Well, a deck of cards is four suits of thirteen cards each. So when constructed, it should build those if needed. It might be best to use two classes, one for the deck and one for the cards. That way if you have cards in player's hands, they can have an empty deck that is their hand and cards can be moved from the main deck to it. Then the discard pile, if needed, could be another deck.

OpenStudy (anonymous):

is it required to do it object oriented ?

OpenStudy (anonymous):

you could create a class "Deck" and a class "Card" lik e.mccormick sais. Then you could use a loop to initiate all the cards. You can approach this multiple ways. 1 way is to list all the possible card types in an array an loop through that array. something like this (syntax ignored, it's been a while for java) class Deck{ array cards; public Deck(){ cards = new array(size); } public void Add(Card card){ array.push(card); } } class Card{ string _name; string _type; public Card(string name, string type){ this._name = name; this._type = type; } } array cards = new array(2, 3,4,5,6,7,8,9,10, Jack, Queen, King, Ace); array types = new array(clubs, hearts, spades, diamonds); then in you main class static void main([] args){ Deck deck = new Deck(); // initiate every card for every type foreach(string type in types){ foreach(string card in cards){ deck.Add(new card(card, type); } } } this is sample code since it's been a while since i wrote java but it should clarify the approach.

OpenStudy (anonymous):

p.s. The size of the Deck array should be 52 since there are 52 cards in a deck..

OpenStudy (e.mccormick):

What about jokers?

OpenStudy (anonymous):

you can add them after you initialized all the standard cards by using deck.Add(new Card('Joker', 1)); deck.Add(new Card('Joker', 2));

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!