Constructors

Hi I am writing a program that contains two classes and a main driver. I am creating a "new Player" Class within the main driver but it keeps telling me it cannot find the constructor for player? I don't understand ? Why should it say this when the constructor has been built .? I am very new to this and have been searching through the web and in the forum but can't see a question to my answer. I just want to know why it cant find it when it has been created? many thanks for your time and reading.

[510 byte] By [Camzio45a] at [2007-11-26 20:20:25]
# 1
How could we tell you without seeing your code? Post the code of your driver, and player class. And also the exact compiler error.When posting code, please use code tags as described here: http://forum.java.sun.com/help.jspa?sec=formatting
prometheuzza at 2007-7-10 0:44:52 > top of Java-index,Java Essentials,New To Java...
# 2

> I just want to know why it cant find it when it has been

> created? many thanks for your time and reading.

Your compiler doesn't look in your current working directory by default

for already compiled other classes. It check the value of the 'classpath'

which is a list of directories and .jar files. You can set the value of this

classpath on the command line:

javac -classpath . YourFile.java

Note the dot; it indicates the current working directory to look for for

already compiled class files.

kind regards,

Jos

JosAHa at 2007-7-10 0:44:52 > top of Java-index,Java Essentials,New To Java...
# 3

private String name;

private Game game1;

private Game game2;

//Constructor for objects of class Player

public Player(String firstname, Game agame1, Game agame2)

{

// initialise instance variables

name = firstname;

game1 = agame1;

game2 = agame2;

}

public String tostring()

{

String result;

result = "Details for player" + name + " \n";

result += "Chess Score = " + game1 + "\n";

result += "Hockey Score = " + game2 ;

return result;

}

}

public Game(String aname, int ascore)

{

name = aname;

score= ascore;

}

public String tostring()

{

String results;

results = name + " \n";

results = score +" \n";

results = score +" \n";

return results;

}

}

public class TestGame

{

public static void main(String [] args)

{

Player lizzy = new Player("lizzy");

Game lscore = new Game ("45");

Game lscore = new Game ("27");

System.out.println(lizzy);

System.out.println( + score);

System.out.println( + score);

}

}

Appreciate you didn't have my code "sorry"

Camzio45a at 2007-7-10 0:44:52 > top of Java-index,Java Essentials,New To Java...
# 4
Sorry the actual comment was cannot find symbol-constructor player (java.lang.string)
Camzio45a at 2007-7-10 0:44:52 > top of Java-index,Java Essentials,New To Java...
# 5

Hi,

In the Player class you define the following constructor:

public Player(String firstname, Game agame1, Game agame2)

That is, a constructor which accepts a String and two Game objects as parameters.

But in the TestGame class you call the Player constructor like this:

Player lizzy = new Player("lizzy");

That's why the compiler is issuing this message.

Regards,

Juliano

JulianoAlbertoa at 2007-7-10 0:44:52 > top of Java-index,Java Essentials,New To Java...
# 6

> Sorry the actual comment was cannot find

> symbol-constructor player (java.lang.string)

That means you are trying to create a Player with a constructor that takes 1 String as an argument, which doesn't exist.

You could either define another constructor that takes one String:

public Player(String firstname)

Or use the one you have already, and pass in 2 Games as well.

public Player(String firstname, Game agame1, Game agame2)

Game lscore1 = new Game ("45");

Game lscore2 = new Game ("27");

Player lizzy = new Player("lizzy", lscore1, lscore2);

System.out.println(lizzy);

System.out.println( + score);

System.out.println( + score);

ErikSilkensena at 2007-7-10 0:44:53 > top of Java-index,Java Essentials,New To Java...
# 7

> ...

>Player lizzy = new Player("lizzy");

>Game lscore = new Game ("45");

>Game lscore = new Game ("27");

> ...

One more thing: you cannot declare two (Game) objects with th same variable name. So do it like this:Game score2 = new Game ("45");

Game score2 = new Game ("27");

and after that create your Player class:Player lizzy = new Player("lizzy"); // <- also provide those two Game objects as paramaters, not only the name

Message was edited by:

prometheuzz

Needed to refresh the page first...

prometheuzza at 2007-7-10 0:44:53 > top of Java-index,Java Essentials,New To Java...
# 8
Many thanks for your help Juliano! I am trying to award you some duke stars but seem to be having trouble. I sent the last lot to other reply and unable to get any to you. Once again thank you.
Camzio45a at 2007-7-10 0:44:53 > top of Java-index,Java Essentials,New To Java...
# 9

> Many thanks for your help Juliano! I am trying to award you some

> duke stars but seem to be having trouble. I sent the last lot to other

> reply and unable to get any to you. Once again thank you.

I just noticed that you gave me five of those thingies. Do you want them

back? I can open up a new topic; you reply to it and I'll return you your

five Dukes. You can do the same for Juliano.

kind regards,

Jos

JosAHa at 2007-7-10 0:44:53 > top of Java-index,Java Essentials,New To Java...
# 10
Thank you very much for your time and advice. Still having a few problems but the information you have given and other replies should enable me to sort this out.
Camzio45a at 2007-7-10 0:44:53 > top of Java-index,Java Essentials,New To Java...
# 11
Hi its fine I appreciate your time and paitience the knowledge you share is very valuable!
Camzio45a at 2007-7-10 0:44:53 > top of Java-index,Java Essentials,New To Java...
# 12
Ok, I keep them; thanks for those Dukes.kind regards,Jos
JosAHa at 2007-7-10 0:44:53 > top of Java-index,Java Essentials,New To Java...
# 13
All this fair and equitable duke swapping simultaneously warms the heart and make me wanna puke. :-)( keith. well done chums.
corlettka at 2007-7-10 0:44:53 > top of Java-index,Java Essentials,New To Java...