Page 123 - 2617_JSSPS_C-7
P. 123

For example:

                 1  class rectangle2
                 2  {

                 3      int length, breadth, area, perimeter;
                 4      rectangle2 (int l, int b)
                 5      {

                 6         length=l;
                 7         breadth=b;
                 8      }

                 9      rectangle2 (rectangle2 r)
                 10      {
                 11         length = r.length;

                 12           breadth=r.breadth;
                 13      }

                 14      void cal()
                 15      {
                 16         area = length * breadth;

                 17         perimeter = 2*(length+breadth);
                 18      }
                 19

                 20      void display ()
                 21      {
                 22          System.out.println("Area of Rectangle:" + area);

                 23          System.out.println("Perimeter of Rectangle:" + perimeter);
                 24      }

                 25      public static void main(String[] args)
                 26      {
                 27          rectangle2 ob1 = new rectangle2 (5,4);
                 28

                 29         rectangle2 ob2 = new rectangle2(ob1);
                 30         ob1.cal ();

                 31         ob1.display();
                 32         ob2.cal ();
                 33         ob2.display();

                 34      }
                 35  }






                                                                                               Java Programming    121
   118   119   120   121   122   123   124   125   126   127   128