0% found this document useful (0 votes)
4 views

java assessmment 1

The document outlines an experiment for a vehicle rental service implemented in Java, focusing on object-oriented programming principles. It includes the creation of a base class 'Vehicle' and subclasses 'Car', 'Bike', and 'Truck' to manage vehicle types and rental fees using encapsulation and polymorphism. The program allows customers to rent vehicles and calculates rental fees based on the type and duration of the rental.

Uploaded by

premrawat9873
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

java assessmment 1

The document outlines an experiment for a vehicle rental service implemented in Java, focusing on object-oriented programming principles. It includes the creation of a base class 'Vehicle' and subclasses 'Car', 'Bike', and 'Truck' to manage vehicle types and rental fees using encapsulation and polymorphism. The program allows customers to rent vehicles and calculates rental fees based on the type and duration of the rental.

Uploaded by

premrawat9873
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

DEPARTMENT OF

COMPUTER SCIENCE & ENGINEERING

Experiment 1.1
Student Name: Prem Rawat UID: 23BCS10659
Branch:BE-CSE Section/Group: 811-A
Semester: Date of Performance: 21/01/2025
Subject Name: OOPs USING JAVA Subject Code: 23CSP-202

1. Aim: Implement a vehicle rental service where customers can rent different

types of vehicles. Develop a base class Vehicle and subclasses like Car, Bike,

and Truck to represent various vehicle types. Use encapsulation to manage

vehicle availability and customer information, and polymorphism to calculate

rental fees based on vehicle type and rental duration

2. Objective: The objective of this program is to implement a vehicle rental

service using object-oriented principles, enabling customers to rent different

types of vehicles (e.g., Car, Bike, Truck). It uses encapsulation to manage

vehicle availability and customer data, and polymorphism to calculate rental

fees based on vehicle type and rental duration, ensuring scalability and code

reusability.
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

3. Java Code:

abstract class Vehicle {

private String vehicleID;

private boolean isAvailable;

public Vehicle(String vehicleID) {

this.vehicleID = vehicleID;

this.isAvailable = true;

public boolean isAvailable() {

return isAvailable;

public void setAvailable(boolean available) {

this.isAvailable = available;

public abstract double calculateRentalFee(int days);

class Car extends Vehicle {


DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

public Car(String vehicleID) {

super(vehicleID);

@Override

public double calculateRentalFee(int days) {

return days * 50; // Daily rate for car

class Bike extends Vehicle {

public Bike(String vehicleID) {

super(vehicleID);

@Override

public double calculateRentalFee(int days) {

return days * 150; // Daily rate for bike

class Truck extends Vehicle {

public Truck(String vehicleID) {


DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

super(vehicleID);

@Override

public double calculateRentalFee(int days) {

return days * 100; // Daily rate for truck

class Customer {

private String name;

private int rentalTime; // Rental time in days

public Customer(String name, int rentalTime) {

this.name = name;

this.rentalTime = rentalTime;

public String getName() {

return name;

}
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

public int getRentalTime() {

return rentalTime;

public class VehicleRentalService {

public static void main(String[] args) {

Vehicle car = new Car("V123");

Vehicle bike = new Bike("V124");

Vehicle truck = new Truck("V125");

Customer customer1 = new Customer("Prem Rawat", 5);

Customer customer2 = new Customer("Ayush", 3);

Customer customer3 = new Customer("Ritesh", 7);

rentVehicle(car, customer1);

rentVehicle(bike, customer2);

rentVehicle(truck, customer3);

public static void rentVehicle(Vehicle vehicle, Customer customer) {

if (vehicle.isAvailable()) {

double fee = vehicle.calculateRentalFee(customer.getRentalTime());

System.out.println(customer.getName() + " rented the vehicle for " + customer.getRentalTime() + " days.");
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

System.out.println("Rental Fee: " + fee);

vehicle.setAvailable(false); // Vehicle rented

} else {

System.out.println("Vehicle is not available.");

4. Output:

You might also like