Solved

How do I make a deck of cards using ArrayLists?

Posted on 2011-09-08
6
277 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
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 

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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Suggested Solutions

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…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

803 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