Problem returning a vector list

Asked

Viewed 48 times

0

I’m doing a Java Project and I’m having a hard time dealing with vectors. In the project I have the following methods to create:

1 - Create a method that receives as parameter the course name and return all students enrolled in this course and also, show all teachers that teach in this course.
2 - Create a method that takes a discipline as a parameter and returns the number of students enrolled in it.
3 - Create a method that takes a student name as a parameter and returns all information about it.
4 - Create a method that returns how many female students there are in the university.

I managed to elaborate almost all, except item 2. I am trying to return information of this method and is not being possible.

Pupil Class

package universidade;

public class Aluno {
    private String nome, curso;
    private int idade, semestre;
    private char sexo;
    private String disciplinas[];
    private float notas[];

    public Aluno() {
    }

    public Aluno(String nome, String curso, int idade, int semestre, char sexo, String[] disciplinas, float[] notas) {
        this.nome = nome;
        this.curso = curso;
        this.idade = idade;
        this.semestre = semestre;
        this.sexo = sexo;
        this.disciplinas = disciplinas;
        this.notas = notas;
    }

    @Override
    public String toString() {
        return "Nome: "+nome+
                " - Curso: "+curso+
                " - Idade: "+idade+
                " - Semestre "+semestre+
                " - Sexo"+sexo
                ;
    }



    /**
     * @return the nome
     */
    public String getNome() {
        return nome;
    }

    /**
     * @param nome the nome to set
     */
    public void setNome(String nome) {
        this.nome = nome;
    }

    /**
     * @return the curso
     */
    public String getCurso() {
        return curso;
    }

    /**
     * @param curso the curso to set
     */
    public void setCurso(String curso) {
        this.curso = curso;
    }

    /**
     * @return the idade
     */
    public int getIdade() {
        return idade;
    }

    /**
     * @param idade the idade to set
     */
    public void setIdade(int idade) {
        this.idade = idade;
    }

    /**
     * @return the semestre
     */
    public int getSemestre() {
        return semestre;
    }

    /**
     * @param semestre the semestre to set
     */
    public void setSemestre(int semestre) {
        this.semestre = semestre;
    }

    /**
     * @return the sexo
     */
    public char getSexo() {
        return sexo;
    }

    /**
     * @param sexo the sexo to set
     */
    public void setSexo(char sexo) {
        this.sexo = sexo;
    }

    /**
     * @return the disciplinas
     */
    public String[] getDisciplinas() {
        return disciplinas;
    }

    /**
     * @param disciplinas the disciplinas to set
     */
    public void setDisciplinas(String[] disciplinas) {
        this.disciplinas = disciplinas;
    }

    /**
     * @return the notas
     */
    public float[] getNotas() {
        return notas;
    }

    /**
     * @param notas the notas to set
     */
    public void setNotas(float[] notas) {
        this.notas = notas;
    }


}

University Class

package universidade;

import javax.swing.JOptionPane;

public class Universidade {

    private Aluno alunos[];
    private Professor professores[];

    public static void main(String[] args) {
        new Universidade();
    }

    public Universidade() {

        dadosDeAlunos_Professores();
        String nomecurso = JOptionPane.showInputDialog(null, "Digite o nome do Curso", "Dados", JOptionPane.INFORMATION_MESSAGE);
        System.out.println(alunosMatriculados(nomecurso));

        // String nomedisciplina = JOptionPane.showInputDialog(null, "Digite o nome do Curso", "Dados", JOptionPane.INFORMATION_MESSAGE);
        //System.out.println(alunosDisciplina(nomedisciplina+"test"));
        alunosDoSexoFeminino();

        String nomealuno = JOptionPane.showInputDialog(null, "Digite o nome do aluno para pesquisar - lo ", "Dados", JOptionPane.INFORMATION_MESSAGE);
        System.out.println(pesquisarAlunos(nomealuno));

        String nomedisciplina = JOptionPane.showInputDialog(null, "Digite o nome da disciplina", "Dados", JOptionPane.INFORMATION_MESSAGE);
        System.out.println(alunosDisciplina(nomedisciplina));

    }

    public void dadosDeAlunos_Professores() {

        alunos = new Aluno[3];
        professores = new Professor[3];

        //dados de vários alunos:
        String disc0[] = {"poo", "tda", "física"};
        float not0[] = {5.0f, 3.0f, 2.0f};
        alunos[0] = new Aluno("Ana", "ADS", 22, 2, 'F', disc0, not0);

        String disc1[] = {"matemática", "tda", "inteligência artificial"};
        float not1[] = {1.0f, 2.0f, 1.0f};
        alunos[1] = new Aluno("Joao", "ADS", 22, 2, 'M', disc1, not1);

        String disc2[] = {"desenho", "projeto 1", "pontes"};
        float not2[] = {5.0f, 5.0f, 5.0f};
        alunos[2] = new Aluno("Maria", "ADS", 20, 2, 'F', disc2, not2);

        //System.out.println(disc0+" "+not0);
    }

    public String alunosMatriculados(String nomecurso) {
        String saida = "";
        for (int i = 0; i < alunos.length; i++) {
            if (alunos[i].getCurso().equalsIgnoreCase(nomecurso)) {
                saida += "" + alunos[i].getNome() + ", ";
            }
        }
        return saida;
    }

    public String alunosDisciplina(String nomedisciplina) {

        String saida = "";
        int cont = 0;
        for (int i = 0; i < alunos.length; i++) {
            if (alunos[i].getDisciplinas().equals(nomedisciplina)) {
                saida += ""+cont++;
            }
        }
        return saida;
    }

    public void alunosDoSexoFeminino() {
        int cont = 0;
        //char genero ='f';
        for (int i = 0; i < alunos.length; i++) {
            //equalsIgnoreCase
            if (alunos[i].getSexo() == 'F' || alunos[i].getSexo() == 'f') {
                cont++;
            }
        }
        System.out.println("Na universidade possui " + cont + " alunas do Sexo Feminino");

    }

    public String pesquisarAlunos(String nomealuno) {
        String saida = "";
        System.out.println("Aluno pesquisado: " + nomealuno);
        for (int i = 0; i < alunos.length; i++) {
            if (alunos[i].getNome().equalsIgnoreCase(nomealuno)) {
                saida += alunos[i].toString();
            }
        }
        return saida;
    }
}

Teacher class

package universidade;

public class Professor {
    private String nome;
    private int idade;
    private char sexo;
    private double salario;
    private String disciplinas[];

    public Professor() {
    }

    public Professor(String nome, int idade, char sexo, double salario, String[] disciplinas) {
        this.nome = nome;
        this.idade = idade;
        this.sexo = sexo;
        this.salario = salario;
        this.disciplinas = disciplinas;
    }

        @Override
        public String toString() {
            return ""+nome+
                   " "+idade+
                   " "+sexo+
                   " "+salario+
                   " "+disciplinas; //To change body of generated methods, choose Tools | Templates.
        }



        /**
         * @return the nome
         */
        public String getNome() {
            return nome;
        }

        /**
         * @param nome the nome to set
         */
        public void setNome(String nome) {
            this.nome = nome;
        }

        /**
         * @return the idade
         */
        public int getIdade() {
            return idade;
        }

        /**
         * @param idade the idade to set
         */
        public void setIdade(int idade) {
            this.idade = idade;
        }

        /**
         * @return the sexo
         */
        public char getSexo() {
            return sexo;
        }

        /**
         * @param sexo the sexo to set
         */
        public void setSexo(char sexo) {
            this.sexo = sexo;
        }

        /**
         * @return the salario
         */
        public double getSalario() {
            return salario;
        }

        /**
         * @param salario the salario to set
         */
        public void setSalario(double salario) {
            this.salario = salario;
        }

        /**
         * @return the disciplinas
         */
        public String[] getDisciplinas() {
            return disciplinas;
        }

        /**
         * @param disciplinas the disciplinas to set
         */
        public void setDisciplinas(String[] disciplinas) {
            this.disciplinas = disciplinas;
        }

    }
  • You have already implemented the number 2, the method alunosDisciplina already does. Instead of concatenating, just increase 1 each time you locate student.

  • Yeah, I’ve done that before. But even so, when trying to print this information on the screen using System.out.println, it shows me nothing.

  • Gabriel, your Student class doesn’t have mechanisms that locate just one discipline, you’re returning an array of disciplines and checking if it’s equal to the name, so it’ll never work. I recommend making Discipline abstract as well, rather than using string array, or creating some way to search for disciplines in the array that Voce created in the Student class

1 answer

0

I believe your mistake may be related to the type of method, since it must be of the type INT which is the number of disciplines that will be returned.

public int alunosMatriculados(String nomecurso) {
    int numAlunos = 0;
    for (int i = 0; i < alunos.length; i++) {
        if (alunos[i].getCurso().equalsIgnoreCase(nomecurso)) {
            numAlunos++;
        }
    }
    return numAlunos;
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.