• Post Reply Bookmark Topic Watch Topic
  • New Topic

Naming an Object  RSS feed

 
Shaman Dasuta
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Everyone,

Inspired by this awesome campfire story I set out to make these sets of classes:
http://www.javaranch.com/campfire/StoryPoly.jsp

Animal.java

package inheritance;



Dog.java



Zoo,java



Output:



Desired Output:



Questions:

How can I name a dog ONCE. So I want to name Fido ONCE in this code. Also I know I can say: Fido.speak("Fido");
But that messes up the array. Also seems illogical.

Why Array?
I have several others Cow, Bird, etc. I want a generic print out and then a name set to the object when I desire a name to be added.
 
Knute Snortum
Sheriff
Posts: 4276
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try using an instance variable called name in Animal.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66306
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The whole point of object is not only that they can have methods, but they can also store state. When you construct a dog named Fido, you can store this name in an instance variable and it will be retained for the life of that instance.

By the way, please follow accepted naming conventions. Variables begin with a lowercase letter, so animals, not Animals.

[Edit: Knute snuck in while I was posting. ]
 
Shaman Dasuta
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So I tried to change Animal but its giving errors now:



Also I commented out the speak function in Dog.
 
Knute Snortum
Sheriff
Posts: 4276
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What errors and when? (running or compiling). TellTheDetails (← that's a link)
 
Shaman Dasuta
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Sorry about that. Here it is.

The code I ran in Dog:

 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66306
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have two constructors: one that assigns a name, and one that does not. So what happens in the code for your speak() method when a dog has no name?
 
Knute Snortum
Sheriff
Posts: 4276
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And just a side note to Bear's comment: when a String variable is not initialized it has a value of null. null is not equal to the empty string! We sometimes say, "The string is null" when we mean , "The string is empty."
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:And just a side note to Bear's comment: when a String variable is not initialized it has a value of null. null is not equal to the empty string! We sometimes say, "The string is null" when we mean , "The string is empty."

To elaborate a bit...

A String that is not initialized is like saying "I have a label for a box, but I don't have a box".

A String that is empty is like like saying "I have a label for a box, and I have the box, but the box is empty"

A String that is initialized to something is like saying "I have a label for a box, I have the box, and there is something in the box."
 
Shaman Dasuta
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So the key things I took from this is to:
declare String variable "name"
initialize it
Have it be Accessible by all the constructors in Animal.java
Use inbuilt inheritance to make it work.

Ladies and Gentleman here is Dog.java!


Otherwise here is the Animal.java Parent Class.



Zoo.java (Main Class)



Also in response to Bear's Question:

I added the if else statements if a dog is not named.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!