Array of Class Instances

  • (2 Pages)
  • +
  • 1
  • 2

16 Replies - 784 Views - Last Post: 22 April 2011 - 04:26 PM Rate Topic: -----

#1 Ntwiles  Icon User is offline

  • D.I.C Addict

Reputation: 148
  • View blog
  • Posts: 830
  • Joined: 26-May 10

Array of Class Instances

Posted 20 April 2011 - 12:35 PM

I'm trying to create an array of class instances like so:

package carddeck;

import carddeck.PlayingCard;

public class CardDeck 
{
   public CardDeck() 
   {
      PlayingCard[] card;
      for (int i = 0; i < 52; i++) {
         card[i] = new PlayingCard();
      }
      for (int i = 0; i < 4; i++) {
         for (int ii = 0; ii < 13; ii++) {
            card[ii].setSuit(i);
         }
      }
   }
}


I'm getting the error Variable 'card' might not have been initialized. I've tried a few different combinations but they all throw incompatible type errors. Can someone tell me how to do this?

Is This A Good Question/Topic? 0
  • +

Replies To: Array of Class Instances

#2 Ntwiles  Icon User is offline

  • D.I.C Addict

Reputation: 148
  • View blog
  • Posts: 830
  • Joined: 26-May 10

Re: Array of Class Instances

Posted 20 April 2011 - 12:39 PM

I'm trying to create an array of class instances like so:

package carddeck;

import carddeck.PlayingCard;

public class CardDeck 
{
   public CardDeck() 
   {
      PlayingCard[] card;
      for (int i = 0; i < 52; i++) {
         card[i] = new PlayingCard();
      }
      for (int i = 0; i < 4; i++) {
         for (int ii = 0; ii < 13; ii++) {
            card[ii].setSuit(i);
         }
      }
   }
}


I'm getting the error Variable 'card' might not have been initialized. I've tried a few different combinations but they all throw incompatible type errors. Can someone tell me how to do this?

Edit: Sorry for the double thread. As I was posting it I got an Internal Server Error so I refreshed. I guess it had gone through after all.

This post has been edited by Ntwiles: 20 April 2011 - 12:41 PM

Was This Post Helpful? 0
  • +
  • -

#3 Brewer  Icon User is offline

  • Awesome
  • member icon

Reputation: 179
  • View blog
  • Posts: 1,044
  • Joined: 14-June 10

Re: Array of Class Instances

Posted 20 April 2011 - 12:41 PM

When initializing an Array you have to call it as so:

PlayingCard[] card = new PlayingCard[<insert length of array here>];


I imagine that, in this case, we would want to use PlayingCard[52];
Was This Post Helpful? 3
  • +
  • -

#4 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10464
  • View blog
  • Posts: 38,783
  • Joined: 27-December 08

Re: Array of Class Instances

Posted 20 April 2011 - 12:42 PM

Happy coding. :)
PlayingCard[] deck = new PlayingCard[someIntForNumPlayingCards];


Was This Post Helpful? 3
  • +
  • -

#5 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1391
  • View blog
  • Posts: 3,077
  • Joined: 05-April 11

Re: Array of Class Instances

Posted 20 April 2011 - 12:42 PM

I dont think I have to explain my code, I think you will be able to see it :)

package carddeck;

import carddeck.PlayingCard;

public class CardDeck 
{
   public CardDeck() 
   {
      PlayingCard[] card = new PlayingCard[52];
      for (int i = 0; i < card.length; i++) {
         card[i] = new PlayingCard();
      }
      for (int i = 0; i < 4; i++) {
         for (int ii = 0; ii < 13; ii++) {
            card[ii].setSuit(i);
         }
      }
   }
}



Was This Post Helpful? 3
  • +
  • -

#6 Brewer  Icon User is offline

  • Awesome
  • member icon

Reputation: 179
  • View blog
  • Posts: 1,044
  • Joined: 14-June 10

Re: Array of Class Instances

Posted 20 April 2011 - 01:04 PM

Woohoo for 3 people having answers that agree with each other!
Was This Post Helpful? 0
  • +
  • -

#7 Ntwiles  Icon User is offline

  • D.I.C Addict

Reputation: 148
  • View blog
  • Posts: 830
  • Joined: 26-May 10

Re: Array of Class Instances

Posted 20 April 2011 - 01:34 PM

Ohh that's how it works. Thanks for the help guys!
Was This Post Helpful? 0
  • +
  • -

#8 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10464
  • View blog
  • Posts: 38,783
  • Joined: 27-December 08

Re: Array of Class Instances

Posted 20 April 2011 - 01:34 PM

Glad we could help! :)
Was This Post Helpful? 0
  • +
  • -

#9 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5800
  • View blog
  • Posts: 12,636
  • Joined: 16-October 07

Re: Array of Class Instances

Posted 21 April 2011 - 04:54 AM

package carddeck;

// you really shouldn't need this
// you're already in carddeck namespace
import carddeck.PlayingCard;

public class CardDeck {
	// magic numbers bad
	public static final int DECK_SIZE = 52;
	public static final int SUIT_SIZE = 4;
	public static final int FACE_SIZE = 13;
	
	// this will work better here
	private PlayingCard[] card; // more below
	public CardDeck() {
		// this is kind of pointless, because the variable is local
		// PlayingCard[] card = new PlayingCard[52];
		// use an instance variable 
		card = new PlayingCard[DECK_SIZE];
		
		for (int i = 0; i < DECK_SIZE; i++) {
			// you know, now would be a good time to consider
			// "card" as a bad name for a collection of cards
			PlayingCard singleCard = new PlayingCard();
			singleCard.setSuit(i/FACE_SIZE);
			// A constructor for PlayingCard would be nice
			// Does PlayingCard only have suit but no face value?
			card[i] = singleCard;
		}
		// forget this...
		/*
		for (int i = 0; i < 4; i++) {
			for (int ii = 0; ii < 13; ii++) {
				card[ii].setSuit(i);
			}
		}*/
	}
}



Hope this helps.
Was This Post Helpful? 3
  • +
  • -

#10 cfoley  Icon User is online

  • Cabbage
  • member icon

Reputation: 1949
  • View blog
  • Posts: 4,050
  • Joined: 11-December 07

Re: Array of Class Instances

Posted 21 April 2011 - 04:57 AM

	public static final int SUIT_SIZE = 4;
	public static final int FACE_SIZE = 13;
	public static final int DECK_SIZE = SUIT_SIZE * FACE_SIZE;


Just eliminating one more magic number. ;)
Was This Post Helpful? 1
  • +
  • -

#11 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5800
  • View blog
  • Posts: 12,636
  • Joined: 16-October 07

Re: Array of Class Instances

Posted 21 April 2011 - 06:34 AM

Bite me. :)
public static final int JOKERS = 2;
public static final int DECK_SIZE = 54;



But you're right, that's better. Some languages don't let you do math on constants...

I'd actually prefer SUIT_SIZE and FACE_SIZE belong to Card. Card should be responsible for taking it's value from the constructor. It would need to know those values to derive it's value from a starting position.
Was This Post Helpful? 0
  • +
  • -

#12 Ntwiles  Icon User is offline

  • D.I.C Addict

Reputation: 148
  • View blog
  • Posts: 830
  • Joined: 26-May 10

Re: Array of Class Instances

Posted 22 April 2011 - 03:40 PM

Thanks baavgai, that was a big help! I'm new to Java so I've got quite a few things to learn.

I've been having some trouble with handling instances of playing cards. I've found it easier to handle the deck without the OO route, and just hold all the cards in a series of arrays as ints/chars for face/value/suit. For such a small game it seemed like creating a class for cards would be more work than would be beneficial.

Can any of you guys give me some tips on whether or not this is a good habit to be getting into?

This post has been edited by Ntwiles: 22 April 2011 - 03:43 PM

Was This Post Helpful? 0
  • +
  • -

#13 cfoley  Icon User is online

  • Cabbage
  • member icon

Reputation: 1949
  • View blog
  • Posts: 4,050
  • Joined: 11-December 07

Re: Array of Class Instances

Posted 22 April 2011 - 03:48 PM

Creating a class for card is usually the better option. If you have seperate arrays the chances of removing a number but not a suit increase. A card class can be quite simple. Something like this would do it:

public class Card{
  private int number;
  private int suit;
  
  public Card(int suit, int number) {
    this.number = number;
    this.suit = suit;
  }

  public int getSuit()   {return suit;}
  public int getNumber() {return number;}
}


There are other things that you could do to make this better like adding a toString() method or a method to see if one card beats another cars or constants for the suits, etc... but this is your minimum and it will make your code easier than using parallel arrays.
Was This Post Helpful? 1
  • +
  • -

#14 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10464
  • View blog
  • Posts: 38,783
  • Joined: 27-December 08

Re: Array of Class Instances

Posted 22 April 2011 - 04:05 PM

View Postcfoley, on 22 April 2011 - 06:48 PM, said:

but this is your minimum and it will make your code easier than using parallel arrays.

I see my calling card here, since you bring up not using parallel arrays. :)

@Ntwiles: I have a tutorial on Moving Away From Parallel Arrays that expands more on the class design aspect that cfoley suggested. You might want to check it out.
Was This Post Helpful? 1
  • +
  • -

#15 Ntwiles  Icon User is offline

  • D.I.C Addict

Reputation: 148
  • View blog
  • Posts: 830
  • Joined: 26-May 10

Re: Array of Class Instances

Posted 22 April 2011 - 04:22 PM

Actually the issue I was having was trying to access the cards. For instance, in the main class, I would have to have something like:

System.out.print(deck.card[1].getValue());

Which doesn't seem to work. Do I have to create the instances of each card in the main class, then pass them to the deck somehow?

This post has been edited by Ntwiles: 22 April 2011 - 04:26 PM

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2