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
Code Block | ||
---|---|---|
| ||
package StatePatternLabOne;
public interface TravelMode {
Object getETA();
Object getDirection();
}
|
Walk.java
Code Block | ||
---|---|---|
| ||
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
Code Block | ||
---|---|---|
| ||
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
Code Block | ||
---|---|---|
| ||
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)
Code Block | ||
---|---|---|
| ||
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:");
serviceOne.setTravelMode(new Bus() );
serviceOne.getETA();
serviceOne.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:
...