ryanbecker24
asked on
How do I make a deck of cards using ArrayLists?
I am new to Java and this is my first time creating a Card class and DeckOfCards class. I need a constructor that can populate 52 different cards 2 S, 3 S,..Ace S, 2 C, 3 C,...Ace C,...Ace D,...Ace H. I think I also need to create a resetDeckOfCards method to use. I know I need to use a loop but I can't think of a way to do it.
Here is my Card class:
public class Card
{
private String rank;//card rank: a number between 2 and 10, or Jack, Queen, King or Ace
private char suit;//card suit: S,C,H,or D
private static String[] Ranks={"2","3","4","5","6" ,"7","8"," 9","10","J ack","Quee n","King", "Ace"};
private static char[] Suites={'C','H','D','S'};
/**
* Constructor for objects of class Card
*/
public Card()
{
rank="2";
suit='H';
}
public Card(String rank, char suit)
{
this.rank=rank;
this.suit=suit;
}
public String getRank()
{
return rank;
}
public char getSuit()
{
return suit;
}
public void setRank(String rank)
{
this.rank=rank;
}
public void setSuit(char suit)
{
this.suit=suit;
}
//Returns an array of possible card ranks
public static String[] getPossibleRanks()
{
return Ranks;
}
//Returns an array of possible card suits
public static char[] getPossibleSuits()
{
return Suites;
}
public String toString()
{String suitName = "";
switch (suit){
case 'H':
suitName = rank + " of Hearts";
break;
case 'S':
suitName = rank + " of Spades";
break;
case 'D':
suitName = rank + " of Diamonds";
break;
case 'C':
suitName = rank + " of Clubs";
}
return suitName;
}
}
Here is my DeckOfCards class so far:
import java.util.Random;
import java.util.ArrayList;
public class DeckOfCards
{
// instance variables - replace the example below with your own
private ArrayList<Card> deck = new ArrayList<Card>();
Random getRandom = new Random();
/**
* Constructor for objects of class DeckOfCards
*/
public DeckOfCards()
{
String[] ranks = Card.getPossibleRanks();
char[] suites = Card.getPossibleSuits();
Card newCard;
}
Here is my Card class:
public class Card
{
private String rank;//card rank: a number between 2 and 10, or Jack, Queen, King or Ace
private char suit;//card suit: S,C,H,or D
private static String[] Ranks={"2","3","4","5","6"
private static char[] Suites={'C','H','D','S'};
/**
* Constructor for objects of class Card
*/
public Card()
{
rank="2";
suit='H';
}
public Card(String rank, char suit)
{
this.rank=rank;
this.suit=suit;
}
public String getRank()
{
return rank;
}
public char getSuit()
{
return suit;
}
public void setRank(String rank)
{
this.rank=rank;
}
public void setSuit(char suit)
{
this.suit=suit;
}
//Returns an array of possible card ranks
public static String[] getPossibleRanks()
{
return Ranks;
}
//Returns an array of possible card suits
public static char[] getPossibleSuits()
{
return Suites;
}
public String toString()
{String suitName = "";
switch (suit){
case 'H':
suitName = rank + " of Hearts";
break;
case 'S':
suitName = rank + " of Spades";
break;
case 'D':
suitName = rank + " of Diamonds";
break;
case 'C':
suitName = rank + " of Clubs";
}
return suitName;
}
}
Here is my DeckOfCards class so far:
import java.util.Random;
import java.util.ArrayList;
public class DeckOfCards
{
// instance variables - replace the example below with your own
private ArrayList<Card> deck = new ArrayList<Card>();
Random getRandom = new Random();
/**
* Constructor for objects of class DeckOfCards
*/
public DeckOfCards()
{
String[] ranks = Card.getPossibleRanks();
char[] suites = Card.getPossibleSuits();
Card newCard;
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Waht you mean by resetDeckOfCards ?
If yiou mean that you want to have again 52 cards - then you just use
constructor to ctreate the deck of cards
And if you mena to reshuffle - to your would normally not need it,
as long as your drawFromDeck method uses random slection of the card
Or say draw 52 times - and add them to ArrayList and there is your reshuffled deck
but as your drawing is Random - so you don't need to shuffle
In your variant - you can use theDeck generation above - just
add to yor card clas s the constructor which should take two int - and those will be the indices
into your arrays of rank and suite
add to yor card clas s the constructor which should take two int - and those will be the indices
into your arrays of rank and suite
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
http://www.dreamincode.net/forums/topic/110380-deck-of-cards-using-various-methods/