Doubt in Arrays - Beginner

3

How do I get the mostraEmpregados method to print the name of the employees I've already added?

How to construct the array within the Enterprise class declaration itself, so that every time a Empresa is instantiated, the array of Funcionario that it needs is also created?

package meu.programa;

public class Funcionario {
    String nome;
    String departamento;
    double salario;
    Data dataDeEntrada;
    String rg;
    boolean estaNaEmpresa = true;

    void recebeAumento(double valorDoAumento) {
        salario += valorDoAumento;
        System.out.println("O salario atual do funcionario " + this.nome + " eh: " + this.salario);
    }
    void calculaGanhoAnual () {
        double ganho = salario * 12;
        System.out.println("O ganho anual do funcionario " + this.nome + " eh: " + ganho);
    }
    void demite () {
        if (this.estaNaEmpresa == true) {
            this.estaNaEmpresa = false;
            System.out.println("O funcionario " + this.nome + " foi demitido ...");
        }
        else {
            System.out.println("O funcionario ja foi demitido ...");
        }
    }
    void mostra () {
        System.out.println("Nome: " + this.nome);
        System.out.println("Departamento: " + this.departamento);
        System.out.println("Salario: " + this.salario);
        System.out.println("Data de entrada na empresa: " + this.dataDeEntrada.dataFormatada());
        System.out.println("RG: " + this.rg);
        System.out.println("É funcionario da empresa: " + this.estaNaEmpresa);

    }
}

.

class Empresa {
    String nome;
    String cnpj;
    Funcionario[] empregados;

    void adiciona(Funcionario f) {
        int contador = 0;
        this.empregados[contador] = f;
        contador++;
        if(contador == this.empregados.length) {
            System.out.println("A array está cheio ...");
        }

    }

    void criaArray (int n) {
        this.empregados = new Funcionario [n];

    }
    void mostraEmpregados() {
        for (int i=0; i <= this.empregados.length; i++) {
            System.out.print("Funcionario na pos.: " + i);
            System.out.println(" Nome:" + this.nome);
        }
    }
}

And another class to test the Enterprise class:

package meu.programa;

public class TestaEmpresa {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Empresa empresa = new Empresa();
        empresa.criaArray(5);

        Funcionario f1 = new Funcionario();
        f1.nome = "Victor";

        Funcionario f2 = new Funcionario();
        f2.nome = "Guilherme";

        empresa.adiciona(f1);
        empresa.adiciona(f2);

        empresa.mostraEmpregados();
    }

}
    
asked by anonymous 17.08.2014 / 04:11

3 answers

3

Is this some exercise, or is it to be used in practice?

If it is an exercise, your problem is that when you create an array in empresa.criaArray(5) it will create an array with 5 positions, all of them filled with null . This way, you can find out if there is "space" in the array by testing whether each of these elements is null . The first null you find, use this index to store your Funcionario . If you go through the entire array and find no position with null , it's because the array is full (here you see what's best to do - throw an exception, create a new array ...)

Likewise, your mostraEmpregados is traversing the entire array, so it will be difficult to find null elements. I suggest testing each one of them before trying to print.

Now, if it is to be used in practice, the ideal is to use java.util.ArrayList (or something similar, as pointed out in the response of the carlosfigueira).

    
17.08.2014 / 05:24
2

The place to initialize the employee array would be in the constructor of the Enterprise class, which is called every time an object of this class is created:

class Empresa {
    // ...
    public Empresa(int numeroDeEmpregados) {
        this.empregados = new Funcionario[numeroDeEmpregados];
    }
    // ...
}

Now this solution is a little restricted - when the company is created you need to know the number of employees, but this is not common. What's more, a company can have a variable number of employees (fired / hired), so the array is not the most appropriate data structure for this - java arrays have fixed size. One suggestion is to use a List (for example, ArrayList ) that can grow dynamically depending on the need.

    
17.08.2014 / 05:19
2
for (int i=0; i <= this.empregados.length; i++) {
    System.out.print("Funcionario na pos.: " + i);
    System.out.println(" Nome:" + this.nome);
}

@Victor, the this you use in line 3 of the display method, refers to the instance of the class itself, ie the reference will print the name of the company, while, I believe, what you expect is the name of the employee. The correction would be System.out.println(" Nome:" + empregados[i].nome); because it thus refers to the correct instance of the function that is contained in the vector, through the for counter.

I believe this would work!

I put your code to work here, and it returned what I was really expecting ... It prints Funcionario na pos.: i Nome: Nome dado à empresa . After this I realized that your method is shown Employers is a little bit wrong. Note, the length value, or even the length() function, if they are List s, returns the # of elements in the array, however, the vectors in general treat the position of their element from position 0. So, if the array contains 1 element, it will have length = 1, and position = 0. As you put in the method, would give an ArrayIndexOutOfBounds, since it would try empregados[5] where 5 is the length of the vector, while vector could only go up to 4, since it would have values of 0, 1, 2, 3 and 4. So the right thing would be to put < instead of <= .

No more is this. As I know it's an exercise, I know that you will use Lists so do not hurry. I would advise, as the above mentioned people, initialize the vector size in the constructor itself, would be more coherent, instead of calling a method that would zero the vector and would put a new clean vector in place, losing the previous values ... Tái, it would be nice for you to make this criaArray method a expandeArray by creating a new array, and copying the old values to the new one! It would also be nice if the mostraEmpregados() called direct the mostra() of the class employee, therefore you would have the complete information of the officials.

    
12.11.2014 / 11:55