Modular programming in Java, called methods

Asked

Viewed 159 times

1

He needed help to put the smaller and larger method to be "heard" in the main method. The logic of the program is to show the user what is the highest and lowest value of a vector.

CODE:

import java.util.Scanner;

public class L6ex3 {

    public static void main(String[] args) {
        Scanner e = new Scanner (System.in);
        int quantidade = 0;

        System.out.print("Quantos alunos serão cadastradas as idades?");
        quantidade = e.nextInt();

        int v[] = new int [quantidade];
        System.out.print ("Digite as idades");
        for (int i=0; i<quantidade; i++)
            v[i] =e.nextInt();



    }

    public int menorValor (int v[]){
        int menor =0;
        for (int i=0; i<v.length;i++)
            menor = v[i];

        for (int i=0; i<v.length; i++)
            if (v[i]<menor)
                menor =v[i];

        return menor;
    }

    public int maior (int v[]){
        int maior=0;
        for (int i=0; i<v.length;i++)
            if (v[i]> maior)
                maior = v[i];

        return maior;
    }

}

2 answers

3


To call the methods menorValor() and maiorValor() you must put the modifier static in the methods.

public static int menorValor (int v[]){/*...*/}
public static int maiorValor (int v[]){/*...*/}

Then in your method main,just call them normally:

   /* ... */
    System.out.println ("Maior valor: "+ maiorValor(v));
    System.out.println ("Menor valor: "+ menorValor(v));

I also suggest you change the method code menorValor() to the following:

public static int menorValor (int v[]){

    int menor =0;

    for (int i=0; i<v.length;i++)
        if(v[i] < menor)
            menor = v[i]

    return menor;
}
  • Man, it was worth a lot of help. It was just right. As for your suggestion, I had done it this way, but if the user enters with any number greater than 0 it will return 0, because the smallest starts already worth 0. Hugs.

  • In this case, you can start the variable menor with a very high number, for example 10000, because the ideal is that you do not have many loops for unnecessary. And remembering, the best way to thank a good answer is to accept it as "best response" :)

  • I thought of those hypotheses too, really better to give a crumb and put negative than increase a for.. rsrs.. even "computationally" speaking.. Vlw dude, I’m going in. I approved the answer...

1

Never assign any value, even if it is a ZERO. You do not know if the value is higher or lower than the typed.

  • I suggest you always take the first array value as default, like this he is part of that comparison.
  • Or, use the maximum or minimum value supported by the array type. (Integer.MIN_VALUE, Integer.MAX_VALUE)

If you are to call within the main function 'main', you must leave the two Static methods. If you do not want to leave Static, put inside a Class (in my opinion would be ideal).

Browser other questions tagged

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