PART 1

  • Create a Country class that implements the interface Measurable.
    • A country has the name (String) of the country and its land's surface area (double).
    • The method getMeasurement should return the surface area.
  • You are given the interface Measurable and driver class MeasurableTester
  • Make sure the driver, MeasurableTester works with you new Country class.

PART 2

  • Modify the Measurable interface by writing code for the static interface method: public static Measurable max(Measurable[] list) This method returns the object from the list that has the largest (measurable) value.
  • Uncomment the section for Part 2 in the MeasurableTester that calls the new method on the given array of countries, and prints out the largest Country in the array.

PART 3

  • Modify the Country class to implement the Java API's Comparable Interface based on the surface area
  • Recall the Comparable interface from the lecture slides or refer to https://docs.oracle.com/en/java/javase/15/docs/api/java.base/java/lang/Comparable.html (Links to an external site.) to know how to implement the abstract method.
  • At the class declaration line, use: implements Comparable
  • Write code implementation for the method: public int compareTo(Country c)
  • Note: You are comparing each Country object's surface area to the other
  • Uncomment MeasurableTester code for part 3.
  • Make sure the countries printed out are in order now!
  • Arrays.sort is an API method that sorts elements in an array that has implemented the Comparable interface.
  • In class, we used Collections.sort, an API method that sorts elements stored in an ArrayList
  • The API's sorting algorithm works without you needed to know how its done as long as you implement the interface. Yay!

PART 4

  • Create a class Mdterm that implements the Measurable interface.
    • A Mdterm object has both the score (double) and a student's name (String).
  • Create a new driver program MdtermTester to create and process an array of Mdterm objects. This driver program has similar functionality as the previous MeasurableTester program.
    • Create 10 Mdterm objects -- you can decide on the name and scores of each object
    • Print each object (name and score)
    • Display the average score of the 10 objects (using the interface method)
    • Display the student with the highest score (using the interface method)
    • sort the mdterm objects using the Arrays.sort method
    • Print the sorted objects.

Starter Codes

Measurable.java

public interface Measurable
{
double getMeasurement();
/**
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)
{
double sum = 0.0;
for (Measurable obj : objects)
{
sum = sum + obj.getMeasurement();
}
if (objects.length > 0)
{
return sum / objects.length;
} else {
return 0.0;
}
}
/**
Finds the maximum of the measurements of the given objects.
@param objects an array of Measurable objects
@return the maximum of the measures, null if array is empty
*/
public static Measurable max(Measurable[] objects)
{
// WRITE CODE HERE
return null;
}
}

MeasurableTester.java

/**
This program demonstrates the measurable Country class.
*/
import java.util.*;
public class MeasurableTester
{
public static void main(String[] args)
{
Measurable[] countries = new Measurable[3];
countries[0] = new Country("Uruguay", 176220);
countries[1] = new Country("Thailand", 513120);
countries[2] = new Country("Belgium", 30510);
for(Measurable c: countries)
System.out.println(c);
System.out.println("nThe average surface are of these countries is: " +
Measurable.average(countries));
System.out.println("Expected: 239950.0");
/* ---------------- PART 2 ----------------------
Measurable maxCountry = Measurable.max(countries);
System.out.println("nMaximum area: " + maxCountry.getMeasurement());
System.out.println("Expected: 513120");
*/
/* ---------------- PART 3 ----------------------
Arrays.sort(countries);
for(Measurable c: countries)
System.out.println(c);
*/
}
}
Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.