query about getter and setter

-1

The exercise is of an extreme simplicity, but I want to put a bit of difficulty and do it with POO. I have to ask for 5 numbers and then do an operation with those numbers, there is a condition that the numbers can not be greater than 10. Now, in the class where I declare the numbers I put them value1, value2, value3, value4, value5, the question is that if I want to create a for to ask me the numbers one behind the other, practice a = new practice (); a.setValue1 (read.nextInt ());

How could I do to verify the condition as the numbers are entered, without having to set setValor2, setValor3, etc, since the for would be to simplify the code .. As I said before, it can be done the exercise in a much simpler way, declaring only variables in the same main class, but I am practicing OOP. I await your comments.

    
asked by Guillermo Rubio 05.12.2018 в 00:36
source

2 answers

-1

One thing you could do is replace, in the practical class, the declaration of the five values by an array of int of length 5. With that you would store the five numbers in a single variable.

//Creas el array de int
    private int[] numeros;

    public Practica(){
        //lo inicias con longitud cinco
        numeros = new int[5];
    }

And since you are going to try it in a for, you can do a getter method that returns the array of numbers or a method to which you pass the position and the value and in this way you enter a value (nextInt () ) in a certain position (the value that the iterator takes in the for loop).

//Devulve el array de int numeros
public int[] getNum(){
        return numeros;
    }

//ingresa en la posicion = posicion del array un número = num
    public void ingresaNum(int posicion, int num){
        numeros[posicion] = num;
    }

Already in the for loop you could use either of these two methods.

Scanner teclado = new Scanner(System.in);
        Practica practica = new Practica();
        int numero = 0;
        for(int i=0; i<5;i++){
            numero = teclado.nextInt();
            practica.getNum()[i] = numero;
        }

Or,

Scanner teclado = new Scanner(System.in);
    Tarea practica = new Practica();
    int numero = 0;
    for(int i=0; i<5;i++){
        numero = teclado.nextInt();
        practica.ingresaNum(i, numero);
    }

Finally, remember that if you then have to operate with them, the positions of the numbers in the array begin with 0. Therefore, if you wanted to add the first and third values you should select the position 0 and the 2.

//Si lo haces desde el main
int suma = practica.getNum()[0] + practica.getNum[2];

//Si lo haces desde la clase Practica
int suma = numeros[0] + numeros[2];

Greetings.

    
answered by 05.12.2018 в 06:55
-1

public class Example {

public static void main(String[] args) {
    Scanner leer = new Scanner(System.in);
    practica a = new practica();

    int dato;
    int i = 0;
    // creé esta condición de (i<5) ya que lo que pedías es muy 
    //específico a 5 datos
    while (i < 5) {
        dato = leer.nextInt();
    //en este if se aumenta la variable i 
    //la cual necesita ser 5 o mayor para que el loop termine
    //pero solo se va a incrementar si el dato que damos por teclado
    //es menor o igual a 10, esto para cumplir con la condición que pedías

        if (dato <= 10) {
            a.setValor(dato);
            i++;
        }
    }

}

}

If you try to add more than 5 numbers it will give you an exception since you asked for it very specific to 5 numbers if you want to enter more data you must increase the size of the array or do it in another way

class practica {

private int[] array= new int[5];
private int contador = 0; // el objetivo de esta variable es controlar la 
                       //cantidad de números en el array

public void setValor(int a) {
    array[contador] = a;
    contador++;
}

}

This would be my basic way of solving what you ask for

Greetings

    
answered by 10.12.2018 в 02:56