Sei sulla pagina 1di 7

Programação Orientada a Objeto

Lista de Exercícios - Herança

Com base no diagrama e nos códigos fornecidos implemente a aplicação a seguir.

package negocio;
public class Aluno {

private int matricula;


private String nome;
private INotas notas;

public Aluno() {
}

public Aluno(int matricula, String nome) throws AlunoException {


this.setMatricula(matricula);
this.setNome(nome);
}

public int getMatricula() {


return matricula;
}

Prof. Evandro César Freiberger 1/7


Programação Orientada a Objeto
Lista de Exercícios - Herança

public final void setMatricula(int matricula) throws AlunoException {


if (matricula > 0) {
this.matricula = matricula;
} else {
throw new AlunoException("Matricula inválida - deve ser maior que zero");
}
}

public String getNome() {


return nome;
}

public final void setNome(String nome) throws AlunoException {


if (nome.length() > 0) {
this.nome = nome;
} else {
throw new AlunoException("Nome inválido - não pode ser vazio");
}
}

public void setNotas(INotas notas) throws AlunoException {


if (notas != null) {
this.notas = notas;
} else {
throw new AlunoException("A coleção de notas não pode ser nula");
}
}

public float getMedia() {


return this.notas.obterMedia();
}

public float getSomatoriaNotas() {


return this.notas.somatoriaNota();
}
}

package negocio;
public class AlunoException extends Exception{

public AlunoException() {
}

public AlunoException(String mensagem) {


super(mensagem);
}

@Override
public String toString(){
return this.getMessage()+" detalhes: [Exceção ocorrida em Aluno]";
}
}

Prof. Evandro César Freiberger 2/7


Programação Orientada a Objeto
Lista de Exercícios - Herança
package negocio;
public interface INota {

float getValor();

package negocio;
public interface INotaPonderada extends INota{

int getPeso();

package negocio;
public interface INotas {

float obterMedia();

float somatoriaNota();

void adicionaNota(INota nota) throws NotaException;


}

package negocio;
public class NotaAritmetica implements INota{

private float valor;

public NotaAritmetica() throws NotaException{


}

public NotaAritmetica(float valor) throws NotaException{


this.setValor(valor);
}

public float getValor() {


return valor;
}

public final void setValor(float valor) throws NotaException {


if (valor >= 0 && valor <= 10) {
this.valor = valor;
} else {
throw new NotaException("O valor da nota deve ficar entre 0 e 10");
}
}
}

Prof. Evandro César Freiberger 3/7


Programação Orientada a Objeto
Lista de Exercícios - Herança

package negocio;
public class NotaException extends Exception{

public NotaException() {
}

public NotaException(String mensagem) {


super(mensagem);
}

@Override
public String toString(){
return this.getMessage()+" detalhes: [Exceção ocorrida em Nota]";
}
}

package negocio;
public class NotaPonderada extends NotaAritmetica implements INotaPonderada{

private int peso;

public NotaPonderada() throws NotaException{


super(0);
this.setPeso(1);
}

public NotaPonderada(float valor, int peso) throws NotaException{


super(valor);
this.setPeso(peso);
}

public int getPeso() {


return peso;
}

public final void setPeso(int peso) throws NotaException{


if (peso > 0 && peso <= 3) {
this.peso = peso;
} else {
throw new NotaException("O peso da nota deve ficar entre 1 e 3");
}
}

@Override
public float getValor(){
return super.getValor() * this.peso;
}
}

Prof. Evandro César Freiberger 4/7


Programação Orientada a Objeto
Lista de Exercícios - Herança

package negocio;
import java.util.ArrayList;
import java.util.List;

public class NotasAritmetica implements INotas{

protected List<INota> listaNota;

public NotasAritmetica(){
this.listaNota = new ArrayList<INota>();
}

public void adicionaNota(INota nota) throws NotaException{


this.listaNota.add(nota);
}

public float obterMedia(){


return this.somatoriaNota() / this.listaNota.size();
}

public float somatoriaNota(){


float soma = 0;
for(INota nota : this.listaNota){
soma += nota.getValor();
}
return soma;
}
}

package negocio;
public class NotasPonderada extends NotasAritmetica{

public NotasPonderada(){
}

@Override
public float obterMedia(){
return this.somatoriaNota() / this.somatoriaPeso();
}

private int somatoriaPeso(){


int soma = 0;
INotaPonderada notaTemp;
for(int x=0; x < this.listaNota.size(); x++){
notaTemp = (INotaPonderada) this.listaNota.get(x);
soma += notaTemp.getPeso();
}
return soma;
}
}

Prof. Evandro César Freiberger 5/7


Programação Orientada a Objeto
Lista de Exercícios - Herança

package aplicacao;
import negocio.Aluno;
import negocio.AlunoException;
import negocio.INotas;
import negocio.NotaAritmetica;
import negocio.NotaException;
import negocio.NotaPonderada;
import negocio.NotasAritmetica;
import negocio.NotasPonderada;

public class Principal {

public static void main(String[] args) {

Aluno aluno1=null, aluno2=null;

INotas notas1;

notas1 = new NotasAritmetica();

try {
notas1.adicionaNota(new NotaAritmetica(8.0f));
notas1.adicionaNota(new NotaAritmetica(7.0f));
notas1.adicionaNota(new NotaAritmetica(9.0f));
} catch (NotaException ex) {
System.out.println(ex);
}

try {
aluno1 = new Aluno(1010, "Fulano das Quantas");
aluno1.setNotas(notas1);
} catch (AlunoException ex) {
System.out.println(ex);
}

INotas notas2 = new NotasPonderada();

try {
notas2.adicionaNota(new NotaPonderada(8.0f, 1));
notas2.adicionaNota(new NotaPonderada(6.0f, 2));
notas2.adicionaNota(new NotaPonderada(5.0f, 3));
} catch (NotaException ex) {
System.out.println(ex);
}

try {
aluno2 = new Aluno(2020, "Beltrano das Quantas");
aluno2.setNotas(notas2);
} catch (AlunoException ex) {
System.out.println(ex);
}

Prof. Evandro César Freiberger 6/7


Programação Orientada a Objeto
Lista de Exercícios - Herança

System.out.println("Matricula......: "+aluno1.getMatricula());
System.out.println("Nome...........: "+aluno1.getNome());
System.out.println("Somatoria notas: "+aluno1.getSomatoriaNotas());
System.out.println("Média notas....: "+aluno1.getMedia());
System.out.println("-----------------------------------------");
System.out.println("Matricula......: "+aluno2.getMatricula());
System.out.println("Nome...........: "+aluno2.getNome());
System.out.println("Somatoria notas: "+aluno2.getSomatoriaNotas());
System.out.println("Média notas....: "+aluno2.getMedia());

}
}

Prof. Evandro César Freiberger 7/7

Potrebbero piacerti anche