Vector and Classes, sorting and populating vector in Java

3

I'm doing an exercise that you need to create a vector of 10 names, and you have 3 methods. The first method is called registerName , and has the function to fill in the vectors, the second method is called sortName that returns a String (which is meant to draw names), and the third method called displayName , whose function is to display the names that have been drawn. I have some doubts about the 3 methods, if you can help me I will thank you, I will put below parts of the code, at least the structure of it.

Here is the code with only one scope:

package Testes;

public class ClasseSorteio {

    public static void main(String[] args) {        
         String nomeSorteado[] = new String[10];




    }

    public void cadastrarNome() {
    for(int i = 0; i < 10; i++) {

    }

    }

    public String sortearNome(String nome) {
        for(int i = 0; i < 10; i++) {


        }

        return nome;
    }


    public void exibirNome() {


    }

}

In the main class I will have to use JOptionPane, and I will have to have 3 buttons: Name, Sorte Name and Show Name , each button calling a method.

I'm really sorry, if the code does not have anything at all ready, I'm really in doubt about this part of the 3 methods, so I started learning about methods now.

Thanks for the patience with this my post is not tidy.

    
asked by anonymous 27.11.2015 / 04:24

1 answer

2

The truth is that you did not give enough information to solve your problem, but it's been almost a year that you've posted this problem, and I'll try to give you an answer.

Arrays Indexing

It is important to mention that an array is a content store that stores multiple values. To get and set a value in the array it is necessary to refer to the position of the array where this value is / will be.

In java arrays are indexed from 0. The syntax for indexing an array is using [] . So, to get the first element of the array the statement is arr[0]

Array initialization

Java arrays can be initialized in many ways, like all other types, can be initialized in two ways:

With constant values

String nomeSorteado[] = new String[10]{
  "Pessoa1", "Pessoa2", "Pessoa3", "Pessoa4", "Pessoa5",
  "Pessoa6", "Pessoa7", "Pessoa8", "Pessoa9", "Pessoa10"
};

With dynamic values

String nomeSorteado[] = new String[10];
for(int i = 0; i < nomeSorteado.length; ++i){
   nomeSorteado[i] = <valor dinamico vai aqui>
}

These dynamic values can be obtained in several ways: User input, randomly generated values read from a data source (files, database), and so on.

Now we can solve your problem, I will resove it using constant values.

public class ClasseSorteio {
    //Nota que tive que criar um campo para poder acessar ao array em todos os metodos
    private String[] nomeSorteado = new String[10]{
      "Pessoa1", "Pessoa2", "Pessoa3", "Pessoa4", "Pessoa5",
      "Pessoa6", "Pessoa7", "Pessoa8", "Pessoa9", "Pessoa10"
    };

    public static void main(String[] args) {        
        ClasseSorteio sorteio = new ClasseSorteio();
        sorteio.cadastrarNome();
        String nome = sorteio.sortearNome();
        sorteio.exibirNome(nome);
    }

    public void cadastrarNome() {
        //não faz nada porque estou a usar valores constantes
    }

    //Nota que eu removi o parametro. 
    //Se o seu objetivo é sortear um nome no array aleatoriamente, 
    //não faz sentido passar o nome como parametro
    public String sortearNome() {
        Random r = new Random();
        int idx = r.nextInt(nomeSorteado.length);
        return nomeSorteado[idx];
    }

    //Nota que eu acrescentei o parametro.
    //Para mostrar um nome é preciso saber qual é o nome...
    public void exibirNome(String nome) {
        System.out.println(nome);
    }
}
    
27.07.2016 / 16:36