www.webdeveloper.com
Results 1 to 6 of 6

Thread: Help on my Card class please.

  1. #1
    Join Date
    Jul 2004
    Location
    West Coast, Canada
    Posts
    665

    Help on my Card class please.

    Hey guys, I was wondering if you guys could lookover my Card class.

    Code:
    public class Card
    {
    	private int value;
    	private String suit;
    	
    	public Card (int val, String cardSuit)
    	{
    		value = val;
    		suit = cardSuit;
    	}
    }
    Would this be sufficient enough to implement a card drawing game?

  2. #2
    Join Date
    Nov 2003
    Location
    Jerryville, Tejas
    Posts
    11,715
    I'd think you at least need getters and of course there's the Deck class that's a collection of Cards.

  3. #3
    Join Date
    Jan 2004
    Location
    Melbourne, Australia
    Posts
    5,298
    Agreed, getters and setters will need to be added. You may want to break it into three variables.

    - suit
    - type (1 - 10, Jack, King, etc) (there must be a better word?)
    - value

    Also, override toString() to provide a sentance when the user wants to print the Card.
    Code:
    public String toString()
    {
        return getType() + " of " + getSuit() + "'s";
    }
    Regards.

  4. #4
    Join Date
    Nov 2003
    Location
    Jerryville, Tejas
    Posts
    11,715
    I'm not sure setters are needed. (Or do you Aussies get out your Sharpies and rework your deck of cards every so often?)

  5. #5
    Join Date
    Jan 2004
    Location
    Melbourne, Australia
    Posts
    5,298
    lol. Of coarse.

    I think setters are a good alternative to passing variables in the constructor. It may not be needed for this example, but still conforms to standards.

    Bueza, You can setup a default constructor that takes no parameters.

    Regards.

  6. #6
    Join Date
    Nov 2003
    Location
    Jerryville, Tejas
    Posts
    11,715
    In fact, if you want use those Cards as JSP page scope beans then you'll HAVE to set up setters and an empty constructor.

    I don't mind breaking some standards if the behaviour of the object calls for it. Sometimes we code a default constructor and make it private to eliminate the possibility that it be "automagically" built by a JSP that should be receiving it ready to go.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles