Solved

How do I make a deck of cards using ArrayLists?

Posted on 2011-09-08
6
280 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
[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
  • Learn & ask questions
  • 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
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 

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 500 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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

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…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
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…
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:

734 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