How to use an array of an object

0

I have a class Alumnos where I declare a Array of three elements:

public class Alumnos extends Persona 
{
   private int numeroExpediente;
   private int nota[];

   public Alumnos()
   {
       numeroExpediente=0;
       int nota[] = new int[3];
   }

   public Alumnos(String nombre,String direccion,String fecha,String sexo, int numeroExpediente, int nota[])
   {
       super(nombre,direccion,fecha,sexo);
       this.numeroExpediente=numeroExpediente;
       this.nota[3]=nota[3];
   }

   public int getNumeroExpediente() {
       return numeroExpediente;
   }

   public void setNumeroExpediente(int numeroExpediente) {
       this.numeroExpediente = numeroExpediente;
   }

   public int[] getNota() {
       return nota;
   }

   public void setNota(int[] nota) {
       this.nota = nota;
   }    
}

I want to use it in the class main with the object Alumnos , but I do not know how to put it to enter three notes:

public class Principal 
{

   public static void main(String[] args) {
       Scanner tecla = new Scanner(System.in);
       Alumnos objAlumnos = new Alumnos();
       Empleados objEmpleados = new Empleados();
       int opcion;

       System.out.println("Datos personales\n");

       System.out.println("\t 1---> Alumnos");
       System.out.println("\t 2---> Empleados");
       System.out.print("\nElija una opcion: ");
       opcion=tecla.nextInt();

       if(opcion==1)
       {
           System.out.println(tecla.nextLine());
           System.out.print("Nombre del alumno: ");
           objAlumnos.setNombre(tecla.nextLine());
           System.out.print("Direccion: ");
           objAlumnos.setDireccion(tecla.nextLine());
           System.out.print("Fecha de nacimiento: ");
           objAlumnos.setFecha(tecla.nextLine());
           System.out.print("Sexo del alumno: ");
           objAlumnos.setSexo(tecla.nextLine());
           System.out.print("Numero de Expediente: ");
           objAlumnos.setNumeroExpediente(tecla.nextInt());
           for(int i=0;i<3;i++)
           {
               System.out.print("Nota 1: ");
               objAlumnos.setNota(tecla.nextInt());//Aqui me da fallo
           }            
       }
   }
}
    
asked by Mario Guiber 22.08.2017 в 14:33
source

4 answers

1

The problem of the error java.lang.ArrayIndexOutOfBoundsException is in the constructor that does not receive parameters of your class Alumnos , since in the not you are initializing the variable nota , if not that you are creating another variable. To solve this error you have to make the following correction:

public class Alumnos extends Persona 
{
   private int numeroExpediente;
   private int nota[];

   public Alumnos()
   {
       numeroExpediente=0;
       nota = new int[3]; // Inicializas la variable notas
   }

   public Alumnos(String nombre,String direccion,String fecha,String sexo, int numeroExpediente, int nota[])
   {
       super(nombre,direccion,fecha,sexo);
       this.numeroExpediente=numeroExpediente;
       this.nota = nota;
   }

   public int getNumeroExpediente() {
       return numeroExpediente;
   }

   public void setNumeroExpediente(int numeroExpediente) {
       this.numeroExpediente = numeroExpediente;
   }

   public int[] getNota() {
       return nota;
   }

   public void setNota(int[] nota) {
       this.nota = nota;
   }    
}

With that you should not give an error.

    
answered by 22.08.2017 / 18:21
source
4

Usually the set of a Collection or Array method is waiting for an Array of the indicated type, so your setNota(int [] nota) method expects you to pass an Array of int as argument, which you should use it from the following way:

 public static void main(String[] args) {
   Scanner tecla = new Scanner(System.in);
   Alumnos objAlumnos = new Alumnos();
   Empleados objEmpleados = new Empleados();
   int opcion;

   System.out.println("Datos personales\n");

   System.out.println("\t 1---> Alumnos");
   System.out.println("\t 2---> Empleados");
   System.out.print("\nElija una opcion: ");
   opcion=tecla.nextInt();

   if(opcion==1)
   {
       System.out.println(tecla.nextLine());
       System.out.print("Nombre del alumno: ");
       objAlumnos.setNombre(tecla.nextLine());
       System.out.print("Direccion: ");
       objAlumnos.setDireccion(tecla.nextLine());
       System.out.print("Fecha de nacimiento: ");
       objAlumnos.setFecha(tecla.nextLine());
       System.out.print("Sexo del alumno: ");
       objAlumnos.setSexo(tecla.nextLine());
       System.out.print("Numero de Expediente: ");
       objAlumnos.setNumeroExpediente(tecla.nextInt());
       int[] notas = new int[3];
       for(int i=0;i<3;i++)
       {
           System.out.print("Nota " + (i+1) + ": ");
           notas[i]=tecla.nextInt();
       }     
       objAlumnos.setNota(notas);
   }
}

In any case you should change the name of the variable to notas since it represents a set of elements type int and not a single element.

    
answered by 22.08.2017 в 15:04
0

The problem you have is that you try to access a position in the array of notes that does not exist.

Arrays in Java and many other programming languages start at 0 through n-1.

In the properties

  

private int note [] = new int [3] instead of private int note []

In the body of your constructor

  

this.nota [2] = note [2] instead of this.nota [3] = note [3]

    
answered by 22.08.2017 в 16:26
0

The problem is in the constructor. Here:

public Alumnos()
{
    numeroExpediente=0;
    //aquí, nota es una variable local, no tu atributo
    int nota[] = new int[3];
}

public Alumnos(String nombre,String direccion,String fecha,String sexo, int numeroExpediente, int nota[])
{
    super(nombre,direccion,fecha,sexo);
    this.numeroExpediente=numeroExpediente;
    //aquí estás obteniendo el 4to elemento del parámetro nota
    //y asignándolo al atributo nota que no ha sido inicializado
    this.nota[3]=nota[3];
}

Either you initialize the array by declaring it as an attribute or you initialize it in each constructor according to what is needed.

Example:

public Alumnos()
{
    numeroExpediente=0;
    //se inicializa el arreglo con 3 elementos como máximo
    //los índices varían entre el 0, 1 y 2
    this.nota = new int[3];
}

public Alumnos(String nombre,String direccion,String fecha,String sexo, int numeroExpediente, int nota[])
{
    super(nombre,direccion,fecha,sexo);
    this.numeroExpediente=numeroExpediente;
    //inicializar el atributo nota como una copia del parámetro nota
    //copiar los elementos del parámetro nota al atributo nota
    this.nota = new int[nota.length];
    for (int i = 0; i <= nota.length; nota++) {
        this.nota[i] = nota[i];
    }
}
    
answered by 22.08.2017 в 18:13