2

Over here i'm trying to make use of an interface called "Measurable" with one method called getMeasure. The problem is, I'm trying to get the average height of all the people in my program, by accessing the method in my interface. One of the problems I can't access the getMeasure method for some reason, and at last get the average height of all the people in the array. How can I solve this problem?

class Main {
    public static void main(String args[]) {
        Person[] people = new Person[4];
        people[0] = new Person("Larry", 65);
        people[1] = new Person("Susan", 45);
        people[2] = new Person("Joe", -45);
        people[3] = new Person("", 0);
        double averageHeight = average(people);
    }

    public static double average(Person[] objects)
    {
        if (objects.length == 0) { return 0; }
        double sum = 0;
        for (Person obj : objects)
        {
            double measure = obj.getMeasure();
            sum = sum + measure;
        }
        double result = sum / objects.length;
        return result;
    }
}



interface Measurable {
    double getMeasure();
}
public class Person {
    private String name;
    private Integer height;



    public Person(String name, Integer height)
    {
        this.name = name;
        this.height = height;
    }


    public double getMeasure() {
        return height;
    }
}

2
  • You Person doesn't implement the interface... Make it's declaration public class Person implements Measurable { ... } Commented Mar 10, 2020 at 12:42
  • 1
    Your code should work "as-is", although you dont make use of the Measurable interface at all. Please post the exact error you get! Commented Mar 10, 2020 at 12:46

2 Answers 2

4

The Person class should implement Measurable:

public class Person implements Measurable {
    ...
}

Then if you want to make your average function reusable (I guess this is your desired outcome), you should replace the Person instance with Measurable:

public static double average(Measurable[] objects) {
    ...
    for (Measurable obj : objects){
        ...
    }
}

This way you could have different Measurable implementations to calculate the average.

Sign up to request clarification or add additional context in comments.

4 Comments

Just by calling your function same name as interface function doesn't mean you are overriding it. As explained by Alex you have to implement that interface in order to override it's methods
Sure, the interface is not used if the OP doesn't implements the interface, however that doesn't really answer the question: "One of the problems I can't access the getMeasure method"
I was trying to implement the interface and make use of the getMeasure method
@UMLtotallydistilled is my answer what you intended to use? - trying to guess minds here :)
0
public class Person implements Measurable {
private String name;
private Integer height;



public Person(String name, Integer height)
{
    this.name = name;
    this.height = height;
}

@Override
public double getMeasure() {
    return height;
}

You have to implement the to use it. If the Interface also implements the method, you have to add the @Override modifier.

2 Comments

The @Override annotation is not mandatory in this case. It should work without it, as long as the class implements the interface. Although it won't work the other way - if you have the @Override, but don't implement the interface, you get a compile error.
Therefore I mentioned that this is only necessary if the interface also implements the method @AlexSomai

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.