Page 302 - CA_Blue( J )_Class10
P. 302

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

                18      }
                19      public static void main() {
                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  }
              You will get the following output:










              12.2.3 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. For example:


                 1  class rectangle
                 2  {
                 3      int length,breadth,area, perimeter;

                 4      rectangle()

                 5      {
                 6          length=5;
                 7          breadth=2;

                 8      }
                 9      void cal()

                10      {
                11          area = length * breadth;

                12          perimeter = 2*(length+breadth);
                13      }


                300300  Touchpad Computer Applications-X
   297   298   299   300   301   302   303   304   305   306   307