Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Moving from A of beginning to B of beginning

 
Sangeetha Komarraju
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 ?

 
Campbell Ritchie
Sheriff
Pie
Posts: 48967
60
  • 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
Sheriff
Pie
Posts: 48967
60
  • 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
Sheriff
Pie
Posts: 48967
60
  • 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
 
W. Joe Smith
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic