How to record a student’s grades using the List array

Asked

Viewed 912 times

0

I need to add notes to certain students (each student has several grades and each grade belongs to a discipline), but as I am using Ist arrayand have little knowledge in java I am well lost in passing parameters and how to add new values using the function registrarNota.

Could someone explain what I should do.

PS(I cannot use Extends)

Student class:

package auladia24;

import java.util.ArrayList;

public class Aluno {
    private String nome;
    private String ra;
    private ArrayList<Nota> notas = new ArrayList(); // tipo da array e o nome da array

    public Aluno(String nome, String ra){
        this.nome = nome;
        this.ra = ra;
    }

    public String getNome (){
        return this.nome;
    }

    public String getRa(){
        return this.ra;
    }

    public ArrayList getNotas(){
        return this.notas;
    }

    public Nota registrarNota(double valor, Disciplina nomeDisciplina){

    }
}

Class Note:

package auladia24;

public class Nota {
    private double valor ;
    private Disciplina nomeDisciplica;

    public void  nota (double nota, Disciplina nomeDisciplina){
        this.valor = valor;    
        this.nomeDisciplica = nomeDisciplica;
    }

    public double getNota(){
        return this.valor;
    }

    public Disciplina getDisciplina(){
        return this.nomeDisciplica;
    }

}

Classe Disciplina:

package auladia24;

public class Disciplina {
    private String nome;

    public Disciplina (String nome){
        this.nome = nome;
    }

    public String getNome(){
        return this.nome;
    }
}
  • You can use another way to store that is not arraylist, such as map?

  • I believe so Diego

  • If I understand correctly, you want to store grades and discipline in the student so that you can relate the grade to the discipline, that’s right?

  • That’s exactly what

1 answer

1

From what I understand you need to use the method registrarNota right? He asks for a double and an object Discipline.

public Nota registrarNota(double valor, Disciplina nomeDisciplina)
{
    //primeiro checa se ja existe a nota
    for(Nota n : notas)
    { 
        if(n.getDisciplina().getNome().equals(objetoDisciplina.getNome()))
        {
            n.setNota(valor); 
            return n;
        }
    }
    Nota n = new Nota(valor, nomeDisciplina); //cria a nova nota
    notas.add(n); //adiciona ela no arraylist
    return n; //retorna a nota criada
}

It is necessary to add the method setNota in class Nota

public void setNota(double valor)
{
    this.valor = valor
}

To call her would simply be aluno.registrarNota(nota, objetoDisciplina);

  • I thought that too, but there’s a problem, to update a note, will create the same discipline? In this way, the same discipline will get several different grades;

  • Maybe if you edit the answer with some way to check if the discipline already exists, and just add to the existing value of the note, it would be more complete and without the bug I quoted.

  • Just need to navigate inside your arraylist notas searching for the notes that are there and check if any of them have the name of the discipline equal to the new one being added.

  • I understand, so I suggested you to exemplify this, the OP may not know this detail, Besides making your answer complete :)

  • I went to try to reproduce his note in my code but when I pass the values he does not accept the discipline type . Now I’m in doubt whether my Note builder is wrong or not

  • And it really is my constructor this like void however I can’t pass a parameter like Discipline

Show 1 more comment

Browser other questions tagged

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