Pass variables value between Classes

3

I would like to pass the values stored in the variables from part1 to part2 to validate the highest value typed.

PART 1:

public class SEP_06_parte1{
    public static void main(String args[]){
        System.out.print("Digite 10 valores ");

            int y1 = input.nextInt();
            int y2 = input.nextInt();
            int y3 = input.nextInt();
            int y4 = input.nextInt();
            int y5 = input.nextInt();
            int y6 = input.nextInt();
            int y7 = input.nextInt();
            int y8 = input.nextInt();
            int y9 = input.nextInt();
            int y10 = input.nextInt();

        SEP_06_parte2 lernumeros = new SEP_06_parte2();
        lernumeros.determineMaior();

    }
}

PART 2:

public class SEP_06_parte2{
    public void determineMaior(){
        int maiorResultado = maximum(y1, y2, y3, y4, y5, y6, y7, y8, y9, y10);
                System.out.print("Maior Numero: " + maiorResultado);
        }
        public static int maximum( int y1 , int y2, int y3, int y4, int y5, int y6, int y7, int y8, int y9, int y10)
            {
                int maximumValue = y1;
                if ( y2 > maximumValue )
                maximumValue = y2;
                if ( y3 > maximumValue )
                maximumValue = y3;
                if ( y4 > maximumValue )
                    maximumValue = y4;
                if ( y5 > maximumValue )
                    maximumValue = y5;
                if ( y6 > maximumValue )
                    maximumValue = y6;
                if ( y7 > maximumValue )
                    maximumValue = y7;
                if ( y8 > maximumValue )
                    maximumValue = y8;
                if ( y9 > maximumValue )
                    maximumValue = y9;
                if ( y10 > maximumValue )
                    maximumValue = y10;
                return maximumValue;
            }
        }
    
asked by anonymous 06.10.2014 / 16:40

1 answer

4

This code is confusing and much bigger than it should be. There also seems to be inconsistent use of static , in your case everything should probably be static in "part 2" and there would not even need to create an instance in "part 1". I actually see a lot of unnecessary things in the code. I'll just focus on your question.

Do you understand how parameters work? You understand that variables exist only within a scope and that you can only pass their values to another scope through parameters?

There are several ways to do this. I'll put the two main ones.

You can make the determineMaior() method receive parameters as well, so you can pass all variables from "part 1" to it. This way:

public void determineMaior(int y1 , int y2, int y3, int y4, int y5, int y6, int y7, int y8, int y9, int y10){
    int maiorResultado = maximum(y1, y2, y3, y4, y5, y6, y7, y8, y9, y10);
    System.out.print("Maior Numero: " + maiorResultado);
}

Of course the call in main() would be:

lernumeros.determineMaior(y1, y2, y3, y4, y5, y6, y7, y8, y9, y10);

Perhaps it would be more interesting to use an array or some collection to store these values instead of several variables. Maybe you should get an array as a parameter or at least varargs . And then you would use a loop to check which is the largest instead of as many if s. This way it would be shorter and more flexible.

    
06.10.2014 / 16:54