Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

syntax error when calling method

Status
Not open for further replies.

gemann

Technical User
May 21, 2002
9
CA
Hi, I'm getting this error when I call :
myCard.addCardBackInHand(card);
this is the error I get:
Code:
C:\java\War>javac TryDeal.java
TryDeal.java:23: cannot resolve symbol
symbol  : variable card
location: class TryDeal
    Hand returnHand = myCard.addCardBackInHand(card);
                                               ^
1 error


these are some of my classes :
Code:
import java.util.*;
class Hand
{
  private Stack hand = new Stack();
   public void add(Card card)
   { 
     hand.push(card);
   }
   public Card getCard()
   {
     return (Card)hand.pop();
   }
   public Card addCardBackInHand(Card card)
    {
      return (Card)hand.push(card);
    }
   public String toString()
   { 
     Iterator cards = hand.iterator();
     StringBuffer str = new StringBuffer();
      while(cards.hasNext()) 
      str.append(" "+ (Card)cards.next());
      return str.toString();
  }


Code:
class TryDeal
{
  public static void main(String[] args)
  {
    CardDeck deck = new CardDeck();
    deck.shuffle();

    Hand myHand = deck.dealHand(5);
    Hand yourHand = deck.dealHand(5);
  

    Card myCard   = (Card)myHand.getCard();
    Card yourCard = (Card)yourHand.getCard(); 

    Hand returnHand = myCard.addCardBackInHand(card);
    
    System.out.println("\nYour hand is"+yourCard);
    System.out.println("\nMy hand is"+myCard);
    System.out.println("\nMy return hand is"+returnHand);
  }
}


Code:
class Card implements Comparable
{

  public Card(int value, int suit) throws IllegalArgumentException
  {
    if(value >= ACE && value <= KING)
      this.value = value;
    else
      throw new IllegalArgumentException(&quot;Invalid card value&quot;);
    if(suit >= HEARTS && suit <= SPADES)
      this.suit = suit;
    else
      throw new IllegalArgumentException(&quot;Invalid suit&quot;);
  }
 public int compareTo(Object card)
  {
    if(this.suit != ((Card)card).suit)
      return suit < ((Card)card).suit ? -1: 1;
    else
      if(this.value == ((Card)card).value)
        return 0;
      else
        return value < ((Card)card).value ? -1 : 1;
  }

  public String toString()
  {
    String cardStr;
    switch(value)
    {
      case ACE: cardStr = &quot;A&quot;;
                break;
      case JACK: cardStr = &quot;J&quot;;
                break;
      case QUEEN: cardStr = &quot;Q&quot;;
                break;
      case KING: cardStr = &quot;K&quot;;
                break;
      default: cardStr = Integer.toString(value);
                break;                       
    }

    // Here we add the suit to the value of the card

    switch(suit)
    {
      case CLUBS: cardStr += &quot;C&quot;;
                break;
      case DIAMONDS: cardStr += &quot;D&quot;;
                break;
      case HEARTS: cardStr += &quot;H&quot;;
                break;
      case SPADES: cardStr += &quot;S&quot;;
                break;
    }
    return cardStr;
  }


  // Suit values
  public static final int HEARTS = 0;
  public static final int CLUBS = 1;
  public static final int DIAMONDS = 2;
  public static final int SPADES = 3;

  // Card face values
  public static final int ACE = 1;
  public static final int JACK = 11;
  public static final int KING = 12;
  public static final int QUEEN = 13;

  private int suit;
  private int value;
}


I tried to instanciate a new Card object :
Card card = new Card(); but I got these errors :

Code:
C:\java\War>javac TryDeal.java
TryDeal.java:13: cannot resolve symbol
symbol  : constructor Card  ()
location: class Card
    Card card = new Card();
                ^
TryDeal.java:24: cannot resolve symbol
symbol  : method addCardBackInHand  (Card)
location: class Card
    Hand returnHand = myCard.addCardBackInHand(card);
                            ^
2 errors

I need some help defining the card variable.....gemann
 
Any non-abstract class without an explicit constructor will have a default constructor, but if you define one that takes arguments, you can no longer use the default, no-arguments constructor. The other error occurs when you try to assign a Card object to a Hand reference- addCardBackInHand(card); returns a Card, and it can't be cast to a Hand.
 
I guess the next question is what constructor can I use?
Thanks....gemann
 
In order to use the default constructor after you defined one with argument you have to re-define the default constructor,i.e. to define one that excepts no arguments.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top