CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Sep 2012
    Posts
    4

    Problem with implementing a new class called Card

    I am new at java programming so I need to make one class called Card. This class contains named constnts for the four Suits(CLUBS, DIAMONDS, HEARTS, SPADES) and four Ranks(ACE, JACK, QUEEN, KING) with respectively values 1, 11, 12, 13. This is how i implemented this part:

    public class Card {

    public static final int ACE=1;
    public static final int JACK=11;
    public static final int QUEEN=12;
    public static final int KING=13;

    public static final String SUIT1="CLUBS";
    public static final String SUIT2="DIAMONDS";
    public static final String SUIT3="HEARTS";
    public static final String SUIT4="Spades";

    //I think that i'm okey with this part.


    //Second thing what i need to do is a Constructor that takes a Rank and a Suit and returns a Card with those values. Here is what I did:

    public Card(int Rank, String Suit){
    Rank=ACE;
    Rank=JACK;
    Rank=QUEEN;
    Rank=KING;

    Suit=SUIT1;
    Suit=SUIT2;
    Suit=SUIT3;
    Suit=SUIT4;
    }

    //And the third thing that I can't make it or I am making it wrong is how to make getter methods for
    getSuit and getRank that retrieve the rank and suit components of a Card????

    Anybody can give me a comment about what i have done or how to make this get methods ???

  2. #2
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Problem with implementing a new class called Card

    Please use code tags when posting code.
    Also adhere to the Java naming conventions eg variable names should start with lower case letters.

    Why are your suit constant names SUIT1, SUIT2 etc why not use SUIT_CLUBS, SUIT_DIAMONDS etc or just CLUBS, DIAMONDS etc both of which tell you what the constant actually represents?

    Why is the value of SUIT4 title case whereas all the other suit constants have uppercase values?

    //Second thing what i need to do is a Constructor that takes a Rank and a Suit and returns a Card with those values. Here is what I did:
    You are passing in the rank and suit of the card to the constructor so you need to store these values in instance variable so the constructed Card object 'remembers' what card it is.

    //And the third thing that I can't make it or I am making it wrong is how to make getter methods for
    getSuit and getRank that retrieve the rank and suit components of a Card????
    Once you do what I said above this may become more obvious.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  3. #3
    Join Date
    Sep 2012
    Posts
    4

    Re: Problem with implementing a new class called Card

    Quote Originally Posted by keang View Post

    You are passing in the rank and suit of the card to the constructor so you need to store these values in instance variable so the constructed Card object 'remembers' what card it is.

    Once you do what I said above this may become more obvious.
    Ok what you are saying is this i think:
    I declared two instance variables Rank and Suit

    Code:
    private int Rank;
    	private String Suit;
    the constructor look like this:
    Code:
    	public Card(){
    		Rank=ACE;
    		Rank=JACK;
    	    Rank=QUEEN;
    	    Rank=KING;
    	    
    	    Suit=SUIT_CLUBS;
    	    Suit=SUIT_DIAMONDS;
    	    Suit=SUIT_HEARTS;
    	    Suit=SUIT_SPADES;
    	}
    and the get methods:

    Code:
    public int getRank(){
    		return Rank;
    	}
    	
    	public String getSuit(){
    		return Suit;
    This is working okey but every time i run the program always it shows me the same cart. How can i change this that when i restart the program to show me different cart ?????

  4. #4
    Join Date
    Sep 2012
    Posts
    4

    Re: Problem with implementing a new class called Card

    Quote Originally Posted by keang View Post

    You are passing in the rank and suit of the card to the constructor so you need to store these values in instance variable so the constructed Card object 'remembers' what card it is.

    Once you do what I said above this may become more obvious.
    Ok what you are saying is this i think:
    I declared two instance variables Rank and Suit

    Code:
    private int Rank;
    	private String Suit;
    the constructor look like this:
    Code:
    	public Card(){
    		Rank=ACE;
    		Rank=JACK;
    	    Rank=QUEEN;
    	    Rank=KING;
    	    
    	    Suit=SUIT_CLUBS;
    	    Suit=SUIT_DIAMONDS;
    	    Suit=SUIT_HEARTS;
    	    Suit=SUIT_SPADES;
    	}
    and the get methods:

    Code:
    public int getRank(){
    		return Rank;
    	}
    	
    	public String getSuit(){
    		return Suit;
    This is working okey but every time i run the program always it shows me the same cart. How can i change this that when i restart the program to show me different cart ?????

  5. #5
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Problem with implementing a new class called Card

    the constructor look like this:
    No that's not what I said. I said "You are passing in the rank and suit of the card to the constructor so you need to store these values in instance variable so the constructed Card object 'remembers' what card it is."

    You have declared instance rank and suit variables (use lower case names as I said previously) and are setting them to 4 values (why?) only the last of which will be remembered and what has happened to the rank and suit values you were passing in ie the ones that you need to define what card this object is supposed to represent?
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  6. #6
    Join Date
    Sep 2012
    Posts
    4

    Re: Problem with implementing a new class called Card

    Quote Originally Posted by keang View Post
    You have declared instance rank and suit variables (use lower case names as I said previously) and are setting them to 4 values (why?) only the last of which will be remembered and what has happened to the rank and suit values you were passing in ie the ones that you need to define what card this object is supposed to represent?
    I'm setting this rank and suit instance variables to four values because that's the only way I can connect rank with what rank contain (ace, jack ,queen, king) the same with the suit variable. Is there any other way how can i represent what should rank contain and what suit ??? I'm getting what you want to say but i can't implement it...

  7. #7
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Problem with implementing a new class called Card

    Sorry, don't understand your logic there.

    Each card object represents a single card therefore you need to pass the rank and suit of the card you want to represent into the constructor. The constructor has to save those details in instance variables so they can be used at a later time.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  8. #8
    Join Date
    Sep 2012
    Posts
    4

    Re: Problem with implementing a new class called Card

    so how should the constructor look????

    public Card(int rank, String suit){

    ???????

    }

  9. #9
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Problem with implementing a new class called Card

    Code:
    private int rank; 
    private String suit;
    
    public Card(int cardRank, String cardSuit){
        rank = cardRank;
        suit = cardSuit;
        }
    BTW rather than using constants to define the possible rank and suit values you would be better off using enums. If you haven't come across enums yet then carry on as you are but once you have it all working have a look at enums.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured