Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 2 Current »

Provide the classes 1) Drive and 2) Bicycle and demonstrate their usage in the Lab1 driver using the starter code below:

Final Solution UML:

Code Starter

TravelMode.java

package StatePatternLabOne;

public interface TravelMode {
    Object getETA();
    Object getDirection();
}

Walk.java

package StatePatternLabOne;

public class Walk implements TravelMode {
    @Override
    public Object getETA() {
        System.out.println("Calculating ETA (walking)");
        return 4;
    }
    @Override
    public Object getDirection() {
        System.out.println("Calculating Direction (walking)");
        return 4;
    }
}

Bus.java

package StatePatternLabOne;

public class Bus implements TravelMode {
    @Override
    public Object getETA() {
        System.out.println("Calculating ETA (transit)");
        return 3;
    }
    @Override
    public Object getDirection() {
        System.out.println("Calculating Direction (transit)");
        return 3;
    }
}

DirectionService.java

package StatePatternLabOne;

public class DirectionService {
    private TravelMode travelMode;

    public DirectionService(TravelMode travelMode) {
        this.travelMode = travelMode;
    }

    public Object getETA() {
        return travelMode.getETA();
    }

    public Object getDirection() {
        return travelMode.getDirection();
    }

    public TravelMode getTravelMode() {
        return travelMode;
    }

    public void setTravelMode(TravelMode travelMode) {
        this.travelMode = travelMode;
    }
}

Lab1.java (driver)

package StatePatternLabOne;
public class Lab1 {
    public static void main(String[] args) {

        System.out.println("Walking:");
        var serviceOne = new DirectionService(new Walk());
        serviceOne.getETA();
        serviceOne.getDirection();

        System.out.println("Bus:");
        var serviceTwo = new DirectionService(new Bus());
        serviceTwo.getETA();
        serviceTwo.getDirection();

        //TODO - demonstrate the Bicycle Class - you will need to create the Bicycle class
        // Note the Bicycle class returns 2 and implements the getETA and getDirection

        //TODO - demonstrate the Drive Class - - you will need to create the Drive class
        // Note the Drive class returns 1 and implements the getETA and getDirection
    }
}

Current Output:

  • No labels