Create interface with ArrayList

0

I have to solve an exercise in which they ask me to make an interface, the statement is as follows:

  

Construct an ArrayReals class that declares an attribute of type double   [] and that implements an interface called Statistics. Content   this interface is the following:

     
  • A minimum called method that returns the smallest value in the array.
  •   
  • A method called maximum that returns the largest value in the array.
  •   
  • A method called summation that returns the sum of all elements in the array.
  •   

Within the interface, at the time of creating the corresponding methods I do not know what code to put, that is, when I am asked to, for example, the interface have to return the smallest value of the ArrayList, how do I do it? In theory, the ArrayList is in one class and the interface in another.

Thanks and regards.

    
asked by Marc 30.03.2018 в 17:26
source

1 answer

1

To understand what you have to do first you must understand how interfaces and abstract methods work. These links can be helpful:

link

link

In the interfaces you declare the methods but not their implementation, you declare the implementation when you implement the interface.

Interface

public interface Estadisticas {

    // ...

    public int sumatoria();

}

Class

public class ArrayReals implements Estadisticas {

    double[] numeros;

    // ...

    @Override
    public int sumatoria() {

        int numerosSumados = 0;

        // Sumas los valores que almacena el array

        return numerosSumados;

    }

}

Then when you instantiate class ArrayReals you can call the methods you implemented from the interface and get their value.

ArrayReals arrayReals = new ArrayReals();
int numerosSumados = arrayReals.sumatoria();
    
answered by 30.03.2018 / 18:49
source