8 Replies - 482 Views - Last Post: 01 December 2011 - 01:58 PM Rate Topic: -----

#1 jpb14  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 22-November 11

problem getting Card class to compile.

Posted 29 November 2011 - 03:45 AM

i am doing a game of solitaire for an assignment, and ive created a card and deck class but the card class will not compile, i get the error message, Cannot find Symbol - Variable cards.
could anyone help me debug this,
cheers.


import java.util.*;
public class Card
{

    private int value, suit;
    private static String[] suits = { "h", "s", "d", "c" };
    private static String[] values  = { "A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"};

    Card(int suit, int value)    {
        this.value=value;
        this.suit=suit;
    }

    int cardIndex = 0;
    public void method(){
        for (String suit : suits) {
            for (String card : cards) {
                cards[cardIndex] = new Card(suit, card);
                cardIndex++;
            }

        }
    }
}



Is This A Good Question/Topic? 0
  • +

Replies To: problem getting Card class to compile.

#2 Sheph  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 432
  • View blog
  • Posts: 1,020
  • Joined: 12-October 11

Re: problem getting Card class to compile.

Posted 29 November 2011 - 03:52 AM

-Where do you declare the array cards?
-The point of an extended for loop is so that you don't need to increment a variable like that.
for (String card : cards) {
 cards[cardIndex] = new Card(suit, card);
 cardIndex++;
}

-Perhaps you should consider making a deck class, too. My snippet of these got declined. :(

This post has been edited by Sheph: 29 November 2011 - 03:55 AM

Was This Post Helpful? 0
  • +
  • -

#3 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2652
  • View blog
  • Posts: 11,189
  • Joined: 20-September 08

Re: problem getting Card class to compile.

Posted 29 November 2011 - 03:54 AM

Think about finishing the Card class on line 12. Another class should handle a collection (pref.) or array of Card
Was This Post Helpful? 0
  • +
  • -

#4 Serk102  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 9
  • Joined: 19-June 11

Re: problem getting Card class to compile.

Posted 29 November 2011 - 06:08 PM

Yeah gonna have to agree with the other two on making some sort of deck class that handles the collection of cards. Then at the beginning of the deck class make the constructor run through and make an ordered deck. Also I'd make the values ints so you have an easier time if you plan on making some kind of game from this class.

Also if you do make a deck class collections.shuffle(deck) might spare you some trouble.
Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: problem getting Card class to compile.

Posted 29 November 2011 - 07:41 PM

Please avoid using variables that can only be differentiate by 1 letter

value and values, suit and suits is just very confusing and is really error prone


   private int value, suit;
   private static String[] suitStr = { "h", "s", "d", "c" };

    private static String[] valueStr  = { "A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"};
 

would mean a lot more sense and be a lot understandable
Was This Post Helpful? 0
  • +
  • -

#6 jpb14  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 22-November 11

Re: problem getting Card class to compile.

Posted 30 November 2011 - 09:26 AM

View Postg00se, on 29 November 2011 - 03:54 AM, said:

Think about finishing the Card class on line 12. Another class should handle a collection (pref.) or array of Card


ive got a deck class, so would you advise getting rid of line 13 onwards and creating the array within the deck class?
Was This Post Helpful? 0
  • +
  • -

#7 Sheph  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 432
  • View blog
  • Posts: 1,020
  • Joined: 12-October 11

Re: problem getting Card class to compile.

Posted 30 November 2011 - 04:14 PM

Yes, the cards would probably be more logically initialized in the deck class when you initialize the array. You might also want to create some getter methods for your suit and value fields in your Card class.
Was This Post Helpful? 1
  • +
  • -

#8 jpb14  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 22-November 11

Re: problem getting Card class to compile.

Posted 01 December 2011 - 03:24 AM

View PostSheph, on 30 November 2011 - 04:14 PM, said:

Yes, the cards would probably be more logically initialized in the deck class when you initialize the array. You might also want to create some getter methods for your suit and value fields in your Card class.



how would i create the setter and getter to make it create all 52 cards in the deck? or would i put that in the deck class?
Was This Post Helpful? 0
  • +
  • -

#9 Sheph  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 432
  • View blog
  • Posts: 1,020
  • Joined: 12-October 11

Re: problem getting Card class to compile.

Posted 01 December 2011 - 01:58 PM

In your Card class:

public int getSuit() {
   return suit;
}

public int getValue() {
   return value;
}

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1