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

Lab Manual - Lab 15_ Abstract Class & Interface

The document outlines a lab exercise for CSE 215L at North South University focusing on abstract classes and interfaces in programming. It details the structure of an abstract class 'Vehicle', its subclass 'MotorVehicle', and the concrete class 'Car' that implements the 'Maintainable' interface. The lab includes objectives, class definitions, methods, and an example test case to demonstrate the functionality of the classes created.

Uploaded by

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

Lab Manual - Lab 15_ Abstract Class & Interface

The document outlines a lab exercise for CSE 215L at North South University focusing on abstract classes and interfaces in programming. It details the structure of an abstract class 'Vehicle', its subclass 'MotorVehicle', and the concrete class 'Car' that implements the 'Maintainable' interface. The lab includes objectives, class definitions, methods, and an example test case to demonstrate the functionality of the classes created.

Uploaded by

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

North South University

Department of Electrical and Computer Engineering


CSE 215L: Programming Language II Lab

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

<<abstract>> MotorVehicle inherits Vehicle

- fuelType: String
- mileage: double

+fuelEfficiency(): double

<<interface>> Maintainable

+ scheduleMaintenance(): void
+ repair(): void

Car inherits MotorVehicle and Maintainable

- 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.

3. Abstract Class: MotorVehicle (extends Vehicle)


○ Attributes:
■ fuelType: Specifies the type of fuel (e.g., "Petrol" or "Diesel").
■ mileage: The vehicle's mileage in kilometers per liter.
○ Methods:
■ fuelEfficiency(): Calculates and returns fuel efficiency based on fuel
type. For example:
■ Petrol vehicles return mileage / 8.0.
■ Diesel vehicles return mileage / 10.0.
4. Concrete Class: Car
○ Methods:
■ Implements start(), stop(), and service() to provide
vehicle-specific messages.
■ Implements fuelEfficiency() for fuel calculations based on
fuelType.
■ Implements scheduleMaintenance() to output a message based on the
car’s mileage.
■ Implements repair() to print repair information.
■ Overrides displayInfo() to show full car details.

Example Test

Create a TestVehicleMaintenance class to demonstrate your solution:

1. Instantiate two Car objects with different attributes.


2. Call displayInfo(), start(), stop(), service(), fuelEfficiency(),
scheduleMaintenance(), and repair() on each car.

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 Vehicle(String licensePlate, int year) {


this.licensePlate = licensePlate;
this.year = year;
}

public String getLicensePlate() {


return licensePlate;
}

public int getYear() {


return year;
}

public abstract void start();


public abstract void stop();
public abstract void service();
public abstract void displayInfo();

public abstract class MotorVehicle extends Vehicle {


private String fuelType;
private double mileage;

public MotorVehicle(String licensePlate, int year, String fuelType, double mileage) {


super(licensePlate, year);
this.fuelType = fuelType;
this.mileage = mileage;
}

public String getFuelType() {


return fuelType;
}

public double getMileage() {


return mileage;
}

public abstract double fuelEfficiency();

public interface Maintainable {


void scheduleMaintenance();
void repair();
}

public class Car extends MotorVehicle implements Maintainable {


private int numberOfDoors;

public Car(String licensePlate, int year, String fuelType, double mileage, int numberOfDoors) {
super(licensePlate, year, fuelType, mileage);
this.numberOfDoors = numberOfDoors;
}

public int getNumberOfDoors() {


return 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
}

public class Test {

public static void main(String[] args) {


Car c1 = new Car("CA3456", 2009, "Petrol", 18, 4);
Car c2 = new Car("NY0078", 2017, "Diesel", 45, 2);

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

You might also like