Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • 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
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

Moving from A of beginning to B of beginning  RSS feed

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey folks..

You helped me install and run my sample codes yesterday.

As all of you suggested, am writing programs in notepad and executing them.

Now I would like to know....I have two classes Dog and DogTest...Codes for both of them are given below...

And the output am getting is bow bow...

My question : I want to print the size which am assigning 40 in DogTest...

One way of doing it is...by using S.O.P(size); inside the bark method...second way of doing is by writing a seperate method in Dog...

I want to know are there any more ways...I can print the size ?

 
Marshal
Posts: 64494
225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please always use the Code button; your code is difficult to read otherwise.

You ought to give that Dog class a constructor, to ensure the size and breed fields are set up when the objects are created. Don't use d.size = 40; the size will be set in the constructor.
Then you can give it getSize() and getBreed() fields.

Look at this method. You should give your Dog class a method with exactly the same heading and make it return something like

"A Rottweiler dog, size 99"

Try this
 
Sangeetha Komarraju
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply but am not clear...

Constructors they are topics i havent touched yet....

my question is simple...

when I can say...

num = 10;

S.O.P(num);

Cant I display the size same way ? with reference to the example I have given above...

If yes then how...If no(i know its no) what is the reason behind it ?
 
Campbell Ritchie
Marshal
Posts: 64494
225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, of course you can show the size that way, but it is better design to have "get" methods (and sometimes "set" methods) to allow access to the fields. And fields should usually be labelled with the "private" modifier keyword.
 
Campbell Ritchie
Marshal
Posts: 64494
225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should find information about constructors in any book, or the Java™ Tutorials.
 
Sangeetha Komarraju
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

When I do directly...S.O.P(size); in DogTest its giving me a compilation error....

getSize() and setSize() are gud idea..

I want to know...why am I getting compilation error exactly
 
Ranch Hand
Posts: 710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Sangeetha Komarraju wrote:
When I do directly...S.O.P(size); in DogTest its giving me a compilation error....

getSize() and setSize() are gud idea..

I want to know...why am I getting compilation error exactly



Can you post the error here so we can see it?
 
Sangeetha Komarraju
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hey guys...am able to display the size value now..

I just did a blunder...S.O.P(size); I should have given..S.O.P(d.size)

I know I know its a damn blunder...now all of you dont hit me

Thanks guys for your time
 
W. Joe Smith
Ranch Hand
Posts: 710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Sangeetha Komarraju wrote:
Hey guys...am able to display the size value now..

I just did a blunder...S.O.P(size); I should have given..S.O.P(d.size)

I know I know its a damn blunder...now all of you dont hit me

Thanks guys for your time



Ah, the infamous typo. It is the bane of all programmers' existence.
 
Well don't expect me to do the dishes! This ad has been cleaned for your convenience:
how do I do my own kindle-like thing - without amazon
https://coderanch.com/t/711421/engineering/kindle-amazon
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!