Nenad
~Choco Coronet~ Omnomnom
- Reaction score
- 137
Create a class Card
String field suit in public class Card in package x
A card has a suit. The suit can either be Clubs, Hearts, Spades or Diamonds. We let each be denoted by a capital letter; "C", "H", "S" or "D", respectively.
int field face in public class Card in package x
The face is the value of the card. For our purposes, let Ace have the value 1, and then let the rest of the cards have their conventional values.
public String method toString in public class Card in package x
The toString method returns the suit and face values as one String with no space in between.
Example: The Ace of Spades should return "S1" as the value of the toString method.
Create the class CardDeck. The card deck will obviously hold some cards later on.
ArrayList<Card> field cards in public class CardDeck in package x
A CardDeck is effectively a list of Cards. Create such an list field.
public void method init in public class CardDeck in package x
The init() method fills the deck with the 52 cards that are in a standard deck. Fill it in the order S,H,D,C and in increasing face value, i.e. the first card is the Ace of Spades ("S1"), the second 2 of Spades ("S2"), and the last is King of Clubs ("C13").
public Card method getCard in public class CardDeck in x
Create a method that takes an int argument and returns the card in that position in the Card list.
This is another part of the assignment that is still not going well, several questions here if anyone can help me:
1. What the **** is that toString method supposed to do? This is all of the assignment and frankly i don't even know where to start on that one.
2.
This is my code for the CardDeck part:
Code:
import java.util.ArrayList;
public class CardDeck {
private ArrayList<Card> cards;
public void init()
{
String S[] = {"S", "H", "D", "C"};
String Card[] = new String[51];
int z = 0;
for (int i=0; i==S.length;i++)
{
for (int j=1; j<=13; j++)
{
Card[z]= S[i]+j;
z=z+1;
}
}
}
public Card getcard(int n)
{
return cards.get(n);
}
}
Now, the first method is not yet finished because i had trouble with the add method to add the Cards to the list. How do i do this properly (i always have an error when i try cards.add(Card[z]) ). As it says it needs to be in order, so i figured this way is the easiest to do it but i'm not sure.
3. Is the final method, that is getCard, correct? I'm still learning the get set and stuff like that so i'm not quite sure on that one.
Thanks for all the help again ^^