Question: Have the class implement the Measurable interface in Java. The Segment class describes a segment of the real line with a given start and end

Have the class implement the Measurable interface in Java. The Segment class describes a segment of the real line with a given start and end point. The measure of a segment is the length. For example, the segment from 1 to 5 has length 4, as does the segment from 5 to 1.

Please use the following use:

public class Segment { private double start; private double end;

/** Constructs a linear segment. @param from the starting point @param to the ending point */ public Segment(double from, double to) { start = from; end = to; }

public String toString() { return start + "->" + end; } }

Use the following files for reference:

Measurable.java

/** Describes any class whose objects can be measured. */ public interface Measurable { /** Computes the measure of the object. @return the measure */ double getMeasure(); } 

Tester.java

public class Tester { public static void main(String[] args) { Segment[] segments = { new Segment(1, 5), // measure 4 new Segment(5, 1), // measure 4 new Segment(1, 1), // measure 0 new Segment(1, 3) // measure 2 }; System.out.println(average(segments)); System.out.println("Expected: 2.5"); } /** Computes the average of the measures of the given objects. @param objects an array of Measurable objects @return the average of the measures */ public static double average(Measurable[] objects) { if (objects.length == 0) { return 0; } double sum = 0; for (Measurable obj : objects) { sum = sum + obj.getMeasure(); } return sum / objects.length; } 

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!