-1

Very basic question, new to programming, but I don't get how to call the parameterized constructor in Java.

I have the following constructor in my Card class:

public Card(int n) {
    face = Face.values()[n%13];
    suit = Suit.values()[n%4];
}

I'm trying to utilize it in my Deck class:

public class Deck {
    private Card[] deck;
    private int nextCard;

    public Deck(){
        nextCard = 0;
        Card crd = new Card();      
        for(int i = 0; i < 52; i++){
            deck[i] = crd.Card(i);
            nextCard++;                 
        }
    }
}

I'm supposed to assign every member of the deck array a card from the Card constructor. This obviously didn't work, and if someone could tell me the why and what to do instead, that would be really appreciated.

You can call the toString like this --> crd.toString(); or the setter like card.setCard(i);

I'm not following how to use the parameterized constructor that shares the class name. Help?


Edit

I should add my professor's instruction for this portion:

The Deck's default constructor should fill an array of 52 unique Card objects. Use a loop and pass the loop's counter to the Card constructor as its argument.

blacktide
  • 10,654
  • 8
  • 33
  • 53
Tonantzin
  • 29
  • 2

2 Answers2

3
private Card[] deck;

public Deck() {

    for(int i = 0; i < 52; i++){
        deck[i] = new Card(i);      
    }
}

When you create a 'new' instance of Card, the constructor is executed. Your constructor takes an int argument, so all you need to do is to call

Card c = new Card(i);

...where 'i' is a number. In your case, you just put the object straight into the array without holding a reference to it

deck[i] = new Card(i);

I also removed some redundant lines of code too, which might clarify things.

Ramsay Domloge
  • 695
  • 3
  • 11
2

You need to create a new Card object in your for loop, not try and reuse the same one and just re-construct it.

for(int i = 0; i < 52; i++){
    deck[i] = new Card(i);
    nextCard++;                 
}

Please read up on the documentation on Oracles site if you are still confused about Constructors: https://docs.oracle.com/javase/tutorial/java/javaOO/constructors.html https://docs.oracle.com/javase/tutorial/java/javaOO/arguments.html

CConard96
  • 874
  • 1
  • 7
  • 18
  • That's actually what I initially did, but it won't compile. It says there is an error that this line deck[i] = new Card(i); and it says that crd is not being utilized. Oh, I get what you are saying. I think that did the trick, thanks :) – Tonantzin Jun 25 '16 at 14:33
  • If you mean it says the variable is unused, then its a warning, not an error. You code should compile fine as is. If you post the exact error that shows when you compile, I can try to help more. – CConard96 Jun 25 '16 at 14:35
  • Exception in thread "main" java.lang.NullPointerException at cards.Deck.(Deck.java:45) at cards.DeckDriver.main(DeckDriver.java:38) line 38 public static void main (String [ ] args) { Deck aDeck = new Deck(); System.out.println (aDeck.toString()); } line 45: for(int i = 0; i < 52; i++){ deck[i] = new Card(i); – Tonantzin Jun 25 '16 at 14:46
  • You never initialized Deck. Should be Card [] deck = new Card [53] – CConard96 Jun 25 '16 at 14:48
  • Thank you! This has helped greatly. My toString method now prints 52 different cards. – Tonantzin Jun 25 '16 at 15:28