Results 1 to 10 of 10
Thread: Which one is better?
- 12-23-2011, 05:04 AM #1
Member
- Join Date
- Dec 2011
- Location
- India
- Posts
- 74
- Rep Power
- 0
Which one is better?
Hello Mentors,
I was going through a code, the code is as follows:-
The book from which I am studying Java, specifies a different way of performing the same work as the above code, and the code from the book is as follows:-Java Code:/* BoxDemo1 application demonstrates the usage of a simple function with a return type, function name = getVol */ class Box { double width; double height; double depth; //Method declaration double getVol(double w, double h, double d) { return w*h*d; } } class BoxDemo2 { public static void main(String[] args) { Box mbox1 = new Box(); Box mbox2= new Box(); double vol; /* Assigning values to the instance variable*/ /* mbox1.width=10; mbox1.height=20; mbox1.depth=15; mbox2.width=3; mbox2.height=6; mbox2.depth=10; */ /* Calculating volumes*/ vol=mbox1.getVol(10,20,15); System.out.println("the volume of box1 is:=" + vol); vol=mbox2.getVol(3,6,10); System.out.println("the volume of box1 is:=" + vol); //System.out.println("Sum of the two volumes is:=" + vol1+vol2); } }
I want to know, why is there a need to create an extra function, "setDimensions(<parameter>)" here, if the work can be done with one function only.Java Code:class Box { double width; double height; double depth; // compute and return volume double volume() { return width * height * depth; } // sets dimensions of box void setDim(double w, double h, double d) { width = w; height = h; depth = d; } } class BoxDemo5 { public static void main(String args[]) { Box mybox1 = new Box(); Box mybox2 = new Box(); double vol; // initialize each box mybox1.setDim(10, 20, 15); mybox2.setDim(3, 6, 9); // get volume of first box vol = mybox1.volume(); System.out.println("Volume is " + vol); // get volume of second box vol = mybox2.volume(); System.out.println("Volume is " + vol); } }
Though I feel that it is done only for better understanding of the example, in case there are other reasons request you to please share them.
Thanks in Advance
Ankit GuptaLast edited by pbrockway2; 12-23-2011 at 05:49 AM. Reason: code tags added
- 12-23-2011, 05:48 AM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,561
- Rep Power
- 11
Re: Which one is better?
It is very often better to use a method like setDimensions() rather than allow the instance variables to be assigned to directly. That helps "protect" your Box class.
Suppose, for example, that someone using your Box class said
That would be a crazy value, but you couldn't stop them. It might not matter in this case, but you often write classes where silly values for the instance variables would make the whole class fail to behave correctly.Java Code:Box myBox = new Box(); myBox.width = -1.5;
If you use a so-called setter method you can stop this:
The code is longer - but safer! (When you get to them you will see that you can also use exceptions.)Java Code:/** * Sets the dimensions of the box to given values. If the values are not greater * than zero that dimension will be set to one. */ void setDim(double w, double h, double d) { if(w > 0) { width = w; } else { width = 1.0; } if(h > 0) { height = h; } else { height = 1.0; } if(d > 0) { depth = d; } else { depth = 1.0; } }
-----
Have you read about access modifiers? (public/private) Often you will see only a few methods marked "public" and the instance variables and other methods marked "private" to stop anyone outside the class from being able to use them directly. Don't worry if you haven't come across them yet, but they are getting at the same thing: keeping most of the class secret and letting the outside world have access only to a few well defined methods.
- 12-23-2011, 05:52 AM #3
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,561
- Rep Power
- 11
Re: Which one is better?
Also when you post code use the "code" tags. You put [code] at the start of the code and [/code] at the end. That way the code will be formatted when it appears here. As you can see if you post code that has been cut and pasted from somewhere else you may have to correct the formatting.
- 12-23-2011, 09:10 AM #4
Member
- Join Date
- Dec 2011
- Location
- India
- Posts
- 74
- Rep Power
- 0
Re: Which one is better?
Hi pbrockway2,
Got your point, and I have made the changes suggested by you, the code looks good now. :)
Yes, I have read about the access modifiers in an introductory chapter.
I'll keep the formatting in mind from next time.
Thanks
Ankit Gupta
- 12-23-2011, 09:25 AM #5
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,561
- Rep Power
- 11
Re: Which one is better?
You're welcome.
- 12-23-2011, 10:05 AM #6
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
Re: Which one is better?
Um, that first code for getVol in the OP doesn't use anything inside the Box objects...so what's the point of the attributes?
- 12-23-2011, 08:24 PM #7
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,561
- Rep Power
- 11
Re: Which one is better?
Oh yes, I didn't spot that!
@OP getVol() should take no arguments - like in your other thread.
- 12-27-2011, 02:54 AM #8
Member
- Join Date
- Dec 2011
- Location
- India
- Posts
- 74
- Rep Power
- 0
Re: Which one is better?
Hi Tolls,
I didn't got your point?
If I am correct, than I suppose you meant that the instance variables were not utilized(not initialized), so I must use width, depth and height variables instead of w,d & h.
Request you to kindly correct me, if I am wrong.
Thanks
Ankit
- 12-28-2011, 01:17 PM #9
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
Re: Which one is better?
Your Box has attributes.
You have a getVol method that presumably should be returning the volume of the Box object, so therefore should use those attributes and not have any parameters.
- 12-29-2011, 04:34 AM #10
Member
- Join Date
- Dec 2011
- Location
- India
- Posts
- 74
- Rep Power
- 0


2Likes
LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks