Manipulation of Arraylist

Asked

Viewed 120 times

-1

Good morning Friends, I am having a lot of difficulties in making this application work, I am not able to calculate the days of absence of the students, my difficulty is in the class Agenda in the method marcarFalta(int getMatricula, int mes, int dia)because I have to get the student’s license plate and the day of the absence in that month. Thanks in advance.

package Gerenciar_frequencia;
public class Principal {
public static void main(String[] args) {
    Turma turma = new Turma("Est. de dados","A1","20191");
    turma.inserirAluno(new Aluno("Anselmo",111));
    turma.inserirAluno(new Aluno("Pedro",222));
    turma.inserirAluno(new Aluno("Joao",333));
    turma.imprimir();
    System.out.println("------------------------------------------");
    Pauta pauta = new Pauta(3,6,turma);
    pauta.marcarFalta(111, 3, 5);
    pauta.marcarFalta(111, 3, 12);
    pauta.marcarFalta(111, 3, 21);
    pauta.marcarFalta(111, 3, 26);
    pauta.marcarFalta(111, 4, 2);
    pauta.marcarFalta(111, 4, 4);
    pauta.marcarFalta(111, 4, 11);
    pauta.marcarFalta(111, 4, 23);
    pauta.marcarFalta(111, 4, 25);
    pauta.marcarFalta(111, 4, 30);
    pauta.marcarFalta(111, 5, 7);
    pauta.marcarFalta(111, 5, 23);
    pauta.marcarFalta(222, 3, 12);
    pauta.marcarFalta(222, 3, 21);
    pauta.marcarFalta(222, 4, 9);
    pauta.marcarFalta(222, 4, 16);
    pauta.marcarFalta(222, 4, 25);
    pauta.marcarFalta(222, 4, 30);
    pauta.imprimir();

public class Turma {
    static String nomeTurma;
    String codigo;
    String periodo;
    ArrayList<Aluno> alunos = new ArrayList<>();
    public Turma(String nomeTurma, String codigo, String periodo) {
        this.nomeTurma = nomeTurma;
        this.codigo = codigo;
        this.periodo = periodo;
    }
    public void inserirAluno(Aluno a) {
        alunos.add(a);
    }
    public static String getNomeTurma() {
        return nomeTurma;
    }
    public void imprimir() {
        for (Aluno b: alunos) { 
            System.out.println(b);
        }
    }
 }
 public class Aluno {
    String nome;
    int matricula;
    public Aluno(String nome, int matricula) {
        this.nome = nome;
        this.matricula = matricula;
    }
    public String toString(){
       return "Nome: " + this.nome + ", Matricula: " + this.matricula;
    }
    public int getMatricula() {
       return matricula;
    }
}
public class Pauta {
    int mesInicial;
    int mesFinal;
    public Pauta(int mesInicial, int mesFinal, Turma turma) {
        this.mesInicial = mesInicial;
        this.mesFinal = mesFinal;
    }
    public void marcarFalta(int getMatricula, int mes, int dia) {
        int i=0;
      for (Aluno falta : alunos){
          if (falta.getMatricula==getMatricula && falta.dia==dia){
              i++;
          }
      }
      return i;
    }
    public void imprimir() {
        System.out.println("Mês inicial: "+this.mesInicial+ ",Mês Final: "+ this.mesFinal+",Turma: "+ Turma.getNomeTurma());

    }
}

The Exit would be;

 Matricula: 111,  faltas:12;

 Matricula: 222,  faltas:6;

That is the mistake:

Exception in thread "main" java.lang.Error: Unresolved 
compilation problems: alunos cannot be resolved to a variable getMatricula 
cannot be resolved or is not a field getMatricula cannot be resolved to a 
variable dia cannot be resolved or is not a field at 
Gerenciar_frequencia.Pauta.marcarFalta(Pauta.java:12) at 
Gerenciar_frequencia.Principal.main(Principal.java:11)
  • What is the problem that is occurring? any error? or the output is incorrect?

  • I took a look at your code, there are many errors.

  • Felipe L. Constante. Errors you found are all related to the Agenda class.

2 answers

1


Well... I commented on your code to give you a north to fix the problem, because it is quite basic. I hope that with this I can help you.

public void marcarFalta(int getMatricula, int mes, int dia) {
        int i=0;
      for (Aluno falta : alunos){ // De onde vem o "alunos"?
          if (falta.getMatricula() == getMatricula && falta.dia == dia){ //O falta.da não existe, de onde ele vem?
              i++;
          }
      }
      return i; // Porque você está retornando o inteiro i, se sua classe é void?
    }

public void imprimir() {
            System.out.println("Mês inicial: " + this.mesInicial + ",Mês Final: " + this.mesFinal + ",Turma: "
                    + Turma.getNomeTurma()); //Você precisa instanciar esse cara.

        }

Another thing, if you’re using internal classes, you need to make them static. I advise you to do a good search on basic java and object-oriented programming.

Fix this and your code will compile

I hope I’ve helped.

  • Felipe L. Constant, thanks friend for the remarks, I will work on them.

  • 1

    If there are any questions, be sure to ask.. There is a whole community willing to help.. :)

1

Error may be occurring due to error in variable declaration getMatricula received per parameter:

Error: if (falta.getMatricula==getMmatricula && falta.dia==dia)

Correction: if (falta.getMatricula() == getMatricula && falta.dia == dia)

  • 1

    Hello. this is not an answer but a comment, there is the option to insert a comment in the question.

  • 1

    Right, but unfortunately I don’t have the privilege to comment on a post yet. I could explain better about the problem?

  • I recommend the reading

  • 2

    Caio, I went through this same problem. And I understand the community’s concern about this and they are correct. As much as we have good intentions, there are many that do not have and end up polluting. So, if you’ll allow me to give you a suggestion: wait for the community staff to question, until you have the necessary points to be able to comment.. and it’s not that far.. :)

  • Hello friends, I can not compile this code, is giving error in the line markerFalta of the class Staff, I did not put to print yet the faults I am only with difficulty in picking up the license plate and the day of the missing and go counting.

  • What is the error? Put the error that is giving.

  • Caio Menezes. I fixed getMatricula , but the error continues, thanks friend for the guidance.

  • this is the error:Exception in thread "main" java.lang.Error: Unresolved Compilation problems: students cannot be resolved to a variable getMatricula cannot be resolved or is not a field getMatricula cannot be resolved to a variable dia cannot be resolved or is not a field at Gerenciar_frequencia.Pauta.marcarFalta(Staff.java:12) At Gerenciar_frequencia.Principal.main(Home.java:11)

Show 3 more comments

Browser other questions tagged

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