How to manipulate ArrayList of Java objects

1

The program consists of adding students and for each student add disciplines and for each discipline add notes. In the end, the program should return all students and their subjects, and for each subject the average, standard deviation and variance should be shown. First I created two classes Aluno and Disciplina and a Aluno1 class that makes use of class Aluno . In class Aluno I created the attribute nome and a ArrayList of type Disciplina . My Inquiries are:

1 - How do I create the getDisciplina method, with the attribute being ArrayList ?

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

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

I know I could directly use the Disciplina class in the program, but I do not 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);
}
}
    
asked by anonymous 06.10.2017 / 21:40

1 answer

2
  

1 - How do I create the getDiscipline method, where is the attribute an ArrayList?

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

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

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

You need to access the list of courses 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 do I access or how do I assign values to the objects I create in the Student1 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 because you said "I did not want to directly use the class Disciplina ". Because I do not want to use it, I think you do not want to instantiate an object of Disciplina and so it was used directly in the constructor. But it gets more readable 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 these are your doubts. Anything I update the answer.

    
06.10.2017 / 22:03