Question: LAB^: Hiking Trails (arrays) Suppose elevations along a trail are stored in an array. Each element represents the elevation (in feet) at a marker along

LAB^: Hiking Trails (arrays)

Suppose elevations along a trail are stored in an array. Each element represents the elevation (in feet) at a marker along the trail. The first marker will be stored at array index 0, etc.

The Trail class has been started for you.

Write a method to determine the maximum elevation of a trail segment and a method to determine the minimum elevation of a trail segment.

Write the method isLevel() which determines whether a segment along the trail is level. A level trail is one that has a difference between the maximum and minimum elevations of less than or equal to 50 feet. A trail segment is a subset of the trail, indicated by both a starting and an ending index. Thus the full trail segment would have a start of 0 and an end of number of markers - 1.

A TrailDriver class has been provided for you to test your method.

TRAILDriver.JAVA:

public class TrailDriver { public static void main(String[] args) { Trail trail = new Trail (new int[]{1210, 1375, 1380, 1430, 1290, 1490, 1500, 1250, 850, 925, 765, 640, 860, 920, 1110, 940, 960, 975, 950, 925}); System.out.println("Trail is level between markers 0 and 19? Should be false: " + trail.isLevel(0, 19)); System.out.println("Trail is level between markers 4 and 10? Should be false: " + trail.isLevel(4, 10)); System.out.println("Trail is level between markers 1 and 2? Should be true: " + trail.isLevel(1, 2)); System.out.println("Trail is level between markers 15 and 19? Should be true: " + trail.isLevel(15, 19));

} }

TRAIL.JAVA (NEEDS EDITING);

public class Trail { private int[] elevationMarkers; public Trail (int[] elevations) { // Instantiate instance variable with same length as parameter this.elevationMarkers = new int[elevations.length]; // Copy parameter array to instance variable for (int i = 0; i < elevations.length; i ++) { this.elevationMarkers[i] = elevations[i]; } } /** * toString method returns a String with elevations separated by a comma - ends with a comma */ public String toString() { String s = ""; for (int elevation : elevationMarkers) { s += elevation + ", "; } return s; }

/** * returns the maximum elevation between start and end */ public int maxElevation(int start, int end) {

// TODO Implement this method return 0; } /** * returns the minimum elevation between start and end */ public int minElevation(int start, int end) {

// TODO Implement this method return 0; }

/** * isLevel determines whether a trail segment is level or not. * A trail segment is considered level if the difference between max and min elevations is less than or equal to 50 */ public boolean isLevel(int start, int end) { // TODO Implement this method } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!