Question: The Measurable interface is given as below. (You need to save it in Measurable.java.) /** Describes any class whose objects can be measured. */ public
The Measurable interface is given as below. (You need to save it in Measurable.java.)
/** Describes any class whose objects can be measured. */ public interface Measurable { /** Computes the measure of the object. @return the measure */ double getMeasure(); } Have the given House and Tree classes implement the Measurable interface. A House is measured by size and a Tree is measured by height. Implement the required method by the Measurable interface only.
Also, complete the given method swapMinAndMax that swaps the values with the smallest and largest object in an array of Measurable objects.
public class House { private double size; public House(double theSize) { size = theSize; } public String toString() { return size + "" ; } } public class Tree { private double height; public Tree(double theHeight) { height = theHeight ; } public String toString() { return height + "" ; } } class Reusable { /** Swaps the values with the smallest and largest measure. @param objects an array of objects of a class that implements the Measurable interface. */ public static void swapMinAndMax(Measurable[] objects) { // your work goes here. } } Here is a tester to test your Reusable program.
import java.util.Arrays; public class ReusableTester { public static void main(String[] args) { Measurable[] houses = new Measurable[3]; houses[0] = new House(30.2); houses[1] = new House(20.7); houses[2] = new House(5.8); Reusable.swapMinAndMax(houses); System.out.println(Arrays.toString(houses)); //prints [5.8, 20.7, 30.2] Measurable[] trees = new Measurable[3]; trees[0] = new Tree(10.3); trees[1] = new Tree(30.8); trees[2] = new Tree(2.9); Reusable.swapMinAndMax(trees); System.out.println(Arrays.toString(trees)); // prints [10.3, 2.9, 30.8] } } Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
