Advice with make Card exercise...
OK so I'm trying to do this make a card exercise from the Oracle tutorial and I'm just trying to work out how it should be written out, this is what I'm thinking :
Basically I need to have a class for the suit, a class for the rank of the card and a main method. So that's three classes.
I then need to create an array for the values of the rank within the rank class, and an array for the suits in the suits class. Each of these arrays need to have methods that can can call them (not sure how to do this at the moment) There also needs to be a return value for anything that isn't in the array, like validation ("Sorry incorrect value" or something along those lines)
The two (array) types would be strings
Is this sounding right?
The example mentions using the assert statement, but I haven't used this before.
Cheers.
Re: Advice with make Card exercise...
Cards have attributes rank and suit which can be defined as constants (maybe enums?), but I don't think they need to be separate classes. Typical classes in a card game are Card, Deck, and Hand.
Re: Advice with make Card exercise...
Quote:
Originally Posted by
GregBrannon
Cards have attributes rank and suit which can be defined as constants (maybe enums?), but I don't think they need to be separate classes. Typical classes in a card game are Card, Deck, and Hand.
Oh OK, I thought It was better to have everything as separate as possible for some reason, probably read something about databases!
Card - Suit, rank
Deck - amount of cards
hand - cards that are given to someone
that's about the size of it (i think?) I'll have a bash at that now...
--- Update ---
We're not using enums yet! Apparently we will soon.....
Re: Advice with make Card exercise...
You're on the right track. Some additional thoughts and details:
Card - defines the basic playing piece and contains those attributes needed with appropriate getter and setter methods. Setters? Does a card object change once defined? Probably not.
Deck - a collection of Card objects, usually containing all cards available for one game. Includes an appropriate collection to contain the card objects and methods needed for a deck, like shuffle(), deal()?, etc.
Hand - the player's own collection of Card objects obtained from the Deck per the rules of the game. Hand methods might be toString() or the graphical equivalent, score() to determine the value of the hand based on the rules of the game (or something similar), etc.
Game - named appropriately (TwentyOne, Poker, etc.) controls the flow of the game, player turns, determining wagers, winners/losers etc., according to the rules of the game.
Bank/Kitty/Pot - an optional class to keep track of the game's finances, if appropriate.
Re: Advice with make Card exercise...
I think (hope?!) some of that would be a bit beyond the scope of this particular exercise.... They haven't asked anything remotely like this so far and to ask a whole functioning programme seem's like a bit of a steep one... here's a link to the answer though I've not looked at that yet.
This is what I've done :/
Code :
public class Cards {
// static variables as there's only one of these
public static String[] suit = { "Clubs", "Hearts", "Spades", "Diamonds" };
//used a string for the rank as there were the royal ones...
public static String[] rank = { "one", "two", "three", "four", "five", "six",
"seven", "eight", "nine", "ten", "jack", "queen", "king" };
public static void main(String[] args) {
Cards myCard = new Cards();
// kind of stumped here :(
}
}
I can 'get' a suit or something by accessing the class variable Cards.suit[2] or something, but I don't really know where to go from there.
I'm thinking that perhaps I've gone down the wrong road with this one! I might just look at the answer then learn all the methods from that, but I might be missing the point a bit there....
Re: Advice with make Card exercise...
Ahh, sorry if I got carried away. Back to basics.
Write the Card class first. No main() method, just the Card class with a constructor (or constructors). Imagine how a card object might be created and include the needed constructors. Then the getter() methods will do what you've asked about in your last question, so include the necessary getter methods, like getSuit() and getRank().
Re: Advice with make Card exercise...
This is what I just made posted, double posted then deleted :
Code :
public class Cards {
private String suit;
private String rank;
public String cardSuit(String suit){
this.suit = suit;
return suit;
}
public String cardRank(String rank){
this.rank = rank;
return rank;
}
public void getInformation(){
System.out.println("It's a " + rank + " of " + suit);
}
public static void main(String[] args) {
Cards myCard = new Cards();
myCard.cardRank("Three");
myCard.cardSuit("Diamonds");
myCard.getInformation();
}
}
I'll have a look at your suggestions now though thanks
--- Update ---
I've done it all in one class instead of separate but otherwise what I've posted seems to be what you suggested...? I haven't used constructors though... I'll re-do it now
--- Update ---
OK I'm not sure if what I posted in #7 was OK or not, but I've tried to do one in its own class :
Code :
public class CardClass {
private String rank, suit;
public CardClass(String rank, String suit){
this.rank = rank;
this.suit = suit;
}
public void getCardInformation(){
System.out.println(rank + " of " + suit);
}
}
Not sure if that's all that's needed?
Cheers greg
Re: Advice with make Card exercise...
It's a learning exercise. Look at the answer and learn.
A couple thoughts:
Remember, you'll want to be able to obtain the card object's attributes individually, as in getSuit() and getRank().
Your getCardInformation() method could be useful as a toString()-type of method, but it's usefulness in gameplay would be limited, or very specific to your implementation. getMethods() should return something and let the calling program decide what to do with the info.
Cheers, and keep coding!
Re: Advice with make Card exercise...
Ah yeah you're right, I've changed it about a bit:
Code :
public class CardClass {
private static final String[] suitArray = { "Hearts", "Diamonds", "Clubs",
"Spades" };
private static final String[] rankArray = { "one", "two", "three", "four",
"five", "six", "seven", "eight", "nine", "ten", "jack", "queen",
"king" };
private String rank, suit;
/*
* public CardClass(String rank, String suit){ this.rank = rank; this.suit =
* suit; }
*/
public String toString() {
return ("The cards rank is : " + rank + "\nThe suit is : " + suit);
}
public String getRank() {
return rank;
}
// I've made a mess here, tried to validate using a for loop to iterate through the rankArray
public void setRank(String rank) {
rankLabel: for (int i = 0; i < rankArray.length; i++) {
int j = i;
if ((suit != rankArray[j++])) {
System.out.println("Incorrect value");
continue rankLabel;
} else {
this.rank = rank;
}
}
}
public String getSuit() {
return suit;
}
// tried to do a bit of validation here, I don't really know any other way
public void setSuit(String suit) {
if ((suit != suitArray[0]) && (suit != suitArray[1])
&& (suit != suitArray[2]) && (suit != suitArray[3])) {
System.out.println("Incorrect value");
} else {
this.suit = suit;
}
}
public void getCardInformation() {
System.out.println(rank + " of " + suit);
}
}
I know I'm asking you to 'mark my work' basically so sorry about that! Learning solo and not knowing anyone who does this type of thing can be a bit barren though...
I've tried to borrow some of the method's used in continue with label, though this is probably a bad idea as apparently that program in the Oracle tutorial is very badly written and such a method is rarely used
Re: Advice with make Card exercise...
Why did you bail on the constructor?
I don't know that setters are required, but your validation logic is discussion worthy. I hardly ever see the continue/label: logic used. Yes, it's there, it's legal, it's perhaps due to someone's belief that GOTO was the greatest invention ever, but it's usually not necessary. (I'm sure someone could prove me wrong.) In this case, it's entirely not necessary, and Yes, you may have a bit of a mess there.
If you want to validate that the parameters rank or suit are valid, compare the parameters to every item in the appropriate array, and if found, set the value to the parameter and break out of the loop. If not found, indicate that an error has occurred and leave the value as is.
Not sure what else to say. Keep learning.
Re: Advice with make Card exercise...
Quote:
Originally Posted by
GregBrannon
Why did you bail on the constructor?
dropped the constructor because i had made the setters, but I guess I should have dropped the setters and used the constructor... .
Quote:
your validation logic is discussion worthy. I hardly ever see the continue/label: logic used. Yes, it's there, it's legal, it's perhaps due to someone's belief that GOTO was the greatest invention ever, but it's usually not necessary. (I'm sure someone could prove me wrong.) In this case, it's entirely not necessary, and Yes, you may have a bit of a mess there.
Yeah we (I) haven't really done much validation, although essentially it's just comparing stuff... The second way that I did it worked (Using the
Code :
if ((suit != suitArray[0]) && (suit != suitArray[1])
&& (suit != suitArray[2]) && (suit != suitArray[3])) {
System.out.println("Incorrect value");
} else {
this.suit = suit;
}
But it seemed a bit long winded so I thought Id try it a bit differently with the setRank part, though that one doesn't seem to have worked.
What would a better alternative be for the 'mess' on the setRank method?
Quote:
If you want to validate that the parameters rank or suit are valid, compare the parameters to every item in the appropriate array, and if found, set the value to the parameter and break out of the loop. If not found, indicate that an error has occurred and leave the value as is.
Isn't that what I did on the suitArray one?
cheers
Re: Advice with make Card exercise...
Quote:
Isn't that what I did on the suitArray one?
I suppose so, except I would use a loop. I was so wowed by the other one that I quit reading.
Re: Advice with make Card exercise...
Quote:
I was so wowed by the other one that I quit reading
I feel that I'm capable of much more 'wowing' I'm afraid!
Although if you haven't read until the end you should say otherwise I'm going to get even more confused than I already am with things.