Passing object as arguments.

Posted by Stephen thangaraj at 19:01

class rectangle
{
        private int len,bre;
        public void setrect(int l,int b)
        {
                len = l;
                bre = b;
        }
        public int totlen(rectangle t)
        {
                return len + t.len;
        }
        public int totbre(rectangle t)
        {
                return bre + t.bre;
        }
        rectangle combine(rectangle t)
        {
                rectangle temp = new rectangle();
                temp.len = len + t.len;
                temp.bre = bre + t.bre;
                return temp;
        }
        public void disp()
        {
                System.out.println("Length : " + len + "\t Breadth : " + bre);
        }
        public static void main(String args[])
        {
                rectangle r1 = new rectangle();
                rectangle r2 = new rectangle();
                rectangle r3 = new rectangle();
                r1.setrect(2,3);
                r2.setrect(4,5);
                int tlen = r1.totlen(r2);
                int tbre = r1.totbre(r2);
                r3 = r1.combine(r2);
                System.out.println("Total length : " + tlen);
                System.out.println("Total Breadth : " + tbre);
                r1.disp();
                r2.disp();
                r3.disp();
        }
}


If you enjoyed this post and wish to be informed whenever a new post is published, then make sure you subscribe to my regular Email Updates. Subscribe Now!


Kindly Bookmark and Share it:

YOUR ADSENSE CODE GOES HERE

0 comments:

Have any question? Feel Free To Post Below:

 

© 2011. All Rights Reserved | Interview Questions | Template by Blogger Widgets

Home | About | Top