Results 1 to 6 of 6
  1. #1
    Join Date
    Dec 2012
    Posts
    3
    Rep Power
    0

    Default 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.

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default 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.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    Join Date
    Dec 2012
    Posts
    3
    Rep Power
    0

    Default Re: New to Java, Help needed with methods.

    ye, thats why i need help really, pulling my hair out with this. hahaha.
    Thanks.

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,242
    Rep Power
    19

    Default 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
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default 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.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  6. #6
    Join Date
    Dec 2012
    Posts
    3
    Rep Power
    0

    Default Re: New to Java, Help needed with methods.

    Quote Originally Posted by DarrylBurke View Post
    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
    Well, where else do you lean programming from if you only do it as a hobby? from examples.

Similar Threads

  1. methods needed for drawing a piechart
    By fultonwilcox in forum New To Java
    Replies: 1
    Last Post: 09-14-2012, 07:51 PM
  2. Java Noob, trying to call methods from another methods
    By gabrielpr12 in forum New To Java
    Replies: 8
    Last Post: 11-17-2011, 09:07 PM
  3. Help needed with methods.
    By pizzadude223 in forum Java Applets
    Replies: 37
    Last Post: 07-27-2010, 08:44 PM
  4. Sr. Web Methods Developer Needed
    By pjhong in forum Jobs Offered
    Replies: 1
    Last Post: 08-11-2009, 03:08 PM
  5. help needed with methods in subclasses
    By uncopywritable in forum New To Java
    Replies: 4
    Last Post: 08-01-2007, 01:47 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •