1. This question involves the TrailHiker class, which tracks a hiker's journey through a series of landmarks. The TrailHiker class uses a helper method, getNextLandmark, to determine the path taken. You will write a constructor and a method in the TrailHiker class.
public class TrailHiker
{
/**
* The history of landmarks visited, formatted as a sequence
* separated by dashes (e.g., "Start-River-Mountain").
*/
private String trailHistory;
/**
* The total distance hiked so far.
*/
private int totalDistance;
/**
* Simulates a hike starting at startLandmark and continuing for
* numStops, as described in part (a)
* Precondition: startLandmark is a valid landmark name.
* numStops > 0
*/
public TrailHiker(String startLandmark, int numStops)
{ /* to be implemented in part (a) */ }
/**
* Returns the next landmark to visit based on the current landmark.
* Returns null if the trail ends.
* Precondition: current is a valid landmark name.
*/
public String getNextLandmark(String current)
{ /* implementation not shown */ }
/**
* Returns the distance in miles to hike to the specified landmark.
* Precondition: landmark is a valid landmark name.
*/
public int getDistance(String landmark)
{ /* implementation not shown */ }
/**
* Returns the number of times the specified landmark appears
* in the trailHistory, as described in part (b)
* Precondition: landmark is not null.
*/
public int countLandmark(String landmark)
{ /* to be implemented in part (b) */ }
/* There may be instance variables, constructors, and methods
that are not shown. */
}Write the TrailHiker constructor, which simulates a hike. The constructor should initialize the instance variable trailHistory to the startLandmark and totalDistance to 0. It should then simulate numStops movements. In each movement, the constructor must obtain the next landmark by calling the helper method getNextLandmark using the most recently added landmark. If a next landmark exists (is not null), it is appended to trailHistory preceded by a dash "-", and the distance to that landmark (obtained by calling getDistance) is added to totalDistance. If getNextLandmark returns null, the hike ends early, and no further stops are attempted.
Example 1
| Method Call | Return Value |
|---|---|
| getNextLandmark("Trailhead") | "Stream" |
| getDistance("Stream") | 2 |
| getNextLandmark("Stream") | "Peak" |
| getDistance("Peak") | 5 |
| getNextLandmark("Peak") | "Camp" |
| getDistance("Camp") | 3 |
Example 2
| Method Call | Return Value |
|---|---|
| getNextLandmark("Gate") | "Meadow" |
| getDistance("Meadow") | 4 |
| getNextLandmark("Meadow") | null |
/** * Simulates a hike starting at startLandmark and continuing for * numStops, as described in part (a) * Precondition: startLandmark is a valid landmark name. * numStops > 0 */ public TrailHiker(String startLandmark, int numStops)
Write the countLandmark method, which returns the number of times the parameter landmark appears in the instance variable trailHistory. You may assume trailHistory contains valid landmark names separated by dashes.
Example 1
| Method Call | Return Value |
|---|---|
| countLandmark("River") | 2 |
| countLandmark("Wood") | 1 |
| countLandmark("Mountain") | 0 |
/** * Returns the number of times the specified landmark appears * in the trailHistory, as described in part (b) * Precondition: landmark is not null. */ public int countLandmark(String landmark)
00:00