Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 13 of 13

Thread: Advice with make Card exercise...

  1. #1
    Member
    Join Date
    Dec 2013
    Posts
    51
    Thanks
    11
    Thanked 0 Times in 0 Posts

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


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

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

  3. #3
    Member
    Join Date
    Dec 2013
    Posts
    51
    Thanks
    11
    Thanked 0 Times in 0 Posts

    Default Re: Advice with make Card exercise...

    Quote Originally Posted by GregBrannon View Post
    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.....

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

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

  5. #5
    Member
    Join Date
    Dec 2013
    Posts
    51
    Thanks
    11
    Thanked 0 Times in 0 Posts

    Default 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 :/



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

  6. #6
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

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

  7. #7
    Member
    Join Date
    Dec 2013
    Posts
    51
    Thanks
    11
    Thanked 0 Times in 0 Posts

    Default Re: Advice with make Card exercise...

    This is what I just made posted, double posted then deleted :

    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 :

     
     
    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

  8. #8
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

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

  9. #9
    Member
    Join Date
    Dec 2013
    Posts
    51
    Thanks
    11
    Thanked 0 Times in 0 Posts

    Default Re: Advice with make Card exercise...

    Ah yeah you're right, I've changed it about a bit:

    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

  10. #10
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

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

  11. #11
    Member
    Join Date
    Dec 2013
    Posts
    51
    Thanks
    11
    Thanked 0 Times in 0 Posts

    Default Re: Advice with make Card exercise...

    Quote Originally Posted by GregBrannon View Post
    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... .

    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
    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?


    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

  12. #12
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Advice with make Card exercise...

    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.

  13. #13
    Member
    Join Date
    Dec 2013
    Posts
    51
    Thanks
    11
    Thanked 0 Times in 0 Posts

    Default Re: Advice with make Card exercise...

    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.

Similar Threads

  1. accessor and modifier method exercise; exercise 100
    By ghostheadx in forum What's Wrong With My Code?
    Replies: 6
    Last Post: December 30th, 2013, 10:18 PM
  2. Exercise 95 (I KNOW, I'm at an earlier exercise)
    By ghostheadx in forum What's Wrong With My Code?
    Replies: 9
    Last Post: December 24th, 2013, 08:42 PM
  3. Exercise 86
    By ghostheadx in forum What's Wrong With My Code?
    Replies: 11
    Last Post: December 7th, 2013, 11:31 PM
  4. Exercise
    By keepStriving in forum What's Wrong With My Code?
    Replies: 5
    Last Post: October 21st, 2013, 06:58 PM
  5. Help with exercise.
    By javapol in forum What's Wrong With My Code?
    Replies: 7
    Last Post: February 8th, 2013, 09:40 PM