Page 265 - ComputerScience_Class_11
P. 265

20              if (hours <= 1)

                  21                  bill = 3;
                  22              else
                  23                  bill = 3 + Math.ceil(hours - 1) * 1.5;
                  24          }
                  25          void display() {

                  26              System.out.println("Vehicle number: " + vno);
                  27              System.out.println("Hours: " + hours);
                  28              System.out.println("Bill: " + bill);
                  29          }
                  30          public static void main(String args[]) {

                  31              ParkingLot obj = new ParkingLot();
                  32              obj.input();
                  33              obj.calculate();
                  34              obj.display();

                  35          }
                  36      }

                 The output of the preceding program is as follows:
                       BlueJ: Terminal Window - Java

                   Options

                  Enter vehicle number: WB24A1234
                  Enter hours: 23
                  Vehicle number: WB24A1234
                  Hours: 23
                  Bill: 36.0



                 9.12.1 Features of the Constructor
                 The features of the constructor are as follows:
                 •  The name of the class is the same as the name of the constructor.
                 •  As soon as the object is declared, the constructor is automatically called.
                 •  The constructor does not return a value, so it does not have a return type.

                 •  The constructor is always public.
                 •  The constructor is automatically overloaded.
                 •  The constructor is used to initialise the data members.

                 9.12.2 Different Types of Constructors
                 There are four types of constructors. They are discussed as follows:
                 1. Default constructor: This constructor is created automatically and used to initialise all the data members with default
                   values, such as int variable will be initialised with 0, String variables will be initialised with “ ” (null) and so on.





                                                                                           Methods and Constructors  263
   260   261   262   263   264   265   266   267   268   269   270