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 Aluno
I 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);
}
}
getter
to the list,getDisciplinas()
– Jefferson Quesado