Arrays de objetos JAVA
EJEMPLO: CARRERA CLASES Clase Corredor Clase Carrera Clase AdministraCarrera
public class Corredor  { private int nroCorredor; private String nomCorredor; private int tiempo; public int getNroCorredor()  { return nroCorredor; } public String getNomCorredor()  { return nomCorredor; } public int getTiempo()  { return tiempo; } public Corredor(int nroCorredor,String nomCorredor,int tiempo ) { this.nroCorredor=nroCorredor; this.nomCorredor=nomCorredor; this.tiempo=tiempo; } public String toString() { return ("Corredor: " + this.nroCorredor + "-" + this.nomCorredor + " Tiempo: " + this.tiempo); } }
public class Carrera  { private Corredor [] vecCorredores; private int cantCorredores; //Constructor de la clase Carrera public Carrera() { //Setea en 0 el contador de corredores this.cantCorredores=0; //Crea el vector vecCorredores this.vecCorredores=new Corredor[100]; } public int getCantCorredores() { return this.cantCorredores; } public void agregarCorredor(int nroCorredor,String nomCorredor,int tiempo) { Corredor c= new Corredor(nroCorredor, nomCorredor, tiempo); this.vecCorredores[cantCorredores]=c; cantCorredores++; } public Corredor [] mostrarVectorCorredores() { return (this.vecCorredores); } public double promedioTiempos() { double suma; int i; suma=0; for (i=0;i<this.cantCorredores;i++) { suma=suma+vecCorredores[i].getTiempo(); } return(suma/this.cantCorredores); } }
import java.util.*; public class AdministraCarrera  { public static void main(String[] args)  { Scanner entrada=new Scanner(System.in); entrada.useDelimiter(System.getProperty(&quot;line.separator&quot;)); //Declaraciones int nroCorredor, tiempo, i; String nomCorredor, resp; //Creacción del objeto car del tipo clase Carrera Carrera car=new Carrera(); //Ingreso de datos de todos los corredores System.out.print(&quot;Hay datos para cargar? (S/N)&quot;); resp=entrada.next(); while(resp.equalsIgnoreCase(&quot;S&quot;)) { System.out.print(&quot;Ingrese nro de corredor: &quot;); nroCorredor=entrada.nextInt(); System.out.print(&quot;Ingrese nombre de corredor: &quot;); nomCorredor=entrada.next(); System.out.print(&quot;Ingrese tiempo de la carrera: &quot;); tiempo=entrada.nextInt(); //Pedir agregar este corredor car.agregarCorredor(nroCorredor, nomCorredor, tiempo); System.out.print(&quot;Hay datos para cargar? (S/N)&quot;); resp=entrada.next(); } //Mostrar los datos de todos los corredores utilizando el toString System.out.println(&quot;Datos de todos los corredores utilizando el toString&quot;); for(i=0;i<car.getCantCorredores();i++) { System.out.println(car.mostrarVectorCorredores()[i].toString()); } //Mostrar el tiempo promedio de la carrera System.out.println(&quot;El tiempo promedio de la carrera fue: &quot; + car.promedioTiempos()); } }

Presentación arraysobjetos

  • 1.
  • 2.
    EJEMPLO: CARRERA CLASESClase Corredor Clase Carrera Clase AdministraCarrera
  • 3.
    public class Corredor { private int nroCorredor; private String nomCorredor; private int tiempo; public int getNroCorredor() { return nroCorredor; } public String getNomCorredor() { return nomCorredor; } public int getTiempo() { return tiempo; } public Corredor(int nroCorredor,String nomCorredor,int tiempo ) { this.nroCorredor=nroCorredor; this.nomCorredor=nomCorredor; this.tiempo=tiempo; } public String toString() { return (&quot;Corredor: &quot; + this.nroCorredor + &quot;-&quot; + this.nomCorredor + &quot; Tiempo: &quot; + this.tiempo); } }
  • 4.
    public class Carrera { private Corredor [] vecCorredores; private int cantCorredores; //Constructor de la clase Carrera public Carrera() { //Setea en 0 el contador de corredores this.cantCorredores=0; //Crea el vector vecCorredores this.vecCorredores=new Corredor[100]; } public int getCantCorredores() { return this.cantCorredores; } public void agregarCorredor(int nroCorredor,String nomCorredor,int tiempo) { Corredor c= new Corredor(nroCorredor, nomCorredor, tiempo); this.vecCorredores[cantCorredores]=c; cantCorredores++; } public Corredor [] mostrarVectorCorredores() { return (this.vecCorredores); } public double promedioTiempos() { double suma; int i; suma=0; for (i=0;i<this.cantCorredores;i++) { suma=suma+vecCorredores[i].getTiempo(); } return(suma/this.cantCorredores); } }
  • 5.
    import java.util.*; publicclass AdministraCarrera { public static void main(String[] args) { Scanner entrada=new Scanner(System.in); entrada.useDelimiter(System.getProperty(&quot;line.separator&quot;)); //Declaraciones int nroCorredor, tiempo, i; String nomCorredor, resp; //Creacción del objeto car del tipo clase Carrera Carrera car=new Carrera(); //Ingreso de datos de todos los corredores System.out.print(&quot;Hay datos para cargar? (S/N)&quot;); resp=entrada.next(); while(resp.equalsIgnoreCase(&quot;S&quot;)) { System.out.print(&quot;Ingrese nro de corredor: &quot;); nroCorredor=entrada.nextInt(); System.out.print(&quot;Ingrese nombre de corredor: &quot;); nomCorredor=entrada.next(); System.out.print(&quot;Ingrese tiempo de la carrera: &quot;); tiempo=entrada.nextInt(); //Pedir agregar este corredor car.agregarCorredor(nroCorredor, nomCorredor, tiempo); System.out.print(&quot;Hay datos para cargar? (S/N)&quot;); resp=entrada.next(); } //Mostrar los datos de todos los corredores utilizando el toString System.out.println(&quot;Datos de todos los corredores utilizando el toString&quot;); for(i=0;i<car.getCantCorredores();i++) { System.out.println(car.mostrarVectorCorredores()[i].toString()); } //Mostrar el tiempo promedio de la carrera System.out.println(&quot;El tiempo promedio de la carrera fue: &quot; + car.promedioTiempos()); } }