-
September 19th, 2012, 12:16 PM
#1
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 ???
-
September 19th, 2012, 12:58 PM
#2
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.
-
September 19th, 2012, 04:11 PM
#3
Re: Problem with implementing a new class called Card
Originally Posted by keang
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 ?????
-
September 19th, 2012, 04:12 PM
#4
Re: Problem with implementing a new class called Card
Originally Posted by keang
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 ?????
-
September 20th, 2012, 03:17 PM
#5
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?
-
September 20th, 2012, 05:07 PM
#6
Re: Problem with implementing a new class called Card
Originally Posted by keang
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...
-
September 20th, 2012, 05:12 PM
#7
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.
-
September 20th, 2012, 05:27 PM
#8
Re: Problem with implementing a new class called Card
so how should the constructor look????
public Card(int rank, String suit){
???????
}
-
September 21st, 2012, 02:51 AM
#9
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 Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|