classes and variables (java n00b)
Ok...what I am trying to do is create a deck of cards which is done in my Deck class and each card in the deck is created in the card class. I would like to create a new method in my card class to get the face value and another method to get the suit of each card. I am supposed to do this in the card class...how can I do this?
Here is my card class:
Code:
package deckofcards;
public class Card
{
String suit;
int faceValue;
/** Constructor statement */
/** Creates a new instance of Card */
public Card(String suit, int x)
{
this.suit = suit;
this.faceValue = x;
}
public void getMyFaceValue(int cardNum)
{
}
public void getMySuit()
{
}
}
Here is my deck class:
Code:
package deckofcards;
public class Deck {
Card[] my_cards = new Card[52];
static String[] suitType = {"Heart", "Club", "Diamond", "Spade"};
public Deck()
{
for (int i = 0; i < 52; i++)
{
int suit = i / 13;
int value = i % 13 + 1;
my_cards[i] = new Card(suitType[suit], value);
}
}
public Card getCard(int index)
{
Card c = my_cards[index];
return c;
}
public void showDeck()
{
/** Displays all 52 cards */
for(int j = 0; j < 52; j++)
{
System.out.println(my_cards[j].faceValue + " of " + my_cards[j].suit);
}
}
}
Also, the showCards() method in the deck class doesn't display properly. Can anyone tell me why? It looks like it outputs the memory address of each card object.
Yes this is a homework assignment in my class...but I dont know java and I need a little help. Thanks!
Re: classes and variables (java n00b)
Personally (and if you're using Java5 in class) I'd do it the way shown in the example of Sun's introduction to the real enums, to be found in the Java5 documentation. Much easier.
That said, I don't see anything functionally wrong with showCards(), except of course the direct access to the members of Card. (Make them private, and the compiler will complain about such bad practice.)
The getters of Card have completely wrong prototypes. They should have the return type that makes sense and no parameters. As for what goes in there - just return the value of the corresponding member variable.
If you don't understand the previous paragraph ... google the words. After all, this is your class, and you're supposed to learn Java.