I do not know how to calculate distance

1
    Chromosome chromosome = new Chromosome();
    int[] gene = new int[6];
    gene[0] = 0;
    gene[1] = (int) (1 + (Math.random() * 3));
    gene[2] = (int) (4 + (Math.random() * 4));
    gene[3] = (int) (8 + (Math.random() * 4));
    gene[4] = (int) (12 + (Math.random() * 3));
    gene[5] = 15;

    chromosome.setGenes(gene);
    return chromosome;
}

I have this method that inserts random numbers, and with that, I need to do another method that calculates the total distance traveled, which would be the 6 positions of the array. But I'm having trouble figuring out a way to add that distance.

    
asked by anonymous 05.04.2017 / 19:45

2 answers

0

You can create the method:

public static int somarArray(int[] array) {
    int valorTotal = 0;
    for (int i = 0; i < array.length; i++) {
        valorTotal += array[i];
    }
    return valorTotal;
}

passing your array ex:

int[] gene = new int[6];
gene[0] = 0;
gene[1] = (int) (1 + (Math.random() * 3));
gene[2] = (int) (4 + (Math.random() * 4));
gene[3] = (int) (8 + (Math.random() * 4));
gene[4] = (int) (12 + (Math.random() * 3));
gene[5] = 15;

System.out.println(Classe.somarArray(gene));
    
05.04.2017 / 20:06
0

Just pass this chromosome object as a reference in another method, so you will have access to everything of this object.

example:

private int somaDistancia (Chromosome chromosome){
int soma = 0;
    for(int i=0; i<chromosome.getGenes().length){
        //faz a manipulacao por ex:
        soma += chromosome.getGenes()[i];
    }
    return soma;
}

Is this what you wanted?

    
05.04.2017 / 20:09