We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › Array of objects inside a class...
Page Index Toggle Pages: 1
Array of objects inside a class... (Read 596 times)
Array of objects inside a class...
Jan 30th, 2010, 1:03am
 
well. i'm a total noob here, so bear with me please. Smiley

This is what I'm trying to do:

I want a playerCard class, which will represent a playing card.
I wand a deck class, which will represent a deck, and consist of
a given number of playerCards, plus some other variables, which I don't need arrays of.

so this is the abbreviated playerCard class:
Code:

class playerCard
{
 int player_value, id;
 int current_value, side, position;
 String name, attrib, type;
 boolean inDeck, inHand, discarded;  

 playerCard()
 {
   attrib="none";
   player_value=0;
   position=0;
   type="none";
   name="NONAME";
   current_value=0;
   side=0;
   position=0;
   inHand=false;
   inDeck=false;
   discarded=false;
 }

 void discard()
 {
   inHand=false;
   inDeck=false;
   discarded=true;
 }
}



and this is what I'd like the deck class to be:

Code:
class deck extends playerCard
{
 
 int card_number;
 int position;
 int cards_now;
 boolean shuffled;
 boolean played;
 boolean empty;
 boolean full;

 deck(int num)
 {
   
  card_number=num;
   
  playerCard [] newCards=new playerCard[card_number];
   
   shuffled=false; played=false; empty=false; full=true;
   
   for (int i=1; i<=card_number; i++)
   {
     card[i].discard();
   }
 }
}


This, of course doesn't work. I get null pointer errors everywhere.
I'm really new to OOP. I just took up programming again after many years, so I don't really know if a thing like this can be done in such a way.

The result I'm trying to get is that when you call the deck class, it takes the NUM parameter and dimensions the array of playerCards inside it accordingly.

Any help would be greatly appreciated. Thx.  Smiley
Re: Array of objects inside a class...
Reply #1 - Jan 30th, 2010, 1:43am
 
Having consistency in names can help, there. You use card[i] but it is created nowhere, perhaps you meant to use newCards (or to name newCards as cards). Ah, actually, newCards is a local variable, lost when exiting the constructor, so you need to assign it to a field before. Or just make a field named cards and use it in place of newCards.

General advice: although that, for consistency across the various languages I use, I violate the "rules" set by Sun myself, I should mention that in general, in the Java world (and C++/C# world and some others), class names start with an initial capital, while variable names start with a lowercase char. Just a convention.

Additional notes:
- fields of classes have a default value (unlike variables local to methods), so no need to init them to this default value (0, null, false, etc.).
- You can initialize non-default values in the field declaration: String type = "non"; for example. Which forces (well, not, but it is cleaner this way) to have one variable per declaration.
- Constructors are then more used to set parameters to fields, to initialize non-trivial values (your loop), and so on.
- No need to call discard on each playerCard since they already start with this state.
- deck shouldn't extend playerCards, a desk is a set of cards, ie. it holds cards in an array. A deck doesn't have a current_value, a side, inDeck boolean, and so on.
As many articles and books write: "prefer composition over inheritance" (except, sometime, in libraries and frameworks). Ie. prefer to have objects inside classes instead of inheriting from other classes. That's the difference between "is-a" (inheritance) and "has-a" (composition).
Re: Array of objects inside a class...
Reply #2 - Jan 30th, 2010, 1:47am
 
Depending on what you want to do with your array of cards later you might be better off with an ArrayList rather than a standard array.  There's a little extra complexity getting values out of one, but they're much easier to manipulate.  Actually I'll save myself some work and give you a link to this example posted by PhiLho the other day, which I think would apply in this context.
Re: Array of objects inside a class...
Reply #3 - Jan 30th, 2010, 2:13am
 
Thanks a lot. the discrepancy between naming was because I intended to change the variable names for you to easier understand what it's about. guess I forgot to do that somewhere along the way. Smiley

I managed to avoid the null pointers, though. I was accidentally declaring the deck with 1 item too few.

I'll keep in touch here soon, I expect some more issues soon.  Tongue

Page Index Toggle Pages: 1