How to Manipulate Arraylist of Java Objects

Asked

Viewed 7,190 times

1

The program consists of adding students and for each student adding subjects and for each discipline adding grades. In the end the program must return all the students and their disciplines, and for each discipline must be shown the average, standard deviation and variance. First I created two classes Aluno and Disciplina and a class Aluno1 who makes use of the class Aluno. In class AlunoI created the attribute nome and a ArrayList of the kind Disciplina. My Doubts are:

1 - How I create the method getDisciplina, where the attribute is a ArrayList?

2 - How I access attributes and methods through the method getDisciplina (if I can do that)?

3 - How I access how I assign values to the objects I create in class Aluno1?

I know I could use the class directly Disciplina in the program, but I don’t want to do it that way.

import java.util.ArrayList;

public class Aluno {
private String nome;
private ArrayList<Disciplina> disciplinas;

//Construtor nome
public Aluno(String nome){
    this.nome = nome;

    disciplinas = new ArrayList<Disciplina>();
}
public String getNome(){
    return nome;
}

public Disciplina getDisciplina(){
    for(Disciplina item : disciplinas) {
        return item;
    }
    return null;
}
}







import java.util.Scanner;
import java.util.ArrayList;

public class Aluno1{

public static void main(String[] args) {

    Scanner input = new Scanner(System.in);
    ArrayList<Aluno> alunos = new ArrayList<Aluno>();
    int opcao, opcao2;

    do{
        System.out.println("0 - Sair\n1 - Adicionar novo aluno\n2 - Mostrar todos os alunos");
        opcao = input.nextInt();

        switch(opcao){
            case 0:

                break;

            case 1:

                System.out.println("Nome do aluno: ");
                String nome = input.next();

                Aluno aluno = new Aluno(nome);
                alunos.add(aluno);

                do{
                    System.out.println("0 - Sair\n1 - Adicionar Disciplina");
                    opcao2 = input.nextInt();

                    switch(opcao2){
                        case 0:

                            break;

                        case 1:

                            System.out.println("Nome da disciplina: ");
                            String nomeDisc = input.next();

                            aluno.getDisciplina().;

                            break;

                        default:

                            System.out.println("Opcão Inválida!");
                            break;  
                    }
                }while(opcao2 != 0);

                break;

            case 2:

                break;

            default:
                System.out.println("Opcão Inválida!");

                break;
        }
    }while(opcao != 0);
}
}







import java.util.ArrayList;

public class Disciplina {
private String nome;
private ArrayList<Double> notas;

public String getNome(){
    return nome;
}

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

    notas = new ArrayList<Double>();
}

public ArrayList<Double> getNota(){
    return notas;
}

public double getMedia(){
    double media = 0;

    for(Double nota : notas){
        media += nota;
    }

    if(media != 0)
        return media / notas.size();
    else
        return (double) 0;
}

public double getDesvioPadrao(){
    double soma = 0;

    for(Double nota : notas) {
        soma += Math.pow((nota - getMedia()), 2);
    }
    return Math.sqrt((soma / (notas.size() - 1)));
}

public double getVariancia(){
    double soma = 0;

    for(Double nota : notas) {
        soma += Math.pow((nota - getMedia()), 2);
    }

    return soma / (notas.size() - 1);
}
}
    1. I think you should have the getter to the list, getDisciplinas()

1 answer

3


1 - How I create the getDiscipline method, the attribute being an Arraylist?

Simple, make him return one ArrayList of Disciplina, since each student will have more than one subject. Ex:

public ArrayList<Disciplina> getDisciplinas() {
    return disciplinas;
}

2 - How I access attributes and methods through the getDiscipline method (if I can do that)?

You need to access the list of disciplines by their index:

Aluno aluno = new Aluno();
// para setar um atributo através do setter
aluno.getDisciplinas().get(indice).setNome(nomeDisciplina);

//para pegar um atributo através do getter ou realizar operações
String nomeDisciplina = aluno.getDisciplinas().get(indice).getNome();
double media = aluno.getDisciplinas().get(indice).getMedia();

3 - How I access or how I assign values to the objects I create in the Aluno1 class?

Aluno aluno = new Aluno("Irineu");
aluno.getDisciplinas().add(new Disciplina("Matemática").getNotas().add(9));
double media = aluno.getDisciplinas().get(indice).getMedia();

I did it this way above because you said that "I didn’t want to use the class directly Disciplina". For not wanting to use, I believe you do not want to instantiate an object of Disciplina and so it was used directly in the constructor. But it is more readable in this way:

Aluno aluno = new Aluno("Irineu");
Disciplina disciplina = new Disciplina("Matemática");
disciplina.getNotas().add(9);
aluno.getDisciplinas().add(disciplina);

double media = aluno.getDisciplinas().get(indice).getMedia();

Maybe those are your doubts. Anything I update the answer.

Browser other questions tagged

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