• Post Reply Bookmark Topic Watch Topic
  • New Topic

personalized bark for each of my dogs  RSS feed

 
jon ninpoja
Ranch Hand
Posts: 291
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
in my doggy program..








how can i call the bark method and have it pertain to each of my objects?

so if i went

ralf.bark();

it said..."ralf barks"
is it just passed into the function via a string...or is there a smarter way of doing it?

thanks
 
Carey Brown
Saloon Keeper
Posts: 3329
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I suggest modifying your constructor to look like this
and remove nameDog(). When you construct a new Dog you should know at that time what the name is and whether or not it barks. You don't need nameDog  because a Dog's name won't change once you construct it. Your bark() method's behavior should then behave based on the "barks" boolean that you stored.
 
salvin francis
Bartender
Posts: 1664
37
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jon,

Its simple, your method bark() has access to all the field variables in your class. You can simply modify the code to say


However, it would be interesting if you don't name your dog it would print "null" on the screen.
 
jon ninpoja
Ranch Hand
Posts: 291
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks guys yes i got it sorted
was trying with the object name then did what you suggested

will look at constructors...

 
salvin francis
Bartender
Posts: 1664
37
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Glad to know that
As carey suggested, you can use constructors to construct your object.



This will ensure that you cannot create a dog object without passing a name to it.
 
Carey Brown
Saloon Keeper
Posts: 3329
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:I suggest modifying your constructor to look like this
and remove nameDog(). When you construct a new Dog you should know at that time what the name is and whether or not it barks. You don't need nameDog  because a Dog's name won't change once you construct it. Your bark() method's behavior should then behave based on the "barks" boolean that you stored.

And building on this, you might be able to replace your bark() method with a toString() method like this
This then simplifies your main() method to support


 
salvin francis
Bartender
Posts: 1664
37
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:
This then simplifies your main() method to support



I would suggest not to use toString() for such purposes, the use of that method is to merely log the contents of a class and not the goal of the program.
toString() is a part of the Object class and is generally overridden only to provide debug data.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!