Solved

constructor and error messages

Posted on 2007-11-18
8
226 Views
Last Modified: 2013-11-23
I am writing a program that plays war between two people. However, I am getting four error messages and not sure how to fix them. One is a constructor error, three are method errors all within my main. Here they are:

Game.java:152: cannot find symbol
symbol  : constructor Game(Game)
location: class Game
             Game g = new Game(g);
                         ^
Game.java:154: cannot find symbol
symbol  : method go()
location: class Game
            g.go();
                 ^
Game.java:156: cannot find symbol
symbol  : method goFast()
location: class Game
            g.goFast();
                 ^
Game.java:158: cannot find symbol
symbol  : method playMany(int)
location: class Game
            g.playMany(100);
                 ^
4 errors


public class Game implements Comparable<Game>
{  
    public static final int TWO = 0;
    public static final int THREE = 1;
    public static final int FOUR = 2;
    public static final int FIVE = 3;
    public static final int SIX = 4;
    public static final int SEVEN = 5;
    public static final int EIGHT = 6;
    public static final int NINE = 7;
    public static final int TEN = 8;
    public static final int JACK = 9;
    public static final int QUEEN = 10;
    public static final int KING = 11;
    public static final int ACE = 12;

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

    private static final String[] ourRanks = {"2", "3", "4",
        "5", "6", "7", "8", "9", "10", "Jack",
        "Queen", "King", "Ace"};
    private static final String[] ourSuits = {"Clubs", "Diamonds", "Hearts", "Spades"};

    private final int mySuit;
    private final int myRank;

    //constructors

    /**
     * Default constructor.
     * <br>pre:  none<br>
     * <br>post:   getRank() = TWO, getSuit() = CLUBS
     */
    public Game(){  
          this(TWO, CLUBS);
    }

    /**
     * Create a card with the specified rank and suit.
     * <br> pre: validSuit(suit), validRank(rank)
     * <br> post:   getRank() = rank, getSuit() = suit
     */
    public Game(int rank, int suit){  
          assert validSuit(suit) && validRank(rank) : "Failed precondition. validSuit(suit): "
                + validSuit(suit) + ", validRank(rank): " + validRank(rank) ;
          myRank = rank;
        mySuit = suit;
    }
   
    //accessors

    /**
     * get this Card's rank.
     * <br>pre: none
     * post: returns this Card's rank
     * @return this Card's rank
     */
    public int getRank(){      
          return myRank;    
    }

    /**
     * get this Card's suit.
     * <br>pre: none<br>
     * post: returns this Card's suit
     * @return this Card's suit
     */
    public int getSuit(){  
          return mySuit;
    }

    /**
     * determine if a given int represents a valid rank for this classes' cards.
     * <br>pre: none<br>
     * <br>post: returns true if rank between TWO and ACE inclusive, false otherwise
     * @return returns true if rank between TWO and ACE inclusive, false otherwise
    **/
    public static boolean validRank(int rank){  
          return (TWO <= rank) && (rank <= ACE);    
    }

    /**
     * determine if a given int represents a valid suit for this classes' cards.
     * <br>pre: none<br>
     * <br>post: returns true if rank between CLUBS and SPADES inclusive, false otherwise
     * @return returns true if rank between CLUBS and SPADES inclusive, false otherwise
    **/
    public static boolean validSuit(int suit){
          return (CLUBS <= suit) && (suit <= SPADES);
    }

    /**
     * check to see if two Cards are equal.
     * <br>pre: none
     * <br>post: return true if otherCard has the same suit and rank as this Card, false otherwise
     * @return return true if otherCard has the same suit and rank as this Card, false otherwise
     */
    public boolean equals(Object otherCard){  
          boolean result = false;
        if(otherCard == this)
            result = true;
        else if(otherCard != null && getClass() == otherCard.getClass())
        {   Game tempCard = (Game) otherCard;
            result = (myRank == tempCard.myRank)
                && (mySuit == tempCard.mySuit);
        }
        return result;
    }
   
    /**
     * return a string representation of this card.
     * <br>pre: none
     * <br>post: return a string representation of this card.
     * @return return a string representation of this card.
     */
    public String toString(){
          return ourRanks[getRank()] + " of " + ourSuits[getSuit()];  
    }
   
    /**
     * compare this Card and another Card.
     * Objects of this class are ordered based on rank, with ACE being highest and TWO being
     * lowest. Ties in ranks are broken by rank with the order of ranks from lowest to
     * highest being CLUBS, DIAMONDS, HEARTS, SPADES
     * <br>pre: other != null
     * <br>post: return a positive int if the calling Card is greater than other.
     * return 0 if the two Cards are equal. Return a negative int if the calling Card
     * is less than other
     * @param other
     * @return
     */
    public int compareTo(Game otherCard){  
          int result = getRank() - otherCard.getRank();
          if( result == 0)
                result = getSuit() - otherCard.getSuit();
        return result;
    }
      
       public static void main(String[] args)
       {      
             Game g = new Game(g);
            System.out.println("Game with pauses: ");
            g.go();
            System.out.println("\n\nGame without pauses: ");
            g.goFast();
            System.out.println("\n\n100 games: ");
            g.playMany(100);

      }
}
0
Comment
Question by:monroe6
  • 4
  • 2
  • 2
8 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 20309695
That means you haven't got those ctors and methods with those prototypes

>>Game g = new Game(g);

should be

Game g = new Game();

etc.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 20309700
You don't have go or goFast at all
0
 
LVL 92

Expert Comment

by:objects
ID: 20309718
your game constructors has don't accept a Game
you need to pass either nothing, or rank and suit

    public Game(){

    public Game(int rank, int suit){  

And the methods you are calling do not exist
0
 
LVL 92

Expert Comment

by:objects
ID: 20309723
The code looks like code for a card,  and not your Game :)
Check u are actually using the right code.
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:monroe6
ID: 20332198
ok, I got what you were saying, so I have code for the game. But now I have a new issue which is that this code still has an error. It says class or interface expected at line number 3. How can this be and how do I fix it?

import java.util.*;

char uniformRandom;

public class WarCardGame {

    public static void main (String[] args)
    {
        // Use cards numbered 0,..,9 and deal 26 cards to each player.
      playGame (5, 10);
    }


    static void playGame (int dealSize, int numCards)
    {
        // Cards dealt out to player 1:
      LinkedList<Integer> player1 = new LinkedList<Integer>();
        // Cards dealt out to player 2:
      LinkedList<Integer> player2 = new LinkedList<Integer>();

        // The pile between the two players:
      LinkedList<Integer> pile = new LinkedList<Integer>();


        // Make the cards and shuffle them randomly.
      int[] cards = new int [numCards];
      for (int i=0; i<cards.length; i++) {
          cards[i] = i;
      }
      shuffle (cards);

      // Deal cards to each player.
      int cardCount = 0;
      for (int k=0; k<dealSize; k++) {
          player1.add (cards[cardCount]);
          player2.add (cards[cardCount+1]);
          cardCount += 2;                                   
      }

      // Now play.
      boolean done = false;
      int round = 0;

      while (! done) {

          // Each player plays their first card.
          int player1first = player1.removeFirst ();
          pile.add (player1first);
          int player2first = player2.removeFirst ();
          pile.add (player2first);

          System.out.println ("Round 0: player1's card = " + player1first + "  player2's card=" + player2first);

          if (player1first > player2first+2) {
            // Add pile into player 1's cards.
            addListToList (pile, player1);
            System.out.println ("  => player1 gets pile");
          }
          else if (player2first > player1first+2) {
            // Add pile into player 2's cards.
            addListToList (pile, player2);
            System.out.println ("  => player2 gets pile");
          }
          else {
            System.out.println ("  => both cards added to pile");
          }

          if (player1.isEmpty()) {
            System.out.println ("Player 2 wins!");
            done = true;
          }
          else if (player2.isEmpty()) {
            System.out.println ("Player 1 wins!");
            done = true;
          }

      } //end-while

    }


    static void shuffle (int[] A)
    {
             
      for (int i=0; i<A.length-1; i++) {
          int k = uniformRandom.uniform (i, A.length-1);
          int temp = A[i];
          A[i] = A[k];
          A[k] = temp;
      }
    }
}
0
 
LVL 92

Expert Comment

by:objects
ID: 20332239
get rid of the line:

char uniformRandom;
0
 

Author Comment

by:monroe6
ID: 20332255
I tried that before and when I remove it I get this error:

WarCardGame.java:84: cannot find symbol
symbol  : variable uniformRandom
location: class WarCardGame
          int k = uniformRandom.uniform (i, A.length-1);
0
 
LVL 92

Accepted Solution

by:
objects earned 500 total points
ID: 20332259
you need to add the declaration of that var in you class, but its not a char
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
HashMap Vs TreeMap 12 49
Starting to learn JAVA, 7 47
create a gui in perl 3 45
Java passing arguments (type error) 2 35
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now