• Post Reply Bookmark Topic Watch Topic
  • New Topic

Major problem with GuessGame code, ch02 of "Head First Java, 2nd edition, covers Java 5.0"  RSS feed

 
Dj Driver
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I typed the code on pg. 39 - 40 as is in the book. I have the 'class Player' and 'class GameLauncher' from pg 40 at the end of the 'class GuessGame' code from pg. 39. All the code is in one file. When I 'javac GuessGame.java' I get 2 error messages -
"GuessGame.java:61: class Player is public, should be declared in a file named Player.java
public class Player {" (omit both "s but there is a carot under the c in class)
"GuessGame.java:70: class Gamelauncher is public, should be declared in a file named GameLauncher.java
public class GameLauncher { " (omit both "s but there is a carot under the c in class)

When I viewed the downloaded code for the book the author has 3 java files - one for GuessGame, one for Player and another for GameLauncher (along with a class file for each of the three). I am really stumped on how to get this to work.

The author does not show pg. 39 when you view the book online (very sad).

Not sure if it is allowed but I am including the code I wrote as I saw it in the book:

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;
System.out.println("Player one guessed " + guessp2);

guessp3 = p3.number;
System.out.println("Player one 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!");
break;
} else {
System.out.println("Players will have to try again.");
}
}
}
}
public class Player {
int number = 0; // where the guess goes

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();
}
}
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dj Driver wrote:
"GuessGame.java:61: class Player is public, should be declared in a file named Player.java


That seems pretty straightforward. What part didn't you understand?

public class Player {" (omit both "s but there is a carot under the c in class)


It's "caret". And if you used code or tt tags, you could have posted in the whole thing, including the caret, and it would have lined up.
 
Dj Driver
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am using Notepad++ to write the code and I am executing it from the command line.

Are you saying that I need 3 files for this? If so how does one do the 'javac ???' or the 'java ???' from the command line?

Just trying to follow the example from the book and not having any luck.
 
Dj Driver
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have found that the author has as his first line of code in the 3 files:

package chap02;

and the files are in the chap02 directory. Any significance?
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if all the files are in the same directory, you can do:

javac *.java

or you can compile them one at a time:

javac a.java
javac b.java
javac c.java

then, when you want to run it, you give it the name of the class who's main method you want to run. Since your GameLauncher class has a main, you'd run it with

java GameLauncher
 
James Boswell
Bartender
Posts: 1051
5
Chrome Eclipse IDE Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Sounds like you have defined more than one public class in the same java file. Not allowed. See here.
 
Dj Driver
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was getting error messages by doing the following:

javac GuessGame.java

java GuessGame


I removed the word Public from 'public class Player' and from 'public class GameLauncher'.

Then from the command line 'javac GuessGame.java'

Then from the command line 'java GameLauncher'

It works with no errors.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!