• Post Reply Bookmark Topic Watch Topic
  • New Topic

Guessing Game problem page39 Head First Java  RSS feed

 
Greenhorn
Posts: 4
Java Mac OS X Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I keyed in the Guessing Game problem on pages 39&40 of Head First Jave (2nd Ed.) There are three classes. GuessGame, Player, and GameLauncher . The GameLauncher class has the main() method in it.

Each class is defined with the word "public" in front of the word "class".
When I tried to compile the program I kept getting the following errors:

GuessGame.java:1: class Player is public, should be declared in a file named Player.java
public class Player{
^
GuessGame.java:20: class GameLauncher is public, should be declared in a file named GameLauncher.java
public class GameLauncher{

Now here is the interesting part. When I deleted the word public in front of the word "class" in each class definition, and compiled the GuessGame.java file, the program compiles with no errors and when I type java GameLauncher in Terminal , the program runs fine and gives an output similar to what is shown on page 40.

So is this a misprint on page 39?
 
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If all classes are public, they should be saved in their own respective file. So you should have Player.java, GameLauncher.java and GuessGame.java.
 
Ranch Hand
Posts: 441
IntelliJ IDE Scala Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or just leave the "public" off the Player and GuessGame class declarations and save in the same file. It won't make any difference unless you're trying to access these classes from another class file.
 
Marshal
Posts: 56600
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By omitting public, you are making the classes "package-private" (officially called default access). That means they are inaccessible from outside their own package. At this stage you won't notice any difference.
 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I ran into the same problem. Nowhere in the text does it tell you to save each public class thing (i.e. GuessGame, Player, GameLauncher) as seperate files (i.e. GuessGame.java, Player.java, GameLauncher.java). When you go to compile, only compile GameLauncher. Then run GameLauncher. It worked fine, provided you typed everything exactly as it was written in the book. Or as mentioned above, you can omit the public part from GuessGame and Player and compile and run the whole thing as one single entity.
 
Luigi Plinge
Ranch Hand
Posts: 441
IntelliJ IDE Scala Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can see how it might be confusing... the book doesn't really tell you too much about the mechanics of setting up JDK and compiling and running programs.

You might find it useful to get hold of an IDE like NetBeans or Eclipse (the two most popular free ones). These will flag up any errors before you get to the compiling stage, and give you a helpful explanation of why it's an error (having 3 public classes in 1 file, for example).

I first tried Java in 2001 when we used Microsoft Visual J++, and I remember the nightmares of compiler errors and trying to locate the missing semicolon or brace or accidental lowercase letter. Fortunately, IDEs have moved on immensely. They don't do all the hard work for you, but they know the rules and will alert you when you transgress, so they're brilliant for someone learning the language.
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Disagree: you are better off at the beginner's stage not using an IDE, but getting used to seeing compiler errors at the command line.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!