1. This question involves the TripPlanner class, which is used to plan a journey and calculate the total travel time. The TripPlanner class uses helper methods to determine the sequence of stops and the travel time to each stop.
public class TripPlanner
{
private String itinerary; // To be initialized in part (a)
private int totalMinutes; // To be initialized in part (a)
/**
* Returns the name of the next stop to visit, or null if there are no more stops.
* Postcondition: Returns a String representing the next stop or null.
*/
public String getNextStop()
{ /* implementation not shown */ }
/**
* Returns the time in minutes to travel to the specified stop.
* Precondition: stop is not null.
*/
public int getTravelTime(String stop)
{ /* implementation not shown */ }
/**
* Creates a TripPlanner object with a starting location.
* Builds the itinerary and calculates totalMinutes as described in part (a).
* Precondition: start is a non-empty string.
*/
public TripPlanner(String start)
{ /* to be implemented in part (a) */ }
/**
* Returns a simplified code for the trip based on the itinerary,
* as described in part (b).
* Precondition: itinerary contains at least one location.
* Each location in itinerary is at least 3 characters long.
* Locations in itinerary are separated by " -> ".
*/
public String generateCode()
{ /* to be implemented in part (b) */ }
}Write the TripPlanner constructor, which initializes the instance variables. The constructor should set itinerary to the start parameter and totalMinutes to 0. Then, it should repeatedly call the getNextStop method to obtain the next stop. If a stop is returned (i.e., not null), the constructor should append the string " -> " followed by the stop name to itinerary and add the travel time for that stop (obtained by calling getTravelTime) to totalMinutes. The process continues until getNextStop returns null.
Example 1
| Method Call | Return Value |
|---|---|
| getNextStop() | "Museum" |
| getTravelTime("Museum") | 20 |
| getNextStop() | "Cafe" |
| getTravelTime("Cafe") | 45 |
| getNextStop() | null |
/** * Creates a TripPlanner object with a starting location. * Builds the itinerary and calculates totalMinutes as described in part (a). * Precondition: start is a non-empty string. */ public TripPlanner(String start)
Write the generateCode method, which returns a string code representing the trip. The code is formed by taking the first three letters of each location in itinerary, converting them to uppercase, and separating them with hyphens ("-"). You may assume that itinerary is formatted correctly with locations separated by " -> " and that every location name has at least three characters.
Example 1
| Value of itinerary | Return Value of generateCode() |
|---|---|
| "Boston -> New York -> Washington" | "BOS-NEW-WAS" |
| "Home -> Gym" | "HOM-GYM" |
| "Paris" | "PAR" |
/** * Returns a simplified code for the trip based on the itinerary, * as described in part (b). * Precondition: itinerary contains at least one location. * Each location in itinerary is at least 3 characters long. * Locations in itinerary are separated by " -> ". */ public String generateCode()