How to save parameters in an object array?

0

I'm doing this program

import java.util.*;
public class gestionDesposito {

public static void main(String[] args) {
    int tamanio=0;
    String identificador="00001";
    double lado=1000;
    double alto=500;
    Scanner sc= new Scanner(System.in);

  System.out.println("Escriba el numero de despositos a crear entre 4 y 10: ");
  tamanio=sc.nextInt();                                                           //Se pone la asignacion de tamaño justo despues por que sino estariamos creando un array de longitud 1
  Deposito[] d=new Deposito[tamanio];
  if(tamanio>=4 && tamanio<=10){
      for(int i=0;i<d.length;i++){
         d[i]=new Deposito(identificador,lado,alto);
          System.out.println(d[i]);
          lado=lado+1000;
          alto=alto+500;
      }

  }

  sc.close();
}

}

When executing this Escriba el numero de despositos a crear entre 4 y 10: 8 Deposito@66d3c617 Deposito@63947c6b Deposito@2b193f2d Deposito@355da254 Deposito@4dc63996 Deposito@d716361 Deposito@6ff3c5b5 Deposito@3764951d

I know that it is because I can not save the parameters in the array correctly, but I do not know how to fix it. My class is this:

public class Deposito {
//variables nativas de la clase
private String identificador;
double litros;
private double capacidad;

//constructor
public Deposito(String identificador, double lado, double altura ){
    this.identificador=identificador;
    capacidad=lado*lado*altura*1000;
}

//metodos
public double getCapacidad(){
    return capacidad;

}
public double getLitros(){

    return litros;

}
public String getIdentificador(){
    return identificador;

}

public boolean estaLleno(){

    return capacidad==litros;
}
public boolean estaVacio(){

    return litros==0;
}   




public double aniadir(double litros){
    double diferencia=capacidad-this.litros;
    if(litros>0){
     if(litros<diferencia){
         this.litros=this.litros+litros;


     }
     else 
        this.litros=capacidad;
    }
    else 
        return -1;

    return this.litros;

}

public double vaciar(){
    double litros;
    litros=this.litros;
    this.litros=0;
    return litros;
}
public double vaciar(double litros){
    double diferencia=this.litros-litros;
    if(litros>0){
     if(diferencia>0){
         this.litros=this.litros-litros;
     }
     else
         return this.litros;
    }
    else
        return -1;

    return this.litros;

}



}
    
asked by iHack 20.10.2017 в 12:50
source

2 answers

2

When you want to print an object saved in an array, you can not print it directly. Those lines that you get when you try to show d [i], are memory addresses where each object is stored.

The correct way is to show each of the attributes of the object that is in the 'i' position:

  for(int i=0;i<d.length;i++){
     d[i]=new Deposito(identificador,lado,alto);
      System.out.println(d[i].getCapacidad());
      System.out.println(d[i].getLitros());
      System.out.println(d[i].getIdentificador());
      lado=lado+1000;
      alto=alto+500;
  }
    
answered by 20.10.2017 в 16:38
0

Actually the values are stored correctly, what happens is that when printing the object you are not printing its content, if not your reference.

System.out.println(d[i]);

// Imprime:
Deposito@66d3c617

To print the content of the object you have two options:

  • You access the object variables through get methods to get their values

    System.out.println(d[i].getIdentificador());
    System.out.println(d[i].getCapacidad());
    
    // Imprime:
    0001
    1000
    
  • Overwrite the toString() method on your object and return a String with the values you want it to return. When you print an object, the toString() method is called by default, and if you have not overwritten it, it returns the reference of the object, as in your case. In the toString () method, you usually return the value of the object's variables, but in it you can return whatever you want.

    public class Deposito {
    
        //variables nativas de la clase
        private String identificador;
        double litros;
        private double capacidad;
    
        //constructor
        public Deposito(String identificador, double lado, double altura ){
            this.identificador=identificador;
            capacidad=lado*lado*altura*1000;
        }
    
        // METODOS GET y SET
        ...
    
        // METODOS DEL OBJETO
        ...
    
        // Sobrescribes el método toString()
        @Override
        public String toString() {
    
            return "Id: " +identificador+ " Capacidad:" +capacidad+ "";
    
        }
    }
    

    When you print the object you will get the following:

    System.out.println(d[i]);
    
    // Imprime:
    Id: 0001 Capacidad: 1000
    
  • answered by 20.10.2017 в 20:53