user12938819
user12938819

Reputation:

Static methods in an interface

I'm trying to add static methods largest and smallest to the Measurable interface. The methods should return the object with the largest or smallest measure (double) from an array of Measurable Country objects. I tried doing so in the interface, but someone recommended me using the Comparator interface. How can this be done by using the Measurable interface instead?

class Main {
    public static void main(String args[]) {
        Measurable[] countries = new Measurable[3];
        countries[0] = new Country("Uruguay", 176220);
        countries[1] = new Country("Thailand", 514000);
        countries[2] = new Country("Belgium", 30510);
        Measurable maximum = Measurable.largest(countries);
        Measurable smallest = Measurable.smallest(countries);
    }
}


class Country implements Measurable {
    private String name;
    private double area;


    public Country(String name, double area) {
        this.name = name;
        this.area = area;
    }
}

interface Measurable {
    static Measurable largest(Measurable[] countries) {

        public static Measurable largest(Measurable[]objects){
            if (objects == null || objects.length == 0) {

                return new Country("", 0);

            }

            Measurable max = new Country("", 0);

            for (Measurable obj : objects) {

                if (obj.getMeasure() > max.getMeasure()) {

                    max = obj;

                }

            }

            return max;

        }
    }


    static Measurable smallest(Measurable[] objects) {
        if (objects == null || objects.length == 0) {

            return new Country("", 0);

        }

        Measurable max = new Country("", 0);

        for (Measurable obj : objects) {

            if (obj.getMeasure() < min.getMeasure()) {

                min = obj;

            }

        }

        return min;

    }

}

    double getMeasure();
}

Upvotes: 1

Views: 432

Answers (1)

HomeIsWhereThePcIs
HomeIsWhereThePcIs

Reputation: 1464

You don't need to create the Measurable interface if you want to use Comparator/Comparable.

Just implement Comparable in Country and then loop through the array to find min and max.

class Main {
public static void main(String args[]) {
    Country[] countries = new Country[3];
    countries[0] = new Country("Uruguay", 176220);
    countries[1] = new Country("Thailand", 514000);
    countries[2] = new Country("Belgium", 30510);

    Country max = null;
    Country min = null;
    for (Country c : countries) {
        if (max == null || max.compareTo(c) < 0) {
            max = c;
        }
        if (min == null || min.compareTo(c) > 0) {
            min = c;
        }
    }

    System.out.printf("max: %s (%s)%n", max.name, max.area);
    System.out.printf("min: %s (%s)%n", min.name, min.area);
}
}


class Country implements Comparable<Country> {
String name;
double area;

public Country(String name, double area) {
    this.name = name;
    this.area = area;
}

@Override
public int compareTo(Country other) {
    // Returns int <0 if this is smaller than other
    // 0 if they are equal
    // int >0 if this is greater than other
    return Double.compare(this.area, other.area);
}
}

If you put your countries in a collection you can use the Collections.min() and Collections.max() functions together with the Comparable interface. Your main method would then look like this:

public static void main(String args[]) {
    List<Country> countries = new ArrayList<>();
    countries.add(new Country("Uruguay", 176220));
    countries.add(new Country("Thailand", 514000));
    countries.add(new Country("Belgium", 30510));

    Country max = Collections.max(countries);
    Country min = Collections.min(countries);

    System.out.printf("max: %s (%s)%n", max.name, max.area);
    System.out.printf("min: %s (%s)%n", min.name, min.area);
}

If you still want to use the Measurable interface you can extend ArrayList and have that class implement it like this:

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

class Main {
    public static void main(String args[]) {
        CountryList countries = new CountryList();
        countries.add(new Country("Uruguay", 176220));
        countries.add(new Country("Thailand", 514000));
        countries.add(new Country("Belgium", 30510));

        Country max = countries.getLargest();
        Country min = countries.getSmallest();

        System.out.printf("max: %s (%s)%n", max.name, max.area);
        System.out.printf("min: %s (%s)%n", min.name, min.area);
    }
}

class CountryList extends ArrayList<Country> implements Measurable{
    @Override
    public Country getSmallest() {
        return Collections.min(this);
    }
    @Override
    public Country getLargest() {
        return Collections.max(this);
    }
}

interface Measurable{
    Country getSmallest();
    Country getLargest();
}

class Country implements Comparable<Country> {
    String name;
    double area;

    public Country(String name, double area) {
        this.name = name;
        this.area = area;
    }

    @Override
    public int compareTo(Country o) {
        return Double.compare(this.area, o.area);
    }
}

Upvotes: 1

Related Questions