Results 1 to 10 of 10
  1. #1
    zendavis is offline Member
    Join Date
    Jul 2011
    Posts
    4
    Rep Power
    0

    Default Problems with making a class. New to Java.

    I am presented with the above problem and I am looking for help in how to solve it. The problem was presented in the book "The Art and Science of Java".

    I've never written a class before and would like some advice on how to proceed. Here is my current code. I believe I've gotten the constants portions right but I'm confused about the constructor taking a rank and a suit and returning a Card with the values. I thought constructors did not return anything? Is that what is being asked of me in this task?

    I'm also lost as to how the getRank and getSuit methods would be applied in this context. Can anyone help me sort out this question and how I would go about achieving what's been asked of me?

    /* Implement a new class called Card that includes the following entries:
    *
    • Named constants for the four suits (CLUBS, DIAMONDS, HEARTS, SPADES) and the
    four ranks that are traditionally represented in words (ACE, JACK, QUEEN, KING).
    The values of the rank constants should be 1, 11, 12, and 13, respectively.

    • A constructor that takes a rank and a suit and returns a Card with those values.

    • Accessor methods getRank and getSuit to retrieve the rank and suit components
    of a card.

    • An implementation of the toString method that returns the complete name of the
    card as in exercise 1. Remember that you can use the + operator to connect the
    different parts of the string together, as shown in the toString implementation
    for the Rational class in Figure 6-9.

    */

    import acm. util.*;
    import acm.program.*;

    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 CLUBS = "Clubs";
    public static final String HEARTS = "Hearts";
    public static final String SPADES = "Spades";
    public static final String DIAMONDS = "Diamonds";

    public static final String SUIT;
    public static final int RANK;

    public Card (String Suit, int Rank){
    this.Suit =
    GetRank (Rank);
    GetSuit (Suit);
    }

    private int GetRank(int pickACard) {
    switch (pickACard) {
    case 1: return (ACE);
    case 2: return (JACK);
    case 3: return (QUEEN);
    case 4: return (KING);
    default: return (0);
    }
    }

    private String GetSuit() {
    switch () {
    case 1: return (CLUBS);
    case 2: return (HEARTS);
    case 3: return (DIAMONDS);
    case 4: return (SPADES);
    default: return ("Error. Try Again!");
    }
    }
    }
    Last edited by zendavis; 07-24-2011 at 01:26 AM.

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    So,... Does your code compile? If not, and you can't understand the compiler messages, what are they? Post the whole message and I'm sure someone will help explain what it means.

    -----

    You are working along the right lines. One thing that does strike me is that you should use standard Java coding conventions and start variables and methods with a lowercase letter: getRank() etc. static final things are written, as you do, all upper case (they are special like the speed of light or Newton's gravitational constant. They don't change at all ever, unlike everything else in your program that will have a different value from time to time.)

    Do you understand "static"? If not, read your textbook. I can see why the four named card values and four suits are static, but I can't see why SUIT and RANK are static.

    -----

    Also, now is possibly the right time to get it right and add a small javadoc comment to your methods explaining what each one does:

    Java Code:
        /** Returns the suit of this card. */
    public String getSuit() {
        switch () {
            case 1: return (CLUBS);
            case 2: return (HEARTS);
            case 3: return (DIAMONDS);
            case 4: return (SPADES);
            default: return ("Error. Try Again!");
        }
    }
    And, yes, the "getters" should be public. Why did you change them to provate?
    Last edited by pbrockway2; 07-24-2011 at 01:33 AM.

  3. #3
    zendavis is offline Member
    Join Date
    Jul 2011
    Posts
    4
    Rep Power
    0

    Default

    Quote Originally Posted by pbrockway2 View Post
    So,... Does your code compile? If not, and you can't understand the compiler messages, what are they? Post the whole message and I'm sure someone will help explain what it means.

    -----

    You are working along the right lines. One thing that does strike me is that you should use standard Java coding conventions and start variables and methods with a lowercase letter: getRank() etc. static final things are written, as you do, all upper case (they are special like the speed of light or Newton's gravitational constant. They don't change at all ever, unlike everything else in your program that will have a different value from time to time.)

    Do you understand "static"? If not, read your textbook. I can see why the four named card values and four suits are static, but I can't see why SUIT and RANK are static.

    -----

    Also, now is possibly the right time to get it right and add a small javadoc comment to your methods explaining what each one does:

    Java Code:
        /** Returns the suit of this card. */
    public String getSuit() {
        switch () {
            case 1: return (CLUBS);
            case 2: return (HEARTS);
            case 3: return (DIAMONDS);
            case 4: return (SPADES);
            default: return ("Error. Try Again!");
        }
    }
    And, yes, the "getters" should be public. Why did you change them to provate?
    Thank you for responding!

    I've gotten extremely frustrated with this question and started entering things quickly without taking note of all the Java conventions like javadoc comments and improper capitalization. I haven't attempted to compile my code as I haven't gotten my head around exactly what the question is asking me to do. Once more, for clarity's sake:

    Implement a new class called Card that includes the following entries:

    Named constants for the four suits (CLUBS, DIAMONDS, HEARTS, SPADES) and the
    four ranks that are traditionally represented in words (ACE, JACK, QUEEN, KING).
    The values of the rank constants should be 1, 11, 12, and 13, respectively.

    A constructor that takes a rank and a suit and returns a Card with those values.

    Accessor methods getRank and getSuit to retrieve the rank and suit components
    of a card.

    An implementation of the toString method that returns the complete name of the
    card as in exercise 1. Remember that you can use the + operator to connect the
    different parts of the string together, as shown in the toString implementation
    for the Rational class in Figure 6-9.


    I understand HOW to make the constants, but I'm lost as to what I'm working towards.

    Also, I'm unsure of how create a constructor (I believe it's the same name as the class and it initializes... something) and I thought it wasn't supposed to return anything at all. Yet the question is asking me to create a constructor that returns a Card with values.

    I'm finally also confused as to what role the getRank and getSuit methods would play.
    Last edited by zendavis; 07-24-2011 at 01:41 AM.

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    I've gotten extremely frustrated with this question and started entering things quickly without taking note of all the Java conventions like javadoc comments and improper capitalization.
    The continual pickyness you'll find on forums like this isn't just because we're neurotic: it's because we know that frustration won't lead anywhere (good) and the only cure is to work slowly, patiently, step by step, getting each step right.

    I haven't attempted to compile my code as I haven't gotten my head around exactly what the question is asking me to do
    OK. Step one ("Named constants for the four suits...") you have done. Make sure you understand why those variables have been made static and final. It's the correct thing to do - but make sure you understand why. Ie why "static" and "final" capture aspects of what we mean by "constant".

    At this point you should remove the attempts at writing a constructor and methods, compile the code with just the class definition and the constants and ensure that it really does comple without error.

    Then write the constructor. Its javadoc is something along the lines of "/** Constructs a card with given suit and rank. */" Same deal: stop and compile. If there are messages you can't understand, post them. Even if it does compile but you are unsure that it meets the question's requirements, post the code.

  5. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    I understand HOW to make the constants, but I'm lost as to what I'm working towards.
    You don't have to overthink this but, basically, what you are working towards is the creation of a type of thing (a card) in a universe of your own making. The card will have characteristics (suit and rank) and most importantly it will have behaviour (the ability to return what its suit and rank are etc). The roles played by getSuit() and getRank() are that they are this public behaviour: the ability of a card to say what its suit and rank are. The actual suit and rank variables are not accessible directly but only through this behaviour. Again, don't overthink it, but the reason for having private (hidden/inaccessible) variables with public (open/accessible) getters is that you will forbid someone to just change the rank of a card (from a Jack to a King, say) which would be cheating in an actual game of cards.

    Also, I'm unsure of how create a constructor (I believe it's the same name as the class and it initializes... something) and I thought it wasn't supposed to return anything at all. Yet the question is asking me to create a constructor that returns a Card with values.
    You are right (and the question wrong, or at least badly worded) constructors don't return anything. The point is that the Card class has two variables String suit and int rank that mirror the fact that a card has the attributes suit and rank. The job of the constructor is - as mentioned above - to create an instance of the Card class that has the suit and rank values that you give it. It will initialise the suit and rank variables, and it will initialise them to the values that it is passed as arguments.
    Last edited by pbrockway2; 07-24-2011 at 02:13 AM.

  6. #6
    zendavis is offline Member
    Join Date
    Jul 2011
    Posts
    4
    Rep Power
    0

    Default

    Thank you for responding again.

    What I still don't get is what I'm supposed to do with all the named constants.

    I've made changes. This is what I have now. I don't understand what the point of assigning all the suits to the constants.

    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 CLUBS = "Clubs";
    public static final String HEARTS = "Hearts";
    public static final String SPADES = "Spades";
    public static final String DIAMONDS = "Diamonds";

    public String suitHi;
    public int rankHi;

    public Card (String suit, int rank){
    this.suitHi = suit;
    this.rankHi = rank;
    }

    private int getRank() {
    return rankHi;

    }

    private String getSuit() {
    return suitHi;
    }
    }
    Last edited by zendavis; 07-24-2011 at 02:44 AM.

  7. #7
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    You don't do anything with them just yet. But they will be very useful in some other class that wants to create and use cards.

    A user of your Card class may want to create the ace of spades. They will do it by calling the constructor you are writing. But what arguments will they use to tell the constructor to create the ace of spades and not some other card? The answer is that the caller will use the constant values and say something like:

    Java Code:
    Card card = new Card(Card.SPADES, Card.ACE);
    // now card is the ace of spades

  8. #8
    zendavis is offline Member
    Join Date
    Jul 2011
    Posts
    4
    Rep Power
    0

    Default

    Quote Originally Posted by pbrockway2 View Post
    OK. Step one ("Named constants for the four suits...") you have done. Make sure you understand why those variables have been made static and final. It's the correct thing to do - but make sure you understand why. Ie why "static" and "final" capture aspects of what we mean by "constant".
    Lets backtrack a little bit so I can better understand the why of things.

    I understand that making the named constants for the four suits makes it so that they cannot be changed. The why of that is still confusing to me. Any advice?

    Is it basically so that someone else can't change the value of the cards?

    Furthermore: Card card = new Card(Card.SPADES, Card.ACE);

    This format is basically used to call constants from the "Card" class? Correct?
    Last edited by zendavis; 07-24-2011 at 05:40 AM.

  9. #9
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    I understand that making the named constants for the four suits makes it so that they cannot be changed.
    No. It's true that they can't be changed (because they're final). But you make them so that the card class and other classes can use the constants. You are making the Card class, and you get to say (define) what it is to be a card. By defining SPADES you are saying that one of the things about cards is that there exists a spades suit that any particular card may (or may not) have.

    Suppose you were God and you were creating a Photon class. One of the things you might do is create a public static final int SPEED, the speed of light, that all the photons (and anybody using the Photon class) could use, for instance to tell how far any particular photon would travel in a second.

    There is, as it happens, just one constant speed of light that all photons share, defining SPEED is part of defining what a photon is. And there are, as it happens, just four suits that all cards share and defining SPADES etc is part of defining what a card is.

    Furthermore: Card card = new Card(Card.SPADES, Card.ACE);

    This format is basically used to call constants from the "Card" class? Correct?
    Yes. Constants are typically used together with the name of the class of which they are part of the definition. Different classes may have their own concept denoted with "ACE" and this syntax allows us to say Card.ACE and TennisServe.ACE without any confusion.

  10. #10
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,436
    Blog Entries
    7
    Rep Power
    20

    Default

    To throw some oil on the fire: nothing forbids me to create a Card like this: new Card("foo", 42). It doesn't matter much but just some food for thoughts ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. help making class print in jpanel
    By Grid_iso in forum New To Java
    Replies: 1
    Last Post: 05-19-2011, 02:39 PM
  2. Newbie Need help Making Java to class and signing
    By altainta in forum Java Applets
    Replies: 1
    Last Post: 02-20-2011, 04:47 PM
  3. Making Data available to every class in a project.
    By JOHNINALBANY in forum New To Java
    Replies: 5
    Last Post: 12-15-2010, 09:09 AM
  4. Problems making a JTable's cells transparent
    By Muskar in forum New To Java
    Replies: 5
    Last Post: 12-09-2010, 09:50 PM
  5. Replies: 13
    Last Post: 08-23-2008, 09:09 PM

Posting Permissions

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