Page 264 - ComputerScience_Class_11
P. 264

Syntax:
                  class [name_of_class]
                  {
                      //data members
                      // A constructor
                      [name_of_class]()
                      {
                          //Statements;
                      }
                      //methods
                  }
              Let us see the following programs.


                Program 11    Define a class named ParkingLot with the following description:
                              Instance variables/Data members
                              String vno        :    To store the vehicle number
                              int hours         :    To store the number of hours the vehicle is parked in the parking lot
                              double bill       :   To store the bill amount
                              Member Methods
                              ParkingLot()      :    Non-parameterised Constructor
                              void input( )     :    To input and store the vno and hours
                              void calculate( )   :      To compute the parking charge at the rate of `3 for the first hours or
                                                    part thereof and `1.50 for each additional hour or part thereof.
                              void display ( )    :    To display the detail
                              Write a main method to create an object of the class and call the above methods.
                1       import java.util.*;
                2       class ParkingLot {

                3           String vno;
                4           int hours;
                5           double bill;
                6           ParkingLot()
                7           {

                8                 hours=0;
                9                 bill=0.0;
                10           }
                11           void input()
                12          {

                13              Scanner sc = new Scanner(System.in);
                14              System.out.print("Enter vehicle number: ");
                15              vno = sc.next();
                16              System.out.print("Enter hours: ");

                17              hours = sc.nextInt();
                18          }
                19          void calculate() {




                  262  Touchpad Computer Science (Ver. 3.0)-XI
   259   260   261   262   263   264   265   266   267   268   269