Tenho de fazer um programa que tenha 3 classes. A principal, uma de alunos e outra da turma. Na da turma devo fazer um método que calcule a média das médias dos alunos. Já consegui fazer a classe principal e a de alunos. Mas não consegui fazer a da turma, pois não consigo popular os dados com array. Alguém pode ajudar?
Envio abaixo as classes:
/*
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
*/
package controlenotasmedias;
import java.util.Scanner;
/**
*
@author Usuario
*/
public class ControleNotasMedias {
/**
@param args the command line arguments
*/
public static void main(String[] args) {
Turma t = new Turma();
Aluno a = new Aluno();
Scanner sc = new Scanner(System.in);
for (int i = 0; i < t.alunos.length; i++) {
double media;
System.out.println("Digite o nome do aluno " + i + ":");
a.setNome(sc.nextLine());
System.out.println("RA:");
a.setRa(sc.nextInt());
System.out.println("Digite a nota 1 do aluno " + a.getNome() + ":");
a.setNota1(sc.nextDouble());
System.out.println("Digite a nota 2 do aluno " + a.getNome() + ":");
a.setNota2(sc.nextDouble());
System.out.println("Digite a nota 3 do aluno " + a.getNome() + ":");
a.setNota3(sc.nextDouble());
System.out.println("Digite a nota 4 do aluno " + a.getNome() + ":");
a.setNota4(sc.nextDouble());
System.out.println("A média do aluno " + a.getNome() + " é:");
System.out.println(a.getMedia());
sc.nextLine();
}
System.out.println(“A média de notas da turma é:”);
System.out.println(t.mediaTurma());
/*
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
*/
package controlenotasmedias;
/**
* @return the nome
*/
public String getNome() {
return nome;
}
/**
* @param nome the nome to set
*/
public void setNome(String nome) {
this.nome = nome;
}
/**
* @return the ra
*/
public int getRa() {
return ra;
}
/**
* @param ra the ra to set
*/
public void setRa(int ra) {
this.ra = ra;
}
/**
* @return the nota1
*/
public double getNota1() {
return nota1;
}
/**
* @param nota1 the nota1 to set
*/
public void setNota1(double nota1) {
this.nota1 = nota1;
}
/**
* @return the nota2
*/
public double getNota2() {
return nota2;
}
/**
* @param nota2 the nota2 to set
*/
public void setNota2(double nota2) {
this.nota2 = nota2;
}
/**
* @return the nota3
*/
public double getNota3() {
return nota3;
}
/**
* @param nota3 the nota3 to set
*/
public void setNota3(double nota3) {
this.nota3 = nota3;
}
/**
* @return the nota4
*/
public double getNota4() {
return nota4;
}
/**
* @param nota4 the nota4 to set
*/
public void setNota4(double nota4) {
this.nota4 = nota4;
}
}
/*
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
*/
package controlenotasmedias;
/**
*
@author Usuario
*/
public class Turma {
protected Aluno[] alunos;
public Turma() {
alunos = new Aluno[5];
Aluno a = new Aluno();
for (int i = 0; i < alunos.length; i++) {
alunos[i]=new Aluno();
alunos[i].
}
}
public double mediaTurma(){
double mediaTurma;
double[] media = new double[5];
for (int i = 0; i < media.length; i++) {
media[i] = (alunos[i].getNota1() + alunos[i].getNota2() + alunos[i].getNota3() + alunos[i].getNota4())/4;
}
mediaTurma = (media[0] + media[1] + media[2] + media[3] + media[4])/5;
return mediaTurma;
}
private String get(String nome) {
throw new UnsupportedOperationException(“Not supported yet.”); //To change body of generated methods, choose Tools | Templates.
}
}
Na parte da classe de turma que está com erro, dentro contador, tentei fazer o seguinte:
amigos[i].a.getNome();
O mesmo em relação às outras variáveis. Mas quando tento imprimir amigos[1].a.getNome(), aparece o valor NULL, mesmo quando já digitei os dados na parte principal do programa.
Qual é a dificuldade de deixar o código organizado?
1 - Controle de Notas
package controlenotasmedias;
import java.util.Scanner;
/**
* @author Usuario
*/
public class ControleNotasMedias {
/*
*@param args the command line arguments
*/
public static void main(String[] args) {
Turma t = new Turma();
Aluno a = new Aluno();
Scanner sc = new Scanner(System.in);
for(int i = 0; i < t.alunos.length; i++){
double media;
System.out.println("Digite o nome do aluno " + i + ":");
a.setNome(sc.nextLine());
System.out.println("RA:");
a.setRa(sc.nextInt());
System.out.println("Digite a nota 1 do aluno " + a.getNome() + ":");
a.setNota1(sc.nextDouble());
System.out.println("Digite a nota 2 do aluno " + a.getNome() + ":");
a.setNota2(sc.nextDouble());
System.out.println("Digite a nota 3 do aluno " + a.getNome() + ":");
a.setNota3(sc.nextDouble());
System.out.println("Digite a nota 4 do aluno " + a.getNome() + ":");
a.setNota4(sc.nextDouble());
System.out.println("A média do aluno " + a.getNome() + " é:");
System.out.println(a.getMedia());
sc.nextLine();
}
System.out.println(“A média de notas da turma é:”);
System.out.println(t.mediaTurma());
}
2 - Aluno
/*
* @author Usuario
*/
public class Aluno {
private String nome;
private int ra;
private double nota1;
private double nota2;
private double nota3;
private double nota4;
public double getMedia(){
double media;
media = (getNota1() + getNota2() + getNota3() + getNota4())/4;
return(media);
}
/**
* @return the nome
*/
public String getNome(){
return nome;
}
/**
* @param nome the nome to set
*/
public void setNome(String nome){
this.nome = nome;
}
/**
* @return the ra
*/
public int getRa(){
return ra;
}
/**
* @param ra the ra to set
*/
public void setRa(int ra){
this.ra = ra;
}
/**
* @return the nota1
*/
public double getNota1(){
return nota1;
}
/**
* @param nota1 the nota1 to set
*/
public void setNota1(double nota1){
this.nota1 = nota1;
}
/**
* @return the nota2
*/
public double getNota2(){
return nota2;
}
/**
* @param nota2 the nota2 to set
*/
public void setNota2(double nota2){
this.nota2 = nota2;
}
/**
* @return the nota3
*/
public double getNota3(){
return nota3;
}
}
3 - Turma
/**
* @author Usuario
*/
public class Turma {
protected Aluno[] alunos;
public Turma(){
alunos = new Aluno[5];
Aluno a = new Aluno();
for(int i = 0; i < alunos.length; i++){
alunos[i]=new Aluno();
alunos[i].
}
}
public double mediaTurma(){
double mediaTurma;
double[] media = new double[5];
for(int i = 0; i < media.length; i++){
media[i] = (alunos[i].getNota1()
+ alunos[i].getNota2()
+ alunos[i].getNota3()
+ alunos[i].getNota4())/4;
}
mediaTurma = (media[0] + media[1] + media[2] + media[3] + media[4])/5;
return mediaTurma;
}
private String get(String nome){
throw new UnsupportedOperationException(“Not supported yet.”); //To change body of generated methods, choose Tools | Templates.
}
}
public class Aluno {
private static final short BIMESTRE = 4;
private int ra;
private String nome;
//Usar um vetor é mais produtivo do que várias variáveis
private double[] notas = new double[BIMESTRE];
private double media;
/*
* Métodos acessores e modificadores
*/
public void setRA(int ra){
this.ra = ra;
}
public int getRA(){
return ra;
}
public void setNome(String nome){
this.nome = nome;
}
public String getNome(){
return nome;
}
public void setNota(short indice, double nota){
this.notas[indice] = nota;
}
public double[] getNota(){
return notas;
}
public void setMedia(double media){
this.media = media;
}
public double getMedia(){
return media;
}
}
2 - Classe Turma
public class Turma {
//O escopo da variável aluno é global
private char ano;
private char identificador;
private Aluno[] alunos;
public void setLotacao(short lotacao){
alunos = new Aluno[lotacao];
}
public short getLotacao(){
return (short) alunos.length;
}
public void setAluno(int indice, Aluno aluno){
this.alunos[indice] = aluno;
}
public Aluno[] getAluno(){
return alunos;
}
public void setAno(char ano){
this.ano = ano;
}
public char getAno(){
return ano;
}
public void setIdentificador(char identificador){
this.identificador = identificador;
}
public char getIdentificador(){
return identificador;
}
}
3 - Classe Boletim
import java.util.Scanner;
public class Boletim {
private double media;
private Aluno[] alunos;
private Turma[] turmas;
public Boletim(){
turmas = new Turma[1];
}
private Aluno lerAlunos(){
short contador = 1;
double acumulador = 0;
Scanner leitor = new Scanner(System.in);
Aluno aluno = new Aluno();
System.out.print("\nInforme o número RA: ");
aluno.setRA(leitor.nextInt());
System.out.print("Informe o nome do aluno: ");
leitor.nextLine();
aluno.setNome(leitor.nextLine());
System.out.print("Notas\n");
for(short bimestre = 0; bimestre < aluno.getNota().length; bimestre++){
System.out.print((contador++) + "º bimestre: ");
aluno.setNota(bimestre, leitor.nextDouble());
acumulador += aluno.getNota()[bimestre];
}
aluno.setMedia(acumulador / aluno.getNota().length);
return aluno;
}
private void entumarAlunos(){
Scanner leitor = new Scanner(System.in);
Turma turma = new Turma();
System.out.print("\nAno: ");
turma.setAno(leitor.next().charAt(0));
System.out.print("Identificador: ");
turma.setIdentificador(leitor.next().charAt(0));
System.out.print("Número de alunos na turma: ");
turma.setLotacao(leitor.nextShort());
for(short i = 0; i < turma.getLotacao(); i++){
turma.setAluno(i, lerAlunos());
}
for(short i = 0; i < turmas.length; i++)
turmas[i] = turma;
}
public static void main(String[] args){
Boletim boletim = new Boletim();
boletim.entumarAlunos();
System.out.println("\n\tBoletim");
for(short i = 0; i < boletim.turmas.length; i++){
System.out.println("\tTurma " + boletim.turmas[i].getAno()
+ "º " + boletim.turmas[i].getIdentificador());
for(short j = 0; j < boletim.turmas[i].getAluno().length; j++){
short contador = 1;
System.out.println("\tRA: " + boletim.turmas[i].getAluno()[j].getRA()
+ "\tAluno: " + boletim.turmas[i].getAluno()[j].getNome());
System.out.println("\tNotas");
for(short k = 0; k < boletim.turmas[i].getAluno()[j].getNota().length; k++)
System.out.println("\t" + (contador++) + "º Bim: "
+ boletim.turmas[i].getAluno()[j].getNota()[k]);
System.out.println("\n\tMédia: "
+ Math.round(boletim.turmas[i].getAluno()[j].getMedia())
+ "\n");
}
}
}
}