Page 121 - 2617_JSSPS_C-7
P. 121

For example:

                 1  import java.util.*;
                 2  class rectangle1

                 3  {
                 4      int length,breadth,area, perimeter;

                 5      rectangle1(int l, int b)
                 6      {

                 7         length=l;
                 8         breadth=b;

                 9      }
                 10      void cal()

                 11      {
                 12         area = length * breadth;
                 13          perimeter = 2*(length+breadth);

                 14      }
                 15      void display ()

                 16      {    System.out.println("Area of Rectangle:" + area);
                 17        System.out.println("Perimeter of Rectangle:" + perimeter);

                 18      }
                 19      public static void main(String[] args) {

                 20         Scanner sc= new Scanner(System.in);
                 21          int l1,b1;

                 22          System.out.println("Enter length and breadth");
                 23          l1=sc.nextInt();

                 24          b1=sc.nextInt();
                 25          rectangle1 ob = new rectangle1(l1,b1);
                 26         ob.cal();

                 27         ob.display();
                 28      }

                 29  }
                 Non-parameterised Constructor


                 A  non-parameterised constructor is a type  of constructor without  parameters.  However, it has
                 statements inside its body. It is also known as no-argument constructor. This type of constructor has
                 the same signature as that of the default constructor, but the difference is that it can have any values
                 to initialize the instance variables.





                                                                                               Java Programming    119
   116   117   118   119   120   121   122   123   124   125   126