k778899
k778899

Reputation: 33

Symbol can not be found in card class

Here I created a Card class that gives a value of suit and rank to a card I also created a Deck class that gives 52 cards, one of the standard playing cards, a shuffle method, deal method, report method( to report number left in deck) and toString method. My problem is in my Deck class when i try to make a new deck of cards. I receive this error from the Deck class:

Deck.java:12: error: cannot find symbol
      for (int suit = Card.DIAMONDS; suit <= Card.SPADES; suit++)               
symbol:   variable DIAMONDS
location: class Card

Deck.java:12: error: cannot find symbol
      for (int suit = Card.DIAMONDS; suit <= Card.SPADES; suit++)

symbol:   variable SPADES

I've tried changing Card.DIAMONDS to Suit.DIAMONDS and that gave me another error. Any help as to what method my problem is in would be appreciate. This is a learning experience for me so Id like guidance not exact solutions.

enum Suit {
    HEARTS, DIAMONDS, CLUBS, SPADES
};

enum Rank {
    ACE, TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN, JACK, QUEEN, KING
};

public class Card {

private Rank aRank;
private Suit aSuit;

public Card(Suit aSuit, Rank aRank) {
    this.aSuit = aSuit;
    this.aRank = aRank;
}

public Rank getRank() {
    return aRank;
}

public Suit getSuit() {
    return aSuit;
}

public String toString() {
    String cardValue = "";
    String rank = "";
    String suit = "";
    switch (aSuit) {
    case HEARTS:
      suit = "hearts";
      break;
    case DIAMONDS:
      suit = "diamonds";
      break;
    case CLUBS:
      suit = "clubs";
      break;
    case SPADES:
      suit = "spades";
      break;
  }

switch (aRank) {
  case ACE:
    rank = "Ace";
    break;
  case TWO:
    rank = "2";
    break;
  case THREE:
    rank = "3";
    break;
  case FOUR:
    rank = "4";
    break;
  case FIVE:
    rank = "5";
    break;
  case SIX:
    rank = "6";
    break;
  case SEVEN:
    rank = "7";
    break;
  case EIGHT:
    rank = "8";
    break;
  case NINE:
    rank = "9";
    break;
  case TEN:
    rank = "10";
    break;
  case JACK:
    rank = "Jack";
    break;
  case QUEEN:
    rank = "Queen";
    break;
  case KING:
    rank = "King";
    break;
}
cardValue += rank + " of " + suit;
return cardValue;
}

public int compareTo(Card other) {
    int rankComparison = aRank.compareTo(other.aRank);
    return rankComparison != 0 ? rankComparison : aSuit.compareTo(other.aSuit);
}

public boolean equals(Card other) {
    if (aRank == other.aRank)
        return true;
    if (aSuit == other.aSuit)
        return true;
    return false;
}

// Main method to test.
public static void main(String[] args) {
    Card c1 = new Card(Suit.SPADES, Rank.FIVE);
    Card c2 = new Card(Suit.HEARTS, Rank.TWO);
    Card c3 = new Card(Suit.CLUBS, Rank.EIGHT);
    Card c4 = new Card(Suit.DIAMONDS, Rank.FIVE);

    Card r1 = new Card(Suit.CLUBS, Rank.ACE);
    Card r2 = new Card(Suit.DIAMONDS, Rank.JACK);
    Card r3 = new Card(Suit.HEARTS, Rank.QUEEN);
    Card r4 = new Card(Suit.SPADES, Rank.KING);

    System.out.println(c1);
    System.out.println(c2);
    System.out.println(c3);

    if (c1.compareTo(c2) < 0)
        System.out.println(c2 + " outranks " + c1);
    else if (c1.compareTo(c2) > 0)
        System.out.println(c1 + " outranks " + c2);

    if (c1.compareTo(c3) < 0)
        System.out.println(c3 + " outranks " + c1);
    else if (c1.compareTo(c3) > 0)
        System.out.println(c1 + " outranks " + c3);

    if (c1.compareTo(c1) == 0)
        System.out.println(c1 + " is equal to " + c1);
    else
        System.out.println(c1 + " is NOT equal to " + c1);

    if (c1.equals(c4))
        System.out.println(c1 + " is equal to " + c4);
    else
        System.out.println(c1 + " is NOT equal to " + c4);

    if (r1.compareTo(r2) < 0)
        System.out.println(r1 + " comes before " + r2);
    else if (r1.compareTo(r2) > 0)
        System.out.println(r1 + " comes after " + r2);
    else
        System.out.println(r1 + " is equal to " + r2);

    if (r4.compareTo(r3) < 0)
        System.out.println(r4 + " comes before " + r3);
    else if (r4.compareTo(r3) > 0)
        System.out.println(r4 + " comes after " + r3);
    else
        System.out.println(r4 + " is equal to " + r3);
}
}

My deck class:

import java.lang.reflect.Array;
import java.util.*;
public class Deck{
   public static final int NEWCARDS = 52;
   private Card[] deckOfCards;        
   private int currentCard;           

   public void Deck( ) {
      deckOfCards = new Card[ NEWCARDS ];
      int i = 0;

      for (int suit = Card.DIAMONDS; suit <= Card.SPADES; suit++)
         for ( int rank = 1; rank <= 13; rank++ )
             deckOfCards[i++] = new Card(suit, rank);
             currentCard = 0;
   }
   public void shuffle(int n) {
      int i, j, k;

      for ( k = 0; k < n; k++ ){ 
      i = (int) ( NEWCARDS * Math.random() );  
      j = (int) ( NEWCARDS * Math.random() );  

      Card temp = deckOfCards[i];
      deckOfCards[i] = deckOfCards[j];
      deckOfCards[j] = temp;
      }
      currentCard = 0;   
   }

   public Card deal() {

      if (currentCard < NEWCARDS) {
         return ( deckOfCards[currentCard++] );
      }
      else{
         System.out.println("Out of cards error");
         return ( null );  
      }
   }

   public String toString() {
      String s = "";
      int k;
      k = 0;

      for ( int i = 0; i < 4; i++ ) {        
          for ( int j = 1; j <= 13; j++ )
             s += (deckOfCards[k++] + " ");
             s += "\n";
     }
      return (s);
   }
}

Upvotes: 0

Views: 130

Answers (1)

Elliott Frisch
Elliott Frisch

Reputation: 201507

The enum is not part of the class. It's also not an int. Change

for (int suit = Card.DIAMONDS; suit <= Card.SPADES; suit++)

to access Suit, and you need ordinal() to assign to an int. Something like

for (int suit = Suit.DIAMONDS.ordinal(); suit <= Suit.SPADES.ordinal(); suit++) {
    Suit s = Suit.values()[suit];
    // ...
}

Upvotes: 2

Related Questions