Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How do I make a deck of cards using ArrayLists?

Posted on 2011-09-08
6
Medium Priority
?
285 Views
Last Modified: 2012-06-27
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++)
           {
              deck.add(new Card(ranks[j], suites));//
           }
       }    
    }
   
   
    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++)
        {
            hand.add(this.getCard());
        }
        return hand;
    }
   
}

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

Expert Comment

by:for_yan
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++)
           {
              deck.add(new Card(ranks[j], suites));// 
           }
       }     
    }
    

Open in new window

0
 
LVL 47

Expert Comment

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

Open in new window

0
 
LVL 47

Expert Comment

by:for_yan
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++)
           {
              deck.add(new Card(ranks[i], suites[j]));// 
           }
       }     
    }
    

Open in new window

0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

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

Expert Comment

by:for_yan
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

by:
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++)
           {
              deck.add(new Card(ranks[i], suites[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++)
        {
            hand.add(this.getCard());
        }
        return hand;
    }

}

Open in new window

0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses

824 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