Home / Programs / Define a class called ParkingLot with the following description: Instance variables/data members:int 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: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 hour 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.
Programming Example

Define a class called ParkingLot with the following description:

Instance variables/data members:
int 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:
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 hour 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.

👁 110 Views
💻 Practical Program
📘 Step by Step Learning
Study this program carefully to understand the logic, output, and explanation in a structured way.

Information & Algorithm

Given Input:


Expected Output:


Program Code

import java.util.Scanner;

public class ParkingLot
{
    private int vno;
    private int hours;
    private double bill;
    
    public void input() {
        Scanner in = new Scanner(System.in);
        System.out.print("Enter vehicle number: ");
        vno = in.nextInt();
        System.out.print("Enter hours: ");
        hours = in.nextInt();
    }
    
    public void calculate() {
        if (hours <= 1)
            bill = 3;
        else
            bill = 3 + (hours - 1) * 1.5;
    }
    
    public void display() {
        System.out.println("Vehicle number: " + vno);
        System.out.println("Hours: " + hours);
        System.out.println("Bill: " + bill);
    }
    
    public static void main(String args[]) {
        ParkingLot obj = new ParkingLot();
        obj.input();
        obj.calculate();
        obj.display();
    }
}

How to learn from this program

First read the algorithm, then study the program code line by line. After that, compare the code with the output and finally go through the explanation. This approach helps learners understand both the logic and the implementation properly.