Create a Java interface called Measurable . The Measurable interface should have
ID: 3682771 • Letter: C
Question
Create a Java interface called Measurable. The Measurable interface should have one method:
int getMeasure()
Modify the Wall and Field classes to implement the Measurable interface.
Return the measure of a Wall as its area (height * length)
Return the measure of a Field as its perimeter (2 * width + 2 * length)
Create a MeasurableSet class that stores and analyzes Measurable objects.The MeasurableSet class should have three instance variables:
ArrayList<Measurable> that holds all Measurable objects that have been added to the MeasurableSet
Measurable min that holds the object with the smallest measure that has been added to the MeasurableSet
Measurable max that holds the object with the largest measure that has been added to the MeasurableSet.
The MeasurableSet class has a default constructor that creates the ArrayList and sets min and max to null.
The MeasurableSet class has the following methods:
void add (Measurable m): a mutator to add a new Measurable object to the MeasurableSet. Set min if the object is smaller than any previously seen object, max if the object is larger than any previously seen object.
Measurable getMin(): accessor to return the min object
Measurable getMax(): accessor to return the max object
ArrayList<Measurable> getMiddle (int floor, int ceiling): accessor to return the Measurable objects in the MeasurableSet whose Measurable value is greater than or equal to floor, and less than or equal to ceiling
Write a class called MeasurableSetTester with a main method. The main method should create a MeasurableSet object, and two Wall objects and two Field objects:
wall1: length = 12, height = 8
wall2: length = 10, height = 8
field1: length = 8, width = 11
field2: length = 10, width = 10
Add these objects to the MeasurableSet, and print the smallest and largest values returned from the MeasurableSet.
Test the getMiddle method twice by passing in
floor: 95 and ceiling: 105.
floor: 75 and ceiling: 100.
Explanation / Answer
interface Measurable
{
int getMeasure();
}
class Wall impliments Measurable
{
int length, height;
Wall(int l, int h)
{
length=l;
height=w;
}
public int getMeasure()
{
return height*length;
}
}
class Field impliments Measurable
{
int width, length;
Field(int w, int l)
{
width=w;
length=h;
}
public int getMeasure()
{
return 2*width+2*length;
}
}
class MeasurableSet
{
public static void main(String args[])
{
Wall wall1=new Wall(12,8);
Wall wall2=new Wall(10,8);
Field field1=new Field(8,11);
Field field2=new Field(10,10);
System.out.println(wall1.getMeasure());
System.out.println(wall2.getMeasure());
System.out.println(field1.getMeasure());
System.out.println(field2.getMeasure());
}
}