Computer Applications

Define a class called ParkingLot with the following description:

Class name : ParkingLot

Data MembersPurpose
int vnoTo store the vehicle number
int hoursTo store the number of hours the vehicle is parked in the parking lot
double billTo store the bill amount
Member MethodsPurpose
void input( )To input the vno and hours
void calculate( )To compute the parking charge at the rate ₹3 for the first hour or the 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.

Java

Java Classes

ICSE 2015

85 Likes

Answer

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();
    }
}

Variable Description Table

Program Explanation

Output

Answered By

36 Likes


Related Questions