Results 1 to 3 of 3
  1. #1
    turbowhat is offline Member
    Join Date
    Feb 2011
    Posts
    3
    Rep Power
    0

    Default Possible someone can take a look at my code?

    My setValue and setSuit are throwing errors.........they say: can not assign a value to a final variable value.........? Did I do something wrong here? Any advice is appreciated, I'm self teaching w/ Joyce Farrell's Java book......I'm just lost right now......




    Java Code:
    public class deckcards {
    
        private final int value;
        private final int suit;
        /**
         * Kinds of suits diamond/club/heart/spade
         * 
         */
        public final static int DIAMONDS = 1;
        public final static int CLUBS = 2;
        public final static int HEARTS = 3;
        public final static int SPADES = 4;
        /**
         * Kinds of ranks 1-13 inclusive
         * 
         */
        public final static int ACE = 1;
        public final static int TWO = 2;
        public final static int THREE = 3;
        public final static int FOUR = 4;
        public final static int FIVE = 5;
        public final static int SIX = 6;
        public final static int SEVEN = 7;
        public final static int EIGHT = 8;
        public final static int NINE = 9;
        public final static int TEN = 10;
        public final static int JACK = 11;
        public final static int QUEEN = 12;
        public final static int KING = 13;
    
        /**
         * use value and suit in THIS form...........
         * @param value
         * @param suit
         */
        public deckcards(int value, int suit) {
            assert isValidValue(value);
            assert isValidSuit(suit);
            this.value = value; //assign the cards value = local card value
            this.suit = suit;
        }
    
        /**
         * getter for Value method..........
         * @return
         */
        public int getValue() {
            return value;
        }
    
        [COLOR="Red"]/**
         * setter for Value method............
         * 
         */
        public void setValue(int value) {
            this.value = value;
        }[/COLOR]
    
        /**
         * getter for suit method.........
         * @return
         */
        public int getSuit() {
            return this.suit;
        }
    
    
        [COLOR="#ff0000"]public void setSuit(int suit) {
            this.suit = suit;
        }[/COLOR]
    
           /**
            * tag booleans for each set of suits..........
            * @param Value
            * @return
            */
        public static boolean isValidValue(int Value) {
            return ACE <= Value && Value <= KING;
        }
    
        public static boolean isValidSuit(int suit) {
            return DIAMONDS <= suit && suit <= SPADES;
        }
    
        /**
         * strings of ranks or 1-13 inclusive numbers..........
         * @param value
         * @return
         */
        public static String valueToString(int value) {
            switch (value) {
                case ACE:
                    return "Ace";
                case TWO:
                    return "Two";
                case THREE:
                    return "Three";
                case FOUR:
                    return "Four";
                case FIVE:
                    return "Five";
                case SIX:
                    return "Six";
                case SEVEN:
                    return "Seven";
                case EIGHT:
                    return "Eight";
                case NINE:
                    return "Nine";
                case TEN:
                    return "Ten";
                case JACK:
                    return "Jack";
                case QUEEN:
                    return "Queen";
                case KING:
                    return "King";
                default:
                    //Handle an illegal argument.
                    return null;
            }
        }
    
        /**
         * string field called suit for all suits D/C/H/S...........
         * @param suit
         * @return
         */
        public static String suitToString(int suit) {
            switch (suit) {
                case DIAMONDS:
                    return "Diamonds";
                case CLUBS:
                    return "Clubs";
                case HEARTS:
                    return "Hearts";
                case SPADES:
                    return "Spades";
                default:
                    return null;
            }
        }
    
        /**
         * Main entry point of program
         * @param args
         */
        public static void main(String[] args) {
    
    
            /**
             *use assert statements to declare what variables are what.............
             * 
             */
            assert valueToString(ACE) == "Ace";
            assert valueToString(TWO) == "Two";
            assert valueToString(THREE) == "Three";
            assert valueToString(FOUR) == "Four";
            assert valueToString(FIVE) == "Five";
            assert valueToString(SIX) == "Six";
            assert valueToString(SEVEN) == "Seven";
            assert valueToString(EIGHT) == "Eight";
            assert valueToString(NINE) == "Nine";
            assert valueToString(TEN) == "Ten";
            assert valueToString(JACK) == "Jack";
            assert valueToString(QUEEN) == "Queen";
            assert valueToString(KING) == "King";
    
            assert suitToString(DIAMONDS) == "Diamonds";
            assert suitToString(CLUBS) == "Clubs";
            assert suitToString(HEARTS) == "Hearts";
            assert suitToString(SPADES) == "Spades";
    
        }
    }

  2. #2
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    952
    Rep Power
    5

    Default

    It's not clear what you're trying to do, but I would suggest you want at least two classes: Card and Deck. A Card is not the same thing as a Deck of Cards, after all. If you search the forum, you'll find a lot of code along these lines, but I certainly understand if you want to struggle through it yourself. You should also consider using enum types rather than constants.

    -Gary-

  3. #3
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    final means constant. Any type that has the final modifier cannot be reassigned after it has been assigned a value.

Similar Threads

  1. Replies: 0
    Last Post: 02-21-2011, 12:50 PM
  2. Replies: 1
    Last Post: 03-04-2010, 12:19 PM
  3. Convert java code to midlet code
    By coldvoice05 in forum New To Java
    Replies: 1
    Last Post: 08-12-2009, 12:14 PM
  4. Convert java code to midlet code
    By coldvoice05 in forum Advanced Java
    Replies: 1
    Last Post: 08-09-2009, 02:21 PM
  5. Replies: 1
    Last Post: 04-26-2007, 04:52 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
  •