Code: Select all
Package Chp9Ex;
import java.util.*;
interface Drivable {
public void drive();
default void start(){
System.out.println("Start the vehicle.");
}
}
interface VehicleHelper {
static void register(Vehicle v){
System.out.println("Vehicle's VIN number: "+v.getVIN()+".");
}
}
abstract class Vehicle {
String vin;
abstract public void getMakeAndModel();
abstract public String getFeatures( String featureName);
final String getVIN(){
return vin;
}
}
class Car extends Vehicle implements Drivable, VehicleHelper {
public Car(String sc){
this.vin = sc;
VehicleHelper.register(this);
//pass value of reference this, since can't instantiate the class within
//its constructor causing stackOverflow due to recursion.
}
@Override
public void drive () { }
@Override
public String getFeatures (String featureName) {
String result = "N.A";
ArrayList<String> fn = new ArrayList<>();
fn.add("Height");
fn.add("width");
fn.add("length");
fn.add("power");
fn.add("boot capacity");
for (String item : fn){
if (featureName.equals(item)){
result = "Feature '"+item+"' is supported.";
break;
}
}
return result;
}
@Override
public void getMakeAndModel(){ }
@Override
public void start(){
System.out.println("Start the car.\n");
}
}
class Truck extends Vehicle implements Drivable, VehicleHelper {
public Truck(String st){
this.vin = st;
VehicleHelper.register(this);
}
@Override
public void drive () { }
@Override
public String getFeatures (String featureName) {
String result = "N.A";
ArrayList<String> fn = new ArrayList<>();
fn.add("Height");
fn.add("width");
fn.add("length");
fn.add("power");
fn.add("boot capacity");
for (String item : fn){
if (featureName.equals(item)){
result = "Feature '"+item+"' is supported.";
break;
}
}
return result;
}
@Override
public void getMakeAndModel(){ }
@Override
public void start(){
System.out.println("Start the Truck.\n");
}
}
class ToyCar extends Car {
public ToyCar(){
super("");
}
}
public class CompareAutos {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
String setCVin = "vinC111";
Car c = new Car(setCVin);
System.out.println("Car's vin number: "+c.getVIN());
VehicleHelper.register(c); //calling register from here does not satisfy question 8
System.out.println(c.getFeatures("Wings"));
c.start();
String setTVin = "vinT222";
Truck t = new Truck(setTVin);
System.out.println("Truck's vin number: "+t.getVIN());
VehicleHelper.register(t); //calling register from here does not satisfy question 8
System.out.println(t.getFeatures("power"));
t.start();
}
}
1. Does the code stratify second half of question 3 - Furthermore, you don't want any subclass to change the behavior of the getVIN method. Where and how will you code the getVIN method?
2. Does the code satisfy questions 5 - Ensure that every vehicle is created with a VIN.?
3. In order to make the code work, I had to change the parameter in VehicleHelper Interfaces' method "register" in question 8, from type Vehicle to Type String. So I have a feeling something is not right, but I can't put my finger on it!
(This is question 8. - Create an interface named VehicleHelper with a static method register(Vehicle v) that prints the VIN of the vehicle. Ensure that VehicleHelper's register method is invoked whenever an instance of a vehicle is created.)
Thank you