Result 0.0 - even with typecast or swapping variable types

-1

I have this method in the secondary class to calculate the median of a vector:

public class Funcoes {
....
 public void setMediana(int[] valores) {

        double med;

        Arrays.sort(valores);
        int meio = valores.length / 2;
        if (valores.length % 2 == 0) {
            int esquerda = valores[meio - 1];
            int direita = valores[meio];
            med = (double) (esquerda + direita) / 2;
        } else {
            med = (double) valores[meio];
        }

        this.mediana = med;
}
    public double getMediana() {
        return mediana;
}

And this is my main class:

{
public class EstatisticaX3 {

    public static void main(String[] args) {

        int[] valores = {56, 61, 57, 77, 62, 75, 63, 55, 64, 60, 60, 57, 61, 57, 67, 62, 69, 67, 68, 59, 65,
            72, 65, 61, 68, 73, 65, 62, 75, 80, 66, 61, 69, 76, 72, 57, 75, 68, 83, 64, 69, 64, 66, 74,
            65, 76, 65, 58, 65, 64, 65, 60, 65, 80, 66, 80, 68, 55, 66, 71};

        Funcoes exemplo01 = new Funcoes(valores);

        System.out.println(exemplo01.getMedia());
        System.out.println("");
        System.out.println(exemplo01.setFrequenciaOrdem(valores));
        System.out.println(" ");
        System.out.println(exemplo01.getMediana()); */aqui que o resultado é 0.0*

    }
}

I tried to use the variable med as integer and give typecast, I tried changing the other variables too, although I think this way would be correct and still only give 0.0

    
asked by anonymous 02.11.2017 / 17:17

1 answer

0

You are not calling the setMediana method so the result is the initial value of the variable mediana , that is, 0.0 .

Try something like

System.out.println(" ");
exemplo01.setMediana(valores);
System.out.println(exemplo01.getMediana());

(not tested)

It's kind of confusing, I'd do a single calcularMediana method instead of setMediana and getMediana

    
03.11.2017 / 15:08