In: Computer Science
Define an interface called Vehicle, which requires the following methods getName: returns a String getTopSpeed: returns an int getMaxPassengers: returns an int Note that the class Car implements this interface and is preloaded (in one of the following questions you will define this class)
For example:
Test
Vehicle v = new Car("BMW", 280, 5); System.out.println(v.getName()); System.out.println(v.getTopSpeed()); System.out.println(v.getMaxPassengers());
Results :
BMW
280
5
Test 2 :
Vehicle v = new Car("Smart", 150, 2); System.out.println(v.getName()); System.out.println(v.getTopSpeed()); System.out.println(v.getMaxPassengers());
Results :
Smart
150
2
//Test.java
/* Interface Vehicle with abstract methods */
interface Vehicle {
public String getName();
public int getTopSpeed();
public int getMaxPassengers();
}
/* class car implements interface Vehicle */
class Car implements Vehicle {
private String name;
private int topSpeed, maxPassengers;
public Car(String string, int i, int j) {
setName(string);
setTopSpeed(i);
setMaxPassengers(j);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getTopSpeed() {
return topSpeed;
}
public void setTopSpeed(int topSpeed) {
this.topSpeed = topSpeed;
}
public int getMaxPassengers() {
return maxPassengers;
}
public void setMaxPassengers(int maxPassengers)
{
this.maxPassengers =
maxPassengers;
}
}
/* Test class */
public class Test {
public static void main(String[] args) {
Vehicle v = new Car("BMW", 280,
5);
System.out.println(v.getName());
System.out.println(v.getTopSpeed());
System.out.println(v.getMaxPassengers());
}
}
//Output