aspose file tools*
The moose likes Beginning Java and the fly likes Moving from A of beginning to B of beginning Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Moving from A of beginning to B of beginning" Watch "Moving from A of beginning to B of beginning" New topic
Author

Moving from A of beginning to B of beginning

Sangeetha Komarraju
Greenhorn

Joined: Dec 03, 2009
Posts: 7
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

Joined: Oct 13, 2005
Posts: 40052
    
  28
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

Joined: Dec 03, 2009
Posts: 7
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

Joined: Oct 13, 2005
Posts: 40052
    
  28
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

Joined: Oct 13, 2005
Posts: 40052
    
  28
You should find information about constructors in any book, or the Java™ Tutorials.
Sangeetha Komarraju
Greenhorn

Joined: Dec 03, 2009
Posts: 7

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

Joined: Feb 10, 2009
Posts: 710
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?


SCJA
When I die, I want people to look at me and say "Yeah, he might have been crazy, but that was one zarkin frood that knew where his towel was."
Sangeetha Komarraju
Greenhorn

Joined: Dec 03, 2009
Posts: 7

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

Joined: Feb 10, 2009
Posts: 710
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Moving from A of beginning to B of beginning