Pass variable value between Classes

Asked

Viewed 1,494 times

3

I would like to pass the values stored in the variables of the Parte1 to the Parte2 to validate the highest entered value.

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;
            }
        }

1 answer

4


This code is very confusing and larger than it should be. There seems to be also inconsistent use of static, in your case probably everything should be static in "part 2" and then you wouldn’t even need to create an instance in "part 1". I actually see a lot of unnecessary things in the code. I’ll just concentrate on your question.

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

There are several ways to do this. I’m going to put the two main.

You can make the method determineMaior() receive parameters too, 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);
}

And of course the call on main() would be:

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

I put in the Github for future reference.

Perhaps it would be more interesting to use one array or a collection to store these values instead of several variables. Perhaps you should receive a array as a parameter or at least varargs. And then I’d use a loop to check which one is bigger instead of so many ifs. This way would be shorter and more flexible.

  • Friend, thanks for the help, what I want is to make a program that sees the largest number among 10 values typed by the user, I am starting now to work with LOO, I have always worked with Front-end development so I do not yet master Java, but I’m training for this... Again Thanks!

Browser other questions tagged

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