Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
Solved

# How do I make a deck of cards using ArrayLists?

Posted on 2011-09-08
Medium Priority
282 Views
For the deckOfCards class I am not allowed to use numbers, I have to use the getPossibleRanks() and getPossibleSuites() methods.

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","Jack","Queen","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;
}
}

DeckOfCards class:

import java.util.Random;
import java.util.ArrayList;
public class DeckOfCards
{
// instance variables - replace the example below with your own
private ArrayList<Card> deck;
Random getRandom = new Random();
/**
* Constructor for objects of class DeckOfCards
*/
public DeckOfCards()
{
deck = new ArrayList<Card>();
resetDeckOfCards();
}

public void resetDeckOfCards()
{
String[] ranks = Card.getPossibleRanks();
char[] suites = Card.getPossibleSuits();

for (int i = 0; i < getPossibleRanks().length ; i++)
{
for (int j = 0; j < getPossibleSuits().length; j++)
{
}
}
}

public Card getCard()//remove random card of arraylist of cards and return value from method
{
if (deck.isEmpty())
resetDeckOfCards();
return deck.remove(getRandom.nextInt(deck.size()));
}

public ArrayList<Card> getHand(int size)
{
ArrayList<Card> hand = new ArrayList<Card>();
for (int i = 0; i < size; i++)
{
}
return hand;
}

}

0
Question by:ryanbecker24
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 5

LVL 47

Expert Comment

ID: 36505455

What you call resetDeckOfCards() this is in fact the method which creates the
deck of cards - waht else is necessary?
Just rename it to createDeck and
create new ArrayList in the beginnighg:

`````` public void createDeckOfCards()
{
deck = new ArrayList<Card>();

String[] ranks = Card.getPossibleRanks();
char[] suites = Card.getPossibleSuits();

for (int i = 0; i < getPossibleRanks().length ; i++)
{
for (int j = 0; j < getPossibleSuits().length; j++)
{
}
}
}

``````
0

LVL 47

Expert Comment

ID: 36505463
Only this should be changed to thios line:
``````deck.add(new Card(ranks[i], suites[j]));//
``````
0

LVL 47

Expert Comment

ID: 36505469
this would be correct:

`````` public void createDeckOfCards()
{
deck = new ArrayList<Card>();

String[] ranks = Card.getPossibleRanks();
char[] suites = Card.getPossibleSuits();

for (int i = 0; i < getPossibleRanks().length ; i++)
{
for (int j = 0; j < getPossibleSuits().length; j++)
{
}
}
}

``````
0

Author Comment

ID: 36505554
It still doesn't compile. I think its my compiler though, I am using BlueJ.
0

LVL 47

Expert Comment

ID: 36505572
Well, let me try to compile.
In generla if you are learining Java, I'd suggest not to use BlueJ, but to use
something more common, say Eclipse
0

LVL 47

Accepted Solution

for_yan earned 2000 total points
ID: 36505650
This compiles
(there were some  mistakes in the lopps where you defined length
from methods without prefix of Card)

The second class is OK

``````import java.util.Random;
import java.util.ArrayList;
public class DeckOfCards
{
// instance variables - replace the example below with your own
private ArrayList<Card> deck;
Random getRandom = new Random();
/**
* Constructor for objects of class DeckOfCards
*/
public DeckOfCards()
{
deck = new ArrayList<Card>();
resetDeckOfCards();
}

public void resetDeckOfCards()
{
String[] ranks = Card.getPossibleRanks();
char[] suites = Card.getPossibleSuits();

for (int i = 0; i < ranks.length ; i++)
{
for (int j = 0; j < suites.length; j++)
{
}
}
}

public Card getCard()//remove random card of arraylist of cards and return value from method
{
if (deck.isEmpty())
resetDeckOfCards();
return deck.remove(getRandom.nextInt(deck.size()));
}

public ArrayList<Card> getHand(int size)
{
ArrayList<Card> hand = new ArrayList<Card>();
for (int i = 0; i < size; i++)
{
}
return hand;
}

}
``````
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectioâ€¦
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different dâ€¦
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importingâ€¦