Calling element of one method in another method

Asked

Viewed 250 times

0

When I run the program, in the method "exibirVetores(int[] vet, int[] vetInvertida)", as Strings "inverted" and "normal" are empty (null).

public class metodos02 {

    public static Scanner sc = new Scanner(System.in);
    int vet[];
    static String normal;
    static String invertida;

    public static void obterNumeros(int[] vet) {
        System.out.println("Informe 5 números: ");
        for (int i = 0; i < vet.length; i++) {
            vet[i] = sc.nextInt();
        }
    }

    public static void inverterOrdemNumeros(int[] vet, int[] vetInvertida) {
        vetInvertida[0] = vet[4];
        vetInvertida[1] = vet[3];
        vetInvertida[2] = vet[2];
        vetInvertida[3] = vet[1];
        vetInvertida[4] = vet[0];
        invertida = (vetInvertida[0] + ", " + vetInvertida[1] + ", " + vetInvertida[2] + ", " + vetInvertida[3]
                + ", " + vetInvertida[4]);
        normal = (vet[0] + ", " + vet[1] + ", " + vet[2] + ", " + vet[3] + ", " + vet[4]);
    }

    public static void exibirVetores(int[] vet, int[] vetInvertida) {
        System.out
                .println("Números informados: "+normal+". Número em ordem inversa: "+ invertida +".");
    }
}
  • What is the order of execution of your methods? you must first call the inverterOrdemNumeros() and then the exibirVetores()

1 answer

0


Your code is very much like a procedural style of programming, there are some things that need to be adapted. If it is your intention to disregard some comments.

public static Scanner sc = new Scanner(System.in);
int vet[];
static String normal;
static String invertida;

It doesn’t make much sense to declare Scanner as an attribute of your class, it can be deleted after using the method obterNumeros() which is the only place he shows up. Nor does it make much sense to declare variables as Static unless you intend to create these global variables. I suggest you read: What is the use of a static or final variable in java?.

Also if you receive everything by parameter in your methods you would not need the class attributes.

public static void obterNumeros(int[] vet) {
    System.out.println("Informe 5 números: ");
    for (int i = 0; i < vet.length; i++) {
        vet[i] = sc.nextInt();
    }
}

Here you use vet.length without having declared vet (I refer to the class attribute) as an array of 5 numbers. If you want something more dynamic I suggest using the List or any of its implementations.

public static void inverterOrdemNumeros(int[] vet, int[] vetInvertida) {
        vetInvertida[0] = vet[4];
        vetInvertida[1] = vet[3];
        vetInvertida[2] = vet[2];
        vetInvertida[3] = vet[1];
        vetInvertida[4] = vet[0];
        invertida = (vetInvertida[0] + ", " + vetInvertida[1] + ", " + vetInvertida[2] + ", " + vetInvertida[3]
                + ", " + vetInvertida[4]);
        normal = (vet[0] + ", " + vet[1] + ", " + vet[2] + ", " + vet[3] + ", " + vet[4]);
    }

Here you are manipulating variables received in the method signature, not those declared as attribute of your class.

The correct code would be the following:

public class metodos02 {
    // Atributos de classe encapsulados e inicializados
    private int vet[] = new int[5];
    private int vetInvertida[] = new int[5];
    private String normal, invertida;

    public void obterNumeros(){
        Scanner sc = new Scanner(System.in);
        System.out.println("Informe 5 números: ");
        for (int i = 0; i < 5; i++) {
            vet[i] = sc.nextInt();
        }
    }

    public void inverterOrdemNumeros() {
        vetInvertida[0] = vet[4];
        vetInvertida[1] = vet[3];
        vetInvertida[2] = vet[2];
        vetInvertida[3] = vet[1];
        vetInvertida[4] = vet[0];

        this.invertida = Arrays.toString(vetInvertida);
        this.normal = Arrays.toString(vet);
    }

    public void exibirVetores() {
        System.out.println("Números informados: "+normal+".\nNúmeros em ordem inversa: "+ invertida +".");
    }  
}

In the main method:

public static void main(String[] args) {
    metodos02 m = new metodos02();
    m.obterNumeros();
    m.inverterOrdemNumeros();
    m.exibirVetores();
}

Output:

run:
Informe 5 números: 
1
2
3
4
5
Números informados: [1, 2, 3, 4, 5].
Números em ordem inversa: [5, 4, 3, 2, 1].

See also why encapsulation here.

Browser other questions tagged

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