In: Computer Science
Instructions
Using the installed software for this course create a generic class called VehicleRental which accepts any generic type of Vehicle ( create an instance of Car, Van and MotorCycle classes)
Each type of Vehicle object has methods called drive, start and stop ( add simple print statement)
The VehicleRental class has a method called rent which accept a generic type of Vehicle object, this method will call drive method of passed Vehicle object
The solution will produce the following:
Since you have not mentioned the language of your preference, I am providing the code in Java.
CODE
interface Vehicle {
void drive();
void start();
void stop();
}
class Van implements Vehicle {
@Override
public void drive() {
System.out.println("Driving the van...");
}
@Override
public void start() {
System.out.println("Starting the van...");
}
@Override
public void stop() {
System.out.println("Stopping the van...");
}
}
class Car implements Vehicle {
@Override
public void drive() {
System.out.println("Driving the car...");
}
@Override
public void start() {
System.out.println("Starting the car...");
}
@Override
public void stop() {
System.out.println("Stopping the car...");
}
}
class MotorCycle implements Vehicle {
@Override
public void drive() {
System.out.println("Driving the motorcycle...");
}
@Override
public void start() {
System.out.println("Starting the motorcycle...");
}
@Override
public void stop() {
System.out.println("Stopping the motorcycle...");
}
}
class VehicleRental<T extends Vehicle> {
public void rent(Vehicle v) {
v.drive();
}
}
public class Main {
public static void main(String[] args) {
VehicleRental<Car> vr1 = new VehicleRental<>();
vr1.rent(new Car());
VehicleRental<Van> vr2 = new VehicleRental<>();
vr2.rent(new Van());
VehicleRental<MotorCycle> vr3 = new VehicleRental<>();
vr3.rent(new MotorCycle());
}
}
OUTPUT