In: Computer Science
JAVA
Add static methods largest and smallest to the Measurable interface. The methods should return the object with the largest or smallest measure from an array of Measurable objects.
Answer-
Your code is here -
public static Measurable largest(Measurable[]
objects)
{
if (objects.length == 0)
{
return null;
}
Measurable largestData = objects[0];
for (int i = 1; i < objects.length; i++)
{
Measurable current = objects[i];
if (current.getMeasure() >
largestData.getMeasure())
{
largestData = current;
}
}
return largestData;
public static Measurable
smallest(Measurable[] objects)
{
if (objects.length == 0)
{
return
null;
}
Measurable smallestData =
objects[0];
for (int i = 1; i <
objects.length; i++)
{
Measurable
current = objects[i];
if
(current.getMeasure() < smallestData.getMeasure())
{
smallestData = current;
}
}
return smallestData;
}
}
Note- Please do upvote, if any problem then comment in box sure
I will help.