Please answer all question and lable each of them. Use JAVA thanks 1. Create an
ID: 3802593 • Letter: P
Question
Please answer all question and lable each of them.
Use JAVA thanks
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
import java.util.*;
interface Manual //interface
{
public void gearUp();
public void gearDown();
}
abstract class Vehicle implements Manual //abstrat class implements functions of interface
{
private int currentGear;
private String registrationNum;
private int MAX_GEAR;
public Vehicle(int currentGear,String registrationNum,int MAX_GEAR) //base class constructor
{
this.currentGear = currentGear;
this.registrationNum = registrationNum;
this.MAX_GEAR = MAX_GEAR;
}
//get methods
public String getRegistrationNum()
{
return registrationNum;
}
public int getCurrentGear()
{
return currentGear;
}
//imlement interface functions
public void gearUp()
{
if(getCurrentGear() < MAX_GEAR)
currentGear++;
}
public void gearDown()
{
if(getCurrentGear() > 1)
currentGear--;
}
}
class Car extends Vehicle
{
private String licensePlate;
public Car(int currentGear,String registrationNum,int MAX_GEAR,String licensePlate)
{
super(currentGear,registrationNum,MAX_GEAR);//pass parameters to base class constructor
this.licensePlate = licensePlate;
}
public String toString()
{
return " Car : Registration Number : "+getRegistrationNum()+" licensePlate :"+ licensePlate +" Current Gear : "+getCurrentGear();
}
}
class Bicycle extends Vehicle
{
public Bicycle(int currentGear,String registrationNum,int MAX_GEAR)
{
super(currentGear,registrationNum,MAX_GEAR);//pass parameters to base class constructor
}
public String toString()
{
return " Bicycle : Registration Number : "+getRegistrationNum()+" Current Gear : "+getCurrentGear();
}
}
class TestVehicle
{
public static void main (String[] args)
{
//create objects and call methods
Car c = new Car(3,"6575786",5,"NY-5768");
c.gearUp();
System.out.println(c.toString());
Bicycle b = new Bicycle(8,"76868",18);
b.gearDown();
System.out.println(b.toString());
}
}
output:
Car : Registration Number : 6575786 licensePlate :NY-5768 Current Gear : 4
Bicycle : Registration Number : 76868 Current Gear : 7
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.