Reputation: 69
Granted, this is probably something rather simple I have looked over by mistake, but I have to be sure.
I am working on getting a classic PlayingCard class running with a PlayingCard Tester to test it as an assignment. Now, when the whole thing is done, I will have a Pack class as well for producing a pack of cards and shuffling it, along with an equals method in the PlayingCard class, but for now, I just want to get basic PlayingCard class to work with the tester.
This is the current set up for the PlayingCard Class, ignore the int statement for i, as that was for a random numbers generator with the aid of a for loop, but that has been or rather, should be rendered redundent with the PlayingCard constructor.
public class PlayingCard
{
public PlayingCard(int rank, int suit)
{
this.rank = rank;
this.suit = suit;
}
// Values for rank
public final int TWO = 2;
public final int THREE = 3;
public final int FOUR = 4;
public final int FIVE = 5;
public final int SIX = 6;
public final int SEVEN = 7;
public final int EIGHT = 8;
public final int NINE = 9;
public final int TEN = 10;
public final int JACK = 11;
public final int QUEEN = 12;
public final int KING = 13;
public final int ACE = 14;
// Values for suits
public final int CLUBS = 0;
public final int DIAMONDS = 1;
public final int HEARTS = -1;
public final int SPADES = -2;
// Set random Rank and Suit values
public int rank;
public int suit;
public int i;
public int getRank()
{
return rank;
}
public int getSuit()
{
return suit;
}
@Override
public String toString()
{
return getClass().getName() + "[rank " + rank + "suit " + suit + "]";
}
public void format()
{
System.out.format(rank + " of " + suit);
}
}
And this is the current tester, note I have used a set of values based of the values for rank and suit in the class.
public class PlayingCardTester
{
public static void main(String[] args)
{
PlayingCard test;
test = new PlayingCard(14, 1);
test.getRank();
test.getSuit();
test.toString();
test.format();
System.out.println("");
}
}
The values put into the constructor should be being used in the methods of the class. The output I want from the actual tester and class is something like this:
Ace of Diamonds
Instead, I get this:
0 of 0
What I am asking is, how can I use the values given in the constructor in the methods implemented in the tester and class so I can get the output I want? The values used a meant to be related to those specified in the class, but something tells me they are effectivly useless or not being used in the actual class. It's probably something rather simple I have missed but unable to find, or myself being incredably stupid about it, but I want to be sure in case something more complex is required.
Advice?
EDIT: Added the details to the constructor, now I have the output:
14 of 1
Upvotes: 0
Views: 10167
Reputation: 23226
You actually have two separate problems. The first issue is, as noted in another answer, that you do not assign the variables passed to the constructor of your class to the corresponding instance variables.
The second issue you have is that you are setting int values to represent the Rank and Suit but do not provide corresponding String representation in your PlayingCard class.
You could do this for ranks by creating an array like:
String ranks [] = {"TWO","THREE"...};
and using ranks[rank -2]
in format();
As suit integer representations have non sequential values you would need to use another data structure (such as a Map).
An easier way of doing all of this however is to creating two enums as below. You would then create a new card like:
PlayingCard card = new PlayingCard(Rank.ACE, Suit.SPADES);
Enum for Suit:
public enum Suit {
HEARTS(0), SPADES(-2), CLUBS(-1), DIAMONDS(1);
private int value;
private Suit(int value){
this.value = value;
}
public int getValue(){
return value;
}
}
Enum For Rank:
public enum RANK {
TWO(2), THREE(3), ACE(14);
private int value;
private Suit(int value){
this.value = value;
}
public int getValue(){
return value;
}
}
Playing Card:
public class PlayingCard
{
private Rank rank;
private Suit suit;
public PlayingCard(Rank rank, Suit suit)
{
this.rank = rank;
this.suit = suit;
}
public Rank getRank()
{
return rank;
}
public Suit getSuit()
{
return suit;
}
@Override
public String toString()
{
return getClass().getName() + "[rank " + rank + "suit " + suit + "]";
}
public void format()
{
System.out.format(rank + " of " + suit);
}
}
Upvotes: 2
Reputation: 1754
You need to set parameters from constructor to your instance.
public PlayingCard(int rank, int suit)
{
this.rank = rank;
this.suit = suit;
}
cause this constructor takes two parameters but dont do anything with them.
public PlayingCard(int rank, int suit)
{
}
and because int is naturally set to 0 thats why you get two 0.
Upvotes: 2