[HD-OCP17/21-Fundamentals Pg 0, Sec. 13.7.0 - exercise]

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
joaoclopes
Posts: 26
Joined: Mon Sep 23, 2024 4:49 pm
Contact:

[HD-OCP17/21-Fundamentals Pg 0, Sec. 13.7.0 - exercise]

Post by joaoclopes »

Hello! Regarding the exercise is this a good solution?

Code: Select all

public interface Drivable {

    void drive();

    default void start() {
        System.out.println("Starting...");
    }
}


public interface VehicleHelper {

    static void register(Vehicle v) {
        System.out.println(v.getVIN());
    }
}


import java.util.List;

public abstract class Vehicle implements Drivable {

    private final String make;
    private final String model;
    private final String vin;
    private final List<String> features;

    public Vehicle(String make, String model, String vin, List<String> features) {
        this.make = make;
        this.model = model;
        this.vin = vin;
        this.features = features;
        VehicleHelper.register(this);
        start();
    }

    public String getMakeAndModel() {
        return "%s and %s".formatted(make, model);
    }

    public final String getVIN() {
        return this.vin;
    }

    public final String getFeature(String feature) {
        for (String f : features) {
            if (f.equals(feature)) {
                return feature;
            }
        }
        return "N.A";
    }

}


import java.util.List;

public class Car extends Vehicle {

    public Car(String make, String model, String vin) {
        super(make, model, vin, List.of("height"));
    }

    @Override
    public void drive() {
        System.out.println("drive car");
    }

    @Override
    public void start() {
        System.out.println("Starting car");
    }
}


public class ToyCar extends Car {

    public ToyCar() {
        super("toy", "toy", "12345");
    }
}


import java.util.List;

public class Truck extends Vehicle {

    public Truck(String make, String model, String vin) {
        super(make, model, vin, List.of("power"));
    }

    @Override
    public void drive() {
        System.out.println("Drive Truck");
    }

    @Override
    public void start() {
        System.out.println("Starting truck");
    }
}
Thanks!

admin
Site Admin
Posts: 10326
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: [HD-OCP17/21-Fundamentals Pg 0, Sec. 13.7.0 - exercise]

Post by admin »

Yes, this looks good :thumbup:
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: No registered users and 9 guests