Results 1 to 10 of 10
Like Tree2Likes
  • 2 Post By pbrockway2

Thread: Which one is better?

  1. #1
    ankiit is offline Member
    Join Date
    Dec 2011
    Location
    India
    Posts
    74
    Rep Power
    0

    Default Which one is better?

    Hello Mentors,

    I was going through a code, the code 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);
    
    
    	}
    
    }
    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:
    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);
    }
    }
    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.

    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 Gupta
    Last edited by pbrockway2; 12-23-2011 at 06:49 AM. Reason: code tags added

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default 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

    Java Code:
    Box myBox = new Box();
    myBox.width = -1.5;
    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.

    If you use a so-called setter method you can stop this:

    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;
        }
    }
    The code is longer - but safer! (When you get to them you will see that you can also use exceptions.)

    -----

    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.
    ankiit and karthikvadapalli like this.

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default 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.

  4. #4
    ankiit is offline Member
    Join Date
    Dec 2011
    Location
    India
    Posts
    74
    Rep Power
    0

    Default 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

  5. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default Re: Which one is better?

    You're welcome.

  6. #6
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,185
    Rep Power
    20

    Default 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?

  7. #7
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default Re: Which one is better?

    Oh yes, I didn't spot that!

    @OP getVol() should take no arguments - like in your other thread.

  8. #8
    ankiit is offline Member
    Join Date
    Dec 2011
    Location
    India
    Posts
    74
    Rep Power
    0

    Default 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

  9. #9
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,185
    Rep Power
    20

    Default 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.

  10. #10
    ankiit is offline Member
    Join Date
    Dec 2011
    Location
    India
    Posts
    74
    Rep Power
    0

    Default Re: Which one is better?

    Hi Toll,

    Thanks a lot for the information.

    I got your point, will be taking care of such details for further programming. :)

    Thanks
    Ankit

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •