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
  • Learn & ask questions
Solved

How do I make a deck of cards using ArrayLists?

Posted on 2011-09-07
5
606 Views
Last Modified: 2012-05-12
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","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;
}
}

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;

     }  

0
Comment
Question by:ryanbecker24
  • 5
5 Comments
 
LVL 47

Accepted Solution

by:
for_yan earned 500 total points
ID: 36499864


package javacards;

import java.util.Random;
import java.util.ArrayList;

public class Deck {
	private ArrayList<Card> cards;

	 Deck()
	{
		cards = new ArrayList<Card>();
		for (int a=0; a<=3; a++)
		{
			for (int b=0; b<=12; b++)
			 {
			   cards.add( new Card(a,b) );
			 }
		}
	}

	public Card drawFromDeck()
	{
		Random generator = new Random();
		int index= generator.nextInt( cards.size() );
		return cards.remove(index);
	}

	 public int getTotalCards()
	{
		return cards.size();
	}
}

Open in new window

0
 
LVL 47

Expert Comment

by:for_yan
ID: 36499869
The above is example form that old link you already know:
http://www.dreamincode.net/forums/topic/110380-deck-of-cards-using-various-methods/
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36499883

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
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36499887
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
0
 
LVL 47

Assisted Solution

by:for_yan
for_yan earned 500 total points
ID: 36499895
just add such constructor:

 public Card(int suitInd, int rankInd)
    {
        this.rank=rank[rankInd];
        this.suit=suit[suitInd];
    }

Open in new window

0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Java exception bubble up 2 21
Java syntax, or is it Selenium 6 30
Java class and jar 3 22
passing enum to a method 4 20
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question