Calling element of a method in another method

0

When you run the program, in the "exibirVetores(int[] vet, int[] vetInvertida)" method, the 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 +".");
    }
}
    
asked by anonymous 17.08.2017 / 21:46

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 does not make much sense to declare the Scanner as an attribute of your class, it can be deleted after using the obterNumeros() method which is the only place where it appears. It also does not make much sense to declare the variables as static unless you intend to create those global variables. I suggest you read: What is the use of a static or final variable in java? .

Furthermore if you get everything by parameter in your methods you would not need the attributes of the class.

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 mean the class attribute) as an array of 5 numbers. If you want something more dynamic then I suggest using List or any of your implementations of their 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 that were declared as an 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 the reason for the encapsulation here.

    
18.08.2017 / 16:20