• Post Reply Bookmark Topic Watch Topic
  • New Topic

Head First Java " says Ruff!"  RSS feed

 
Lee Reader
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I have tried to write copy this code and I don't get the correct end result.
So I tried to copy and paste someone else's code that is coming up with the correct end result but this is still not helping me.

This is the code I am using:



This is the end result:

Ruff! Ruff!
Last Dogs name is Bart.
Ruff! Ruff!
Ruff! Ruff!
Ruff! Ruff!

So I am not getting the names to show up.

Only thing I can think of is that I am using command prompt to start the java.class but I'm not sure if that would be an issue.

Thanks for the help.
 
Junilu Lacar
Sheriff
Posts: 11486
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
IGNORE -- I didn't try it but the program looks correct, except that you need to indent those statements properly to make it more readable. Make sure you recompile Dog2, i.e. run javac Dog2.java again. Also, make sure you are running with the command java Dog2. If that still doesn't help, delete all Dog2.class files that you can find, recompile and run again.

See below.
 
Junilu Lacar
Sheriff
Posts: 11486
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
BTW, this code

is more idiomatically written as one of the following:

 
Junilu Lacar
Sheriff
Posts: 11486
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I see the problem now. The class Dog2 actually uses Dog objects, not Dog2 objects. Since you have a Dog.java file on your system, Dog2.java compiles fine. I don't have Dog.java on my system so I got a compile-time error.

The cut-and-paste monster got you. In Dog2.java, you need to change your references to Dog2 instead of Dog.

 
Junilu Lacar
Sheriff
Posts: 11486
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lee Reader wrote:I have tried to write copy this code and I don't get the correct end result.

So here's a valuable lesson: You actually DID get the correct end results based on what you were telling the computer to do. You just weren't saying what you thought you were saying.

"He said true things, but called them by wrong names." — Elizabeth Barrett Browning

Inigo Montoya: You keep using that word. I do not think it means what you think it means.

Old chinese joke: Young man learning Chinese tries to ask pretty waitress "How much for one bowl?" Waitress slaps him and storms off. His friend explains, "You asked her 'How much for one night?'"
 
Lee Reader
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh i see what you mean.

Ill have to try it out when i get home from work

Thanks for your help and the joke as well.
 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:. . . "He said true things, but called them by wrong names." . . .
Lewis Carroll in Alice Through the Looking‑Glass wrote:“When I use a word,” Humpty Dumpty said, in rather a scornful tone, “it means just what I choose it to mean—neither more nor less.”


And welcome to the Ranch
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem with computers is that they do exactly what you tell them to do instead of what you intended them to do. On the other hand, that's a good thing, I think a computer that tries to guess what you intended and then does that would quickly drive you crazy.

A programmer's joke:

A programmer is going to the grocery store and his wife tells him, "Buy a gallon of milk, and if there are eggs, buy a dozen." So the programmer goes, buys everything, and drives back to his house. Upon arrival, his wife angrily asks him, "Why did you get 13 gallons of milk?" The programmer says, "There were eggs!"

 
Lee Reader
Greenhorn
Posts: 3
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for all the help.

So I changed the code:



and I got the results I was looking for:


image free hosting

Rufff.png
[Thumbnail for Rufff.png]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!