GuideFoot - Learn Together, Grow Smarter. Logo

In Computers and Technology / High School | 2025-07-08

class Deliverable { // Complete the missing code here } abstract class Vehicle { RoutingStrategy routingStrategy; public String deliver(Deliverable deliverable) { // Complete the missing code here } public void setRoutingStrategy(RoutingStrategy routingStrategy) { this.routingStrategy = routingStrategy; } } class Car extends Vehicle { public Car(RoutingStrategy routingStrategy) { // Complete the missing code here } } class Truck extends Vehicle { public Truck(RoutingStrategy routingStrategy) { // Complete the missing code here } } class Plane extends Vehicle { public Plane(RoutingStrategy routingStrategy) { // Complete the missing code here } } abstract class RoutingStrategy { // Complete the missing code here }

Asked by camosloppy7233

Answer (1)

This question involves object-oriented programming concepts using Java. Here's a step-by-step guide to completing the missing parts of the code:

Class Deliverable :

Since Deliverable is not defined, it could be an interface or a class with specific attributes and methods related to items that can be delivered. To keep it simple, let's imagine it has an attribute like weight and a method getWeight() for this example.

interface Deliverable { double getWeight(); }

Class Vehicle :

This class uses a RoutingStrategy for the delivery. The deliver method can demonstrate a simple interaction with the deliverable but would primarily involve using the RoutingStrategy to determine how the delivery would occur.

abstract class Vehicle { RoutingStrategy routingStrategy;
public String deliver(Deliverable deliverable) { return routingStrategy.calculateRoute() + ", delivering a package weighing " + deliverable.getWeight() + " kg."; }
public void setRoutingStrategy(RoutingStrategy routingStrategy) { this.routingStrategy = routingStrategy; } }

Class Car :

The constructor should initialize the routing strategy.

class Car extends Vehicle { public Car(RoutingStrategy routingStrategy) { this.routingStrategy = routingStrategy; } }

Class Truck :

Similarly, initialize the routing strategy in the constructor.

class Truck extends Vehicle { public Truck(RoutingStrategy routingStrategy) { this.routingStrategy = routingStrategy; } }

Class Plane :

Again, initialize the routing strategy in the constructor.

class Plane extends Vehicle { public Plane(RoutingStrategy routingStrategy) { this.routingStrategy = routingStrategy; } }

Abstract Class RoutingStrategy :

This class likely requires a method like calculateRoute() which will be implemented by concrete strategy classes.

abstract class RoutingStrategy { public abstract String calculateRoute(); }


In conclusion , with these implementations, you can see how the strategy pattern is used to vary the routing strategy dynamically at runtime. This setup is very useful in systems where the behavior of a delivery vehicle needs to adapt based on different strategies without altering the vehicle classes themselves. Understanding these concepts can be very beneficial in object-oriented design.

Answered by MasonWilliamTurner | 2025-07-21