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);
}
}