New to Java, Help needed with methods.
Hey, Ive made a Playing card class, it contains a enum method to create cards and another to create suits. Im having some issues creating some methods. I want to create a format() method to print out like this - 2 of Clubs, King of Diamonds, 7 of Hearts, Ace of Spades, I have tried to get this working with the actual symbols but no joy so far . A toString() method that gives the output like this - PlayingCard[value=King, suit=Hearts]. And a equal method so i can test if the card is equal to another.
Here is my code so far -
import java.util.*;
public class Cards{
public enum Rank { DEUCE(2), THREE(3), FOUR(4), FIVE(5), SIX(6),
SEVEN(7), EIGHT(8), NINE(9), TEN(10), JACK(11), QUEEN(12), KING(13), ACE(14);
private int CardValue;
Rank(int points)
{
this.CardValue = points;
}
public int getRankpoints()
{
return this.CardValue;
}
}
public enum Suit { CLUBS(2), DIAMONDS(3), HEARTS(4), SPADES(1);
private int Suitpoints;
Suit(int points)
{
this.Suitpoints = points;
}
public int getSuitpoints()
{
return this.Suitpoints;
}
}
private final Rank rank;
private final Suit suit;
private Cards(Rank rank, Suit suit)
{
this.rank = rank;
this.suit = suit;
}
public Rank rank()
{
return this.rank;
}
public Suit suit()
{
return this.suit;
}
public String toString() { return rank + " of " + suit; }
I think im on the right track but need some help with these methods.
Re: New to Java, Help needed with methods.
Please use [code] tags [/code] when posting code.
Unformatted code is hard to read.
Your toString() does not do what you say it should in your description:
"A toString() method that gives the output like this - PlayingCard[value=King, suit=Hearts]"
It seems to be (sort of) doing what you say the format() method should do.
Re: New to Java, Help needed with methods.
ye, thats why i need help really, pulling my hair out with this. hahaha.
Thanks.
Re: New to Java, Help needed with methods.
Ctrl-C Ctrl-V isn't a good way to learn programming. And code posted on a public forum should be credited to the source.
Java class card enum example. REVISED - Stack Overflow (or any of about 40 other sources of this code).
db
Re: New to Java, Help needed with methods.
So pick a method and work on that.
I would suggest renaming toString() to format() since that's the one you're closest to finishing.
And then explain where you are stuck with that method.
Re: New to Java, Help needed with methods.
Quote:
Originally Posted by
DarrylBurke
Well, where else do you lean programming from if you only do it as a hobby? from examples.