Win a copy of Transfer Learning for Natural Language Processing (MEAP) this week in the Artificial Intelligence and Machine Learning forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Paul Clapham
  • Devaka Cooray
  • Bear Bibeault
Sheriffs:
  • Junilu Lacar
  • Knute Snortum
  • Liutauras Vilda
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Piet Souris
Bartenders:
  • salvin francis
  • Carey Brown
  • Frits Walraven

Guessing Game problem page39 Head First Java

 
Greenhorn
Posts: 4
Mac OS X Netbeans IDE Java
  • 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 VI Editor Ubuntu
  • 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
Scala IntelliJ IDE 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: 68905
275
  • 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
Scala IntelliJ IDE 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: 68905
275
  • 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.
 
Ranch Hand
Posts: 528
10
Android Open BSD Slackware
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote: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.


I do not get your 9 years ago comment, the IDE show errors in the stacktrace at runtime and also  compile time(that are really useful for beginner to start to form a general map of what can go wrong)
please what do you mean?I remember you are a fan of minimal editor as VIM and Emacs if I am not wrong. And they are amazing indeed, but do not have the features of most advanced IDE, anyway I do not want to go off topic
 
Campbell Ritchie
Marshal
Posts: 68905
275
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Giovanni Montano wrote:. . . you are a fan of minimal editor as VIM and Emacs . . .

No, I hardly ever use vi, vim or Emacs. The only “minimal” editor I know about is MS NotePad, which I think is useless for programming.

Maybe I put it badly nine years ago. Also some people have persuaded me that IDEs can be used earlier than I used to think. But many beginners find the learning curve of an IDE too steep. It is also a good idea to learn to speciffy options at the command line. Once somebody is confident about setting a CLASSPATH at the terminal, they should be happy with an IDE.
 
Giovanni Montano
Ranch Hand
Posts: 528
10
Android Open BSD Slackware
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
eheh notepad is maybe my favorite windows software, just get things done, and can wrap the text, but now I am tile window manager fan cannot live without kind of i3 window managers. I see your point sometimes IDE are difficult to cope with indeed. And nine years ago intelliJ was not so good as it is today, using (I guess) artificial intelligence, reflection to suggest alternatives and catch compile time as per today that there is even a codota extension that furnish snippet of code while typing based on statistical relevance.
 
Yes, my master! Here is the tiny ad you asked for:
Two software engineers solve most of the world's problems in one K&R sized book
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic