Use the following steps and write the code on paper. 1. Create an interface call
ID: 3861505 • Letter: U
Question
Use the following steps and write the code on paper.
1. Create an interface called Manual that has two methods, gearUp and gearDown, which does not take any argument. They both return void.
2. Then one abstract class called Vehicle, which we will use to create two subclasses: Car and Bicycle.
3. All vehicle should have a current gear (int) and a registration number (String) but only Car have a license plate (String). All cars have 5 speed (from 1 to 5) and all bicycles have 18 speed (from 1 to 18). You also need an int constant (MAX_GEAR) that indicate the maximum gear.
4. Create an initializer constructor for every class to initialize their data member(s). [hint: you should have 3 and only 3 constructors in total] 5. Implement gearUp and gearDown for both Car and Bicycle. Simply increase current gear if it has not reach the largest possible gear; otherwise, does nothing. gearDown should decrease current gear if it is greater than 1; otherwise, does nothing.
Explanation / Answer
// creating Manual interface
interface Manual {
// has two methods
public void gearUp();
public void gearDown();
}
// this is Vehicle abstract class
public abstract class Vehicle {
// having datamembers
private int current_gear;
private String registration_number;
private int MAX_GEAR;
// Vehicle constructor
public Vehicle(int current_gear, String registration_number, int MAX_GEAR) {
System.out.println("Vehicle Constructor");
this.current_gear = current_gear;
this.registration_number = registration_number;
this.MAX_GEAR = MAX_GEAR;
}
}
public class Car extends Vehicle implements Manual{
String license_plate;
int MAX_GEAR=5;
System.out.println("CAR Constructor");
// Car constructor
public Car(String license_plate,int MAX_GEAR){
this.license_plate=license_plate;
this.MAX_GEAR=MAX_GEAR;
}
// implementing methods which was declared in Manual interface
public void gearUp(){
System.out.println("This is gearUp Method in CAR");
}
public void gearDown(){
System.out.println("This is gearDown Method in CAR");
}
}
public class Bicycle extends Vehicle implements Manual{
int MAX_GEAR=18;
System.out.println("Bicycle Constructor");
// Bicycle Constructor
public Bicycle(int MAX_GEAR){
this.MAX_GEAR=MAX_GEAR;
}
// implementing methods which was declared in Manual interface
public void gearUp(){
System.out.println("This is gearUp Method in Bicycle");
}
public void gearDown(){
System.out.println("This is gearDown Method in Bicycle");
}
}