Use the same method to count one option at a time (Java, eclipse)

0

Hello, this is my first question and my teacher asked me for homework for an exam that we did with the following program in java (we use eclipse mars):

public static void main(String[] args) 
    {
        System.out.println("Total de Alumnos: ");
        int Alu=Leer.datoInt();

        Grupo [] al=new Grupo [Alu];

        captura(al);
        imp(al);
        cal(al);
        imp2(al);
        r(al);

    }

    public static void captura(Grupo [] A)
    {
        String NombreAlumno, Carrera;
        int Semestre, Opcion, Calificacion1, Calificacion2, Calificacion3,total = 0;

        for(int i=0;i<A.length;i++)
        {
        System.out.println("Nombre del alumno: ");
        NombreAlumno=Leer.dato();

        do{
        System.out.println("Carrera: (S= Sistemas, T= Tics)");
        Carrera=Leer.dato();
        }while(!Carrera.equals("S")&&!Carrera.equals("s")&&!Carrera.equals("t")&&!Carrera.equals("T"));

        do{
        System.out.println("Semestre: ");
        Semestre=Leer.datoInt();
        }while(Semestre <1||Semestre>12);

        do
        {
        System.out.println("Tipo: (1=Normal, 2=RC, 3=Especial)");
        Opcion=Leer.datoInt();
        }while(Opcion<1 || Opcion>3);

        System.out.println("Calificacion 1: ");
        Calificacion1=Leer.datoInt();

        System.out.println("Calificacion 2: ");
        Calificacion2=Leer.datoInt();

        System.out.println("Calificacion 3: ");
        Calificacion3=Leer.datoInt();
        A[i]=new Grupo(NombreAlumno,Carrera,Semestre,Opcion,Calificacion1,Calificacion2,Calificacion3,total);
        }

    }

    public static void imp(Grupo [] A)
    {
        String O=null,C = null;

        for(int i=0;i<A.length;i++)
        {
            if(A[i].getCarrera().equals("S"))
                C="Sistemas";
            if(A[i].getCarrera().equals("s"))
                C="Sistemas";
            if(A[i].getCarrera().equals("T"))
                C="Tics";
            if(A[i].getCarrera().equals("t"))
                C="Tics";
            if(A[i].getOpcion()==1)
                O="Normal";
            if(A[i].getOpcion()==2)
                O="RC";
            if(A[i].getOpcion()==3)
                O="Especial";

        System.out.println("Nombre del alumno: " +A[i].getNombreAlumno()+ ". Carrera: "+C+". Semestre " +A[i].getSemestre()+". Tipo: " +O+ ". Calificacion1: "+A[i].getCalificacion1()+". Calificacion2: "+A[i].getCalificacion2()+". Calificacion3: "+A[i].getCalificacion3()+".");
        System.out.println();

        }
    }

    public static Grupo [] cal(Grupo [] p)
    {
        for(int i=0;i<p.length;i++)
        {
            p[i].setPromedio((p[i].getCalificacion1()+p[i].getCalificacion2()+p[i].getCalificacion3())/3);
        }

        return p;
    }

    public static void imp2(Grupo [] imp)
    {
        for(int i=0;i<imp.length;i++)
        System.out.println("Nombre del alumno: "+imp[i].getNombreAlumno()+". Promedio: "+imp[i].getPromedio());

    }
.......

Well let us make a method to count the students that are special, Rc and normal. but you will use that same method to count 1 by 1 each time we call the method, example: first call the method to count the specials after calling the same method but now count the Rc and once again call the same method to count the normal . and to print in the main the truth for more than I try to think how to do it I do not achieve it

    
asked by JesusEnrique 02.03.2017 в 05:35
source

1 answer

5

Good morning,

If you have to implement a single method to count the different options, what you have to do is pass the option to count as a parameter to the method.

It could be something like this:

public static void contarOpcion(Grupo [] alumnos, int opcion)
{
    int count = 0;
    for(int i=0;i<alumnos.length;i++)
    {
       if(alumnos[i].getOpcion() == opcion)
       {
           count++;
       }
    }

    String O = null;
    if(opcion == 1)
        O="Normal";
    if(opcion == 2)
        O="RC";
    if(opcion == 3)
        O="Especial";

    System.out.println("Existen un total de " + count + " alumnos con la opción " + O);
}

If I have not understood you correctly, this could be a solution to your problem. I hope I have helped you!

    
answered by 02.03.2017 в 08:17