Linked lists in Java

2

Assuming I have a linked list of Students, where you must register name and note, however this same student may contain more than one note. In my logic, it only registers a student with a single note, as you can see in the code below:

Class responsible for Getters and Data Setters (the given "number" would be the identified number of each student)

public class Lista {

    private String nome;
    private Lista prox;
    private int numero;
    private double nota;

    public double getNota() {
        return nota;
    }

    public void setNota(double nota) {
        this.nota = nota;
    }

    public int getNumero() {
        return numero;
    }

    public void setNumero(int numero) {
        this.numero = numero;
    }

    public String getNome() {
        return nome;
    }

    public void setNome(String nome) {
        this.nome = nome;
    }

    public Lista getProx() {
        return prox;
    }

    public void setProx(Lista prox) {
        this.prox = prox;
    }

}

Class responsible for methods of adding and then other functions

public class Aluno {

    Lista topo;
    Lista ultimo;
    int numero = 1;

    public String pilharAluno(String nome, double nota) {
        Lista novo = new Lista();
        novo.setNome(nome);
        novo.setNumero(this.numero);
        novo.setNota(nota);
        this.numero++;
        if (topo == null) {
            topo = novo;
            ultimo = novo;
            novo.setProx(null);
        } else {
            novo.setProx(topo);
            topo = novo;
        }
        return "Aluno cadastrado";
    }

}
    
asked by anonymous 11.04.2015 / 22:08

1 answer

1

Use common sense, the student can (and should) have X notes. I think it's a data structure job or something, make a generic list, and then specialize it for what you want.

    public class Lista<T>{
        Class<T> obj;
        Lista<T> prox;
        int tamanho = 0;
        ...
        {Métodos que qualquer lista usaria: insere, retira, tamanho etc...}
        ...
    }

This is an outline of a template list in java , this T is a generic type that is defined at runtime, from this it is only make inheritance to modify the list object to its will.

In your case you would have a list of students that would be a List inheritance ex:

    public class ListaDeAlunos extends Lista<Aluno>{
        {Métodos que apenas uma lista de alunos possui(?)}
    }

If there are no specific methods / functions of a student list, simply instantiate a generic student list

Lista<Aluno> alunos = new Lista<Alunos>();

The student would be any object you would like to "collect" and it would contain another Double List inheritance that would have the specific functions / methods for a list of notes:

    public class ListaDeNotas extends Lista<Double>{
        {Métodos que apenas uma lista de notas possui: média, maior, menor...}

    }
    
22.07.2015 / 02:02