0

I keep looking through the code for this, but I can't find anything that would seem off. I'm new to programming so maybe I'm missing something obvious, but any help would be appreciated. The assignment I'm working on is to make a 52 card deck and then design methods to change the deck, the cards are being created but are not being added to the ArrayList. My code for the deck class is:

import java.util.*;
public class FiftyTwoCardDeck
{
List<Card> totalDeck = new ArrayList<>();

private int deckSize = totalDeck.size();

public FiftyTwoCardDeck()
{
    for(int j=0;j<4;j++) {
        for(int k=0;k<13;k++) {
            String s;
            String c;

            switch(j) {
                case 0:
                s = "Spades";
                break;

                case 1:
                s = "Diamonds";
                break;

                case 2:
                s = "Clubs";
                break;

                case 3:
                s = "Hearts";
                break;
            }

            switch(k) {
                case 0:
                c = "Ace";
                break;

                case 10:
                c = "Jack";
                break;

                case 11:
                c = "Queen";
                break;

                case 12:
                c = "King";
                break;

                default:
                c = ""+(k+1);
                break;
            }

            Card card = new Card(s, c);
            totalDeck.add(card);
        }
    }
}

    public int cardAmount()
    {
        return deckSize;
    }
}

The tester class just creates an object:

public class FiftyTwoCardDeckTester
{
    public static void main(String args[])
    {
        FiftyTwoCardDeck deck = new FiftyTwoCardDeck();
    }
}

And the card class that makes objects for the deck is:

import java.util.*;
public class Card
{
    public Card(String st, String ct)
    {
        suit = st;
        count = ct;
    }

    public String getSuit()
    {
        return suit;
    }

    public String getCount()
    {
        return count;
    }

    public String suit;
    public String count;
}
J. Doe
  • 3
  • 2
  • 1
    Your deckSize is always zero. You should return `totalDeck.size()` in your `cardAmount` method – mertsimsek Mar 10 '17 at 14:14
  • 1
    To help you learn the language, you should learn how to debug the language through break points and a normal debugger. Check out how this article: http://www.vogella.com/tutorials/EclipseDebugging/article.html – DogEatDog Mar 10 '17 at 14:18
  • Thanks for the help everyone, very grateful. I'll check out the debugging link DogEatDog – J. Doe Mar 14 '17 at 21:03

3 Answers3

2
private int deckSize = totalDeck.size();

public int cardAmount()
{
    return deckSize;
}

You're initializing deckSize when a FiftyTwoCardDeck object is created. It's is set to 0 before the constructor is run and never updated. You need to update it when cards are added to the deck.

Or, delete the variable and have cardAmount() ask the list how big it is every time it's called.

public int cardAmount()
{
    return totalDeck.size();
}
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
0

This is the problem.

private int deckSize = totalDeck.size();

This field is initialized before the constructor body is executed. See this answer for explanation: https://stackoverflow.com/a/14806340/342852

Community
  • 1
  • 1
Sean Patrick Floyd
  • 292,901
  • 67
  • 465
  • 588
0

Actually your List is being inflated with Cards however cardAmount() will always return 0 because deckSize is intiated to 0 at the creation of FiftyTwoCardDeck.

Use this instead:

public int cardAmount()
{
    return totalDeck.size()
}
Mouad EL Fakir
  • 3,609
  • 2
  • 23
  • 37