• 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
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

Naming an Object

 
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.
 
Sheriff
Posts: 6029
157
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try using an instance variable called name in Animal.
 
Sheriff
Posts: 67266
170
Mac Mac OS X IntelliJ IDE jQuery Java
  • 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: 6029
157
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • 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
Sheriff
Posts: 67266
170
Mac Mac OS X IntelliJ IDE jQuery Java
  • 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: 6029
157
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • 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."
 
lowercase baba
Posts: 12751
51
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.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!