Vector and Classes, drawing and filling vector in Java

Asked

Viewed 826 times

3

I am doing an exercise that is necessary to create a vector of 10 names, and that has 3 methods. The first method is called register,and has the function to fill the vectors, the second method is called draw which returns a String(which aims to draw names), and the third method called display which has the function of displaying the selected names. I’m having certain doubts about the 3 methods,.

Here is the code only with a scope same :

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, : Register Name,Draw Name and Display Name,each button calling a method.

OBS :I’m really sorry,if the code has practically nothing ready,I’m really having doubts in this part of the 3 methods,why I started now to learn about methods.

Already thank you for having the patience with this my post anything tidy.

  • Hello, @Falino. Welcome. It seems that your question is not about methods or classes but about algorithms. I suggest you ask one question at a time about the things you doubt.

  • Hello friend, yes indeed the doubt is about vector.In the course of Programming that I’m doing, we learn vector and matrix very little even, we focus more on this part of Class and Methods. It is a professional course anyway, but still it was little time to learn vector,and because of this I am with many doubts.

  • So, @Falion. I suggest you ask a question for every question you have, so that your question fits the format of the Portuguese OS. For example: "How to draw an element from an array". This way you’ll even find that there are already solutions to your questions right here in the OS.

1 answer

2


The truth is you haven’t given enough information to solve your problem, but it’s been almost a year since you posted this problem, and I’ll try to give you an answer.

Indexing of arrays

It is important to mention that an array is a contentar that stores several values. To get and define a value in the array you need to specify the position of the array where that 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 instruction is arr[0]

Initialization of arrays

Java arrays can be initialized in various ways, similar to 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 solve 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);
    }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.