?
Solved

constructor and error messages

Posted on 2007-11-18
8
Medium Priority
?
235 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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
 

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 1500 total points
ID: 20332259
you need to add the declaration of that var in you class, but its not a char
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This video teaches viewers about errors in exception handling.
Suggested Courses
Course of the Month11 days, 13 hours left to enroll

752 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