Page 213 - computer science (868) class 11
P. 213

17              hours = sc.nextInt();
                  18          }

                  19          void calculate() {
                  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:
                   Enter vehicle number: WB24A1234
                   Enter hours: 23
                   Vehicle number: WB24A1234
                   Hours: 23
                   Bill: 36.0

                 8.12.1 Features of the Constructor
                 The following are the features of the constructor:
                 •  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.
                 8.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.



                                                                                                                       211
                                                                                              Methods and Constructors  211
   208   209   210   211   212   213   214   215   216   217   218