Arraylist of objects, change the value of an attribute?

1

My zoo needs to add animals, for that the user writes:

  • Number of different animals that the zoo will have. (ex: 3)
  • Name of different animals that the zoo will have (ex: Elephant, gorilla, tiger)
  • Amount of animals that will be added to the zoo (ex: 20 animals)

A number of objects (of the animal class) that the user wrote are created. (ex: 10 elephants, 5 gorillas, 5 tigers) and they are stored in an arraylist of objects.

public class Animal {
String nombre;
double tamaño; 
}

This I have done, what I want to do is change the value of the size attribute of all animals that share the same name as the object arraylist.

Example: all objects with attribute name ="tiger", change the value of the attribute named size to 10. That is, all objects "tiger" will have size 10. All name "gorilla" objects will have size 15, for example.

public static void main(String args[]) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

    System.out.println("Escribe la cantidad de animales distintos que quieres agregar al zoologico: ");
    int animalesDistintos = Integer.parseInt(br.readLine());

    String[] nombresAnimales = new String[animalesDistintos];

    System.out.println("Escribe el nombre de los animales que quieres agregar al zoologico: ");
    for(int x=0; x<animalesDistintos; x++)
    {
        System.out.print(x+">> ");
        String nombreAnimal = br.readLine();
        nombresAnimales[x] = nombreAnimal;
    }

    System.out.println("Escribe la cantidad de animales que quieres agregar al zoologico: ");
    int cantidadAnimales = Integer.parseInt(br.readLine());

    ArrayList<Animal> Animales = new ArrayList<>();
    for(int y=0; y<cantidadAnimales; y++)
    {
        Animal nuevoAnimal = new Animal(nombresAnimales[(int) (Math.random() * nombresAnimales.length)]);
        Animales.add(nuevoAnimal);
    }
    }
    
asked by TheDog 04.11.2018 в 06:40
source

2 answers

0

To change the value of the attributes of an object you need getters and setters. Here is a link where it is explained: link

Basically they are methods that obtain the value of the object's attribute and allow you to change that value.

In your case it would be something like this: Animals.get (0) .set Size (Double Size);

I hope it serves you

    
answered by 04.11.2018 в 22:46
-1

You can condition it in the constructor with a switch

    public class Animal {
      String nombre;
      double tamaño;

      public Animal(String nombre) {
         this.nombre = nombre
         switch (nombre) {
            case 'tigre' :
              this.tamaño = 10;
              break;
           case 'otro_animal' :
             this.tamaño = 5;
             break;
         }
    }

Thus at the moment of creating the animal object Animal animal = new Animal('tigre'); the constructor will assign the variable size in automatic

As an extra tip, never put a variable such as the ñ, since it can cause problems when compiling, you can change the name to tamanio for example

    
answered by 05.11.2018 в 04:03