Lab Manual - Lab 15_ Abstract Class & Interface
Lab Manual - Lab 15_ Abstract Class & Interface
Objective:
● To understand abstract class
● To understand interface
<<abstract>> Vehicle
- licensePlate: String
- year: int
+ start(): void
+ stop(): void
+ service(): void
+ displayInfo(): void
- fuelType: String
- mileage: double
+fuelEfficiency(): double
<<interface>> Maintainable
+ scheduleMaintenance(): void
+ repair(): void
- numberOfDoors: int
1. Interface: Maintainable
○ scheduleMaintenance(): Schedules the maintenance based on the vehicle's
current mileage.
○ repair(): Performs repairs based on the vehicle's needs.
2. Abstract Class: Vehicle
■ start(): Outputs a message indicating the vehicle is starting.
■ stop(): Outputs a message indicating the vehicle is stopping.
■ service(): Prints details of general servicing requirements.
■ displayInfo(): Prints the vehicle’s license plate and year.
Example Test
Expected Output: Each car’s information and the details of starting, stopping, servicing, and
fuel efficiency calculations should be displayed in the console.
public abstract class Vehicle {
private String licensePlate;
private int year;
public Car(String licensePlate, int year, String fuelType, double mileage, int numberOfDoors) {
super(licensePlate, year, fuelType, mileage);
this.numberOfDoors = numberOfDoors;
}
@Override
public void scheduleMaintenance() {
if(getMileage()<30) {
System.out.println("Your car needs maintaineance in 1 year.");
}else if (getMileage()<20) {
System.out.println("Your car needs maintaineance immediately. Please invoke repair");
} else
System.out.println("Your car doesn't yet require any maintaineance");
}
@Override
public void repair() {
System.out.println("Your car is being repaired");
}
@Override
public double fuelEfficiency() {
if(getFuelType().toLowerCase().equals("petrol")) {
System.out.print("Your car's Fuel Efficiency is : ");
return getMileage()/8.0;
}else if(getFuelType().toLowerCase().equals("diesel")) {
System.out.print("Your car's Fuel Efficiency is : ");
return getMileage()/10.0;
}else {
System.out.println("Invalid fuel type");
return 0;
}
}
@Override
public void start() {
System.out.println("Your car has STARTED!");
}
@Override
public void stop() {
System.out.println("Your car has STOPPED!");
}
@Override
public void service() {
int serviceCharge;
if(getYear()>2010) {
serviceCharge= 150;
}else
serviceCharge = 300;
System.out.println("Servicing your car requires "+serviceCharge+"$");
}
@Override
public void displayInfo() {
System.out.println("Car Year :"+getYear()+"\nCar License No : "+getLicensePlate()+ "\nFuel Type: "+getFuelType()+ "\nCurrent Mileage: "+getMileage()+ "\nNumber of doors
}
c1.start();
c1.stop();
c1.service();
c1.scheduleMaintenance();
c1.repair();
System.out.println(c1.fuelEfficiency());
c1.displayInfo();
System.out.println();
c2.service();
c2.scheduleMaintenance();
System.out.println(c2.fuelEfficiency());
c2.displayInfo();
}