Question:
Define a class 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 vno and hours
void calculate() – To compute the parking charge at the rate of Rs.3 for the first hour or part thereof, and Rs.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
Answer:
import java.util.Scanner;
public class ParkingLot {
private int vno;
private int hours;
private double bill;
public void input() {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter vehicle number: ");
vno = scanner.nextInt();
System.out.print("Enter hours: ");
hours = scanner.nextInt();
}
public void calculate() {
bill = 3 + (hours - 1) * 1.50;
}
public void display() {
System.out.println("Vehicle number: " + vno);
System.out.println("Hours: " + hours);
System.out.println("Bill: Rs. " + bill);
}
public static void main(String[] args) {
ParkingLot parkingLot = new ParkingLot();
parkingLot.input();
parkingLot.calculate();
parkingLot.display();
}
}
