• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

source file compilation

 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi there,
when i try to compile following source file, there is a error which states "MovieTestDrive is public, so must be declared in a file MovieTestDrive.java" i didnot understand. i am beginner in java;






thanks
 
Saloon Keeper
Posts: 10732
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If a source file contains more than one class then only one of the classes may be declared 'public' and the name of that class must match the source file name: MovieTestDrive.java.
 
Raj Gurung
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks
 
Ranch Hand
Posts: 99
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Others will probably disagree, but each class should be in its own file with the same name as the class. So if I have a Car class it should go in a file named Car.java; a Book class should go in a file named Book.java, etc.
I can't think of any reason not to do it this way. More than 1 class per file = mess.
 
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Charles D. Ward wrote:Others will probably disagree, but each class should be in its own file with the same name as the class. So if I have a Car class it should go in a file named Car.java; a Book class should go in a file named Book.java, etc.
I can't think of any reason not to do it this way. More than 1 class per file = mess.


I would generally agree with this, the exception being nested classes which have to be in the same file.
 
Raj Gurung
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Carey Brown,
As you posted:
If a source file contains more than one class then only one of the classes may be declared 'public' and the name of that class must match the source file name: MovieTestDrive.java.

But I am able to see Guess game program code in head first java book, which has all 3 classes declared as public.
I am slightly confused.
Could you please explain

Thanks
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Does the book tell you to include all three classes in one source file? I'm sure it doesn't.
 
Raj Gurung
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No it doesn't says.
Ok so it means a single source file can have more multiple classes declared as public but only 1 class declared as public with main() method name must match source file name.
is it correct??
thanks
 
Marshal
Posts: 28226
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Raj Gurung wrote:k so it means a single source file can have more multiple classes declared as public but only 1 class declared as public with main() method name must match source file name.



No, it doesn't have anything to do with whether the class has a main() method or not. (You'll notice that the error message didn't say anything about main() methods.) It means that a source file can contain more than one class, but only one of them can be declared public. And that public class has to have the same name as the file.
 
Raj Gurung
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks but in head first java book, guess game program code has three classes - all declared as public.
Thanks
 
Paul Clapham
Marshal
Posts: 28226
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's very common for applications to have more than one public class. In fact it's extremely common. But they do have to be stored in different classes according to their names. Can you show us the quote from the HFJ book which tells you to put all of those three classes in one file?
 
Charles D. Ward
Ranch Hand
Posts: 99
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just put each class in its own file and the issue will go away.
 
Raj Gurung
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi there,
sorry for late reply.
here is the complete guessgame program.
please could anyone explain what player p1 variable type is, and also what will be source file name as there are 3 public classes.
Thanks in advance

public class Guessgame
{
player p1;
player p2;
player p3;
public void startgame()
{
p1=new Player();
p2=new Player();
p3=new Player();
int guessp1=0;
int guessp2=0;
int guessp3=0;
boolean p1isright=false;
boolean p2isright=false;
boolean p3isright=false;
int targetnumber=(int)(Math.random()*10);
System.out.println("I'm thinking of a number between 0 and 9");
while(true)
{
System.out.println("NUmber to guess is"+targetnumber);
p1.guess();
p2.guess();
p3.guess();
guessp1=p1.number;
System.out.println("player one guessed"+guessp1);
guessp2=p2.number;
Syetem.out.println("player two guessed"+guessp2);
guessp3=p3.number;
System.out.println("player three guessed"+guessp3);
if(guessp1==targetnumber)
{
p1isright=true;
}
if(guessp2==targetnumber)
{
p2isright=true;
}
if(guessp3==targetnumber)
{
p3isright=true;
}
if(p1isright||p2isright||p3isright)
{
System.out.println("we have a winner");
System.out.println("player one got it right"+p1isright);
System.out.println("player two got it right"+p2isright);
System.out.println("player three got it right"+p3isright);
System.out.println("game is over");
}
else
{
System.out.println("players will have to try again");
}
}
}
}

public class Player
{
int number=0;
public void guess()
{
number=(int)(math.random()*10);
System.out.println("I'm guessing"+number);
}
}

public class gamelauncher
{
public static void main(String[] args)
{
Guessgame game=new Guessgame();
game.startgame();
}
}
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You really need to learn to post your code properly. Please read this FAQ. I'd have added them to your post, but it wasn't formatted (i.e. proper indentation). That also needs to be fixed.

Next...

could anyone explain what player p1 variable type is

You have answered your own question. the variable type of p1 is "player".

what will be source file name as there are 3 public classes.


put the code for the Guessgame class in a file named "Guessgame.java"
put the code for the Player class in a file named "Player.java".
and not surprisingly, put the code for the gamelauncher class in a file named "gamelauncher.java"

Also note: gamelauncher is not named correctly, according to java coding standards. Class names should start with a capital letter, so it SHOULD be Gamelauncher. Whichever you use, your file name has to match exactly. gamelauncher should go in gamelauncher.java, and Gamelauncher would go in Gamelauncher.java.
 
Raj Gurung
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So that means, we have variable type player like int, String???

Also,
you mean to say we need to put three public classes in three different source file. Actually, i was thinking we will be putting all three public classes will be in single source file???
 
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Raj Gurung wrote:So that means, we have variable type player like int, String???



That's exactly what it means. When you create a class, you are creating a new Java type. In fact, String is a class, just like any class you might write.

Also, you mean to say we need to put three public classes in three different source file. Actually, i was thinking we will be putting all three public classes will be in single source file???



No, you can't do that. You can only put one public class in a file.
 
Paul Clapham
Marshal
Posts: 28226
95
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Raj Gurung wrote:you mean to say we need to put three public classes in three different source file.



Not only did fred mean to say that, fred did indeed say exactly that.
 
Raj Gurung
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Could i ask 1 thing???

in guessgame code which i posted earlier, what is the significance of declaring Player p1, Player p2 and Player p3 separately from p1=new Player(), p2=new PLayer() and p3=new Player() respectively???

Also, even if they have declared Player p1, Player p2 and Player p3 variable why not they all three have been declared in Player class as they are instance variables???

thanks
 
Knute Snortum
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Raj Gurung wrote:in guessgame code which i posted earlier, what is the significance of declaring Player p1, Player p2 and Player p3 separately from p1=new Player(), p2=new PLayer() and p3=new Player() respectively???



You can declare a variable and create the object at the same time:



Is that what you were asking?
 
Raj Gurung
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am sorry, i was trying to ask: why have Player p1, Player p2 and Player p3 declared separately from p1=new Player(), p2=new PLayer() and p3=new Player() ???


thanks
 
Knute Snortum
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Raj Gurung wrote:Also, even if they have declared Player p1, Player p2 and Player p3 variable why not they all three have been declared in Player class as they are instance variables???



You declare variables where you use them, not in the class.
 
Tony Docherty
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Raj Gurung wrote:in guessgame code which i posted earlier, what is the significance of declaring Player p1, Player p2 and Player p3 separately from p1=new Player(), p2=new PLayer() and p3=new Player() respectively???


They are two different things
1. Player p1 is declaring a variable named p1 as being of type Player.
2. p1=new Player() is creating an object of type Player and assigning it to variable p1

Raj Gurung wrote:Also, even if they have declared Player p1, Player p2 and Player p3 variable why not they all three have been declared in Player class as they are instance variables???


They are not instance variables of the Player class they are instance variables of type Player in the Guessgame class.
If you declared them in the Player class then every time you created a Player object you also would get instance variable p1, p2 and p3 and there is no point in a Player object having references to 3 other Player objects.
 
Knute Snortum
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Raj Gurung wrote:I am sorry, i was trying to ask: why have Player p1, Player p2 and Player p3 declared separately from p1=new Player(), p2=new PLayer() and p3=new Player() ???



When you write



you are declaring the variable. In effect you are saying, I will use a variable called p1 and it will be a type Player. When you write



you are creating a new Player object. These are two separate things, but they can be done on one line of code.
 
Raj Gurung
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
BUt is there any special reason for writing these two statements Player p1; and p1=new Player(); separately?

I am sorry i am new in java and before i always use to see these two statements written together like Player p1=new Player() in other programs.

Thanks
 
Knute Snortum
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Raj Gurung wrote:BUt is there any special reason for writing these two statements Player p1; and p1=new Player(); separately?

I am sorry i am new in java and before i always use to see these two statements written together like Player p1=new Player() in other programs.

Thanks



For a beginner, it might help to see the two statements separately so that they know they are two things happening, but no, there is no special reason. Write them separately or as one statement; it doesn't matter.
 
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is probably better to initialise those fields in the constructor. Declaration and initialisation together imply that you always want that particular value for those fields. That might be valid, but you often want different values. So I prefer to initialise all instance fields in the constructor.

I know there are people who will disagree, and we can have an interesting discussion.
 
Raj Gurung
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi there,
the output of this program is not accurate. It assigns p1isright, p2isright, p3isright as true even if guessing by player is not equivalent to number to guess. I dont know why. I compiled and there is no error.
please could you help.

 
Tony Docherty
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One obvious problem is you don't reset the values of the 3 pXisright variables to false so once they are set to true they will always be true in future games.

I have to say having 3 of everything is a poor way of coding the problem - when you find yourself with this sort of issue think of using an array. That way not only does it make the code cleaner but you can easily change the number of players by changing the size of the array.

Also when setting the value of a boolean rather than having an if statement consider making a straight assignment ie

Note: this will solve your reset problem as well.
 
Raj Gurung
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank You Tony,
But i am still not clear because i am fresher in JAVA and this code is from Head first java book. I was trying to learn it. I am still not clear on why p1isright, p2isright, p3isright are set to true even if guessp1, guessp2, guessp3 is not equivalent to targetnumber. please could you explain once again in an easy way.
In future, after i understand program i will try to use an array.
Thanks in advance.
 
Tony Docherty
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, If you play the game once and p1 guesses correctly p1isright is set to true. The game starts again and this time no player guesses correctly but p1isright is still set to true so the result will be that player 1 has guessed correctly. The next game starts and player 3 guesses correctly p3isright is set to true (and p1isright is still true) so both player1 and 3 are showing as guessing correctly. The next game starts and player 2 guesses correctly p2isright is set to true (and p1isright and p2isright are still true) so all players are now showing as guessing correctly and will continue to do so for every further game.
 
Knute Snortum
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The output isn't correct because you're printing all guesses as correct if only one is correct. Try:

 
Raj Gurung
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thank you very much tony and knute.
 
Raj Gurung
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1 last thing to ask in this command:


what do you mean by:
if (p1isright || p2isright || p3isright)

is this shortcut conditions checking true conditions only???
or it applies for false conditions as well.
thanks
 
Knute Snortum
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It means one or more of the conditions is true, so you could say, If p1 is right or p2 is right or p3 is right, then...
 
Raj Gurung
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
so apart from this program, you mean to say it is only applied in true conditions only??? not in false conditions.

thanks
 
Knute Snortum
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If *all* are false, then the else clause is taken.
 
Raj Gurung
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am sorry, i am talking about generally.
like for example, what will happen in this case:
if(guessp1==targetnumber)
{
p1isright=false;
}
if(guessp2==targetnumber)
{
p2isright=false;
}
if(guessp3==targetnumber)
{
p3isright=false;
}
if(p1isright||p2isright||p3isright)



will if command gets executed if atleast one of p1isright, p2isright, p3isright is false???

thanks
reply
    Bookmark Topic Watch Topic
  • New Topic