Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Pessoa {
- private String nome;
- private int anoNasc;
- public Pessoa(){
- this.nome = "";
- this.anoNasc = 0;
- }
- public Pessoa(String name, int ano){
- this.nome = name;
- this.anoNasc = ano;
- }
- public void setNome(String name){
- this.nome = name;
- }
- public String getNome(){
- return this.nome;
- }
- public void setAnoNasc(int ano){
- this.anoNasc = ano;
- }
- public int getAnoNasc(){
- return this.anoNasc;
- }
- public String toString(){
- String saida;
- saida = this.nome + String.format("Ano de nascimento: %d \nIdade: %d", this.anoNasc, calculaIdade()) + "\n";
- return saida;
- }
- public int calculaIdade(){
- int idade;
- idade = 2019 - this.anoNasc;
- return idade;
- }
- }
- -----------------------------------------------------------------------------------------------
- public class Estudante extends Pessoa {
- private String rgAcademico;
- private double nota1, nota2, media;
- public Estudante(){
- super();
- this.rgAcademico = "";
- this.nota1 = 0;
- this.nota2 = 0;
- this.media = 0;
- }
- public Estudante(String name, int ano, String ra, double n1, double n2){
- super(name, ano);
- this.rgAcademico = ra;
- this.nota1 = n1;
- this.nota2 = n2;
- this.media = (n1+n2)/2.;
- }
- public void setRegAcademico(String ra){
- this.rgAcademico = ra;
- }
- public String getRegAcademico(){
- return this.rgAcademico;
- }
- public void setNota1(double n1){
- this.nota1 = n1;
- }
- public double getNota1(){
- return this.nota1;
- }
- public void setNota2(double n2){
- this.nota2 = n2;
- }
- public double getNota2(){
- return this.nota2;
- }
- public void setMedia(double mid){
- this.media = calculaMedia();
- }
- public double getMedia(){
- return this.media;
- }
- public double calculaMedia(){
- return( (this.nota1 + this.nota2) / 2.);
- }
- public String situcao(){
- double media;
- media = calculaMedia();
- if(media >= 6)
- return ("Aprovado!");
- else
- return ("Reprovado!");
- }
- public String toString(){
- String saida;
- saida = super.toString() + "Registro Academico: " + this.rgAcademico +
- String.format("Media: %f", this.calculaMedia()) + this.situcao() + "\n";
- return saida;
- }
- }
- ---------------------------------------------------------------------------------------------
- import java.awt.*;
- import javax.swing.*;
- import java.util.*;
- import java.awt.event.*;
- public class TelaGraficaHeranca extends JFrame {
- private static final long serialVersionUID = 1L;
- LinkedList <Pessoa> lista = new LinkedList <Pessoa>();
- JLabel lnome, lano, lra, lnota1, lnota2;
- JTextField tnome, tano, tra, tnota1, tnota2;
- JButton btInsere, btExibe;
- JRadioButton rdPessoa, rdEstudante;
- ButtonGroup grupo;
- JPanel painelPessoa, painelEstudante, painelNorte, painelCentro, painelSul;
- public TelaGraficaHeranca(){
- super(" Herança Pessoa-Estudante");
- setLayout(new BorderLayout());
- // Criar os componentes RadioButton
- grupo = new ButtonGroup();
- grupo.add(rdPessoa);
- grupo.add(rdEstudante);
- lnome = new JLabel("Nome", SwingConstants.RIGHT);
- lano = new JLabel("Ano", SwingConstants.RIGHT);
- lra = new JLabel("Registro Academico", SwingConstants.RIGHT);
- lnota1 = new JLabel("Nota 1", SwingConstants.RIGHT);
- lnota2 = new JLabel("Nota 2", SwingConstants.RIGHT);
- tnome = new JTextField(40);
- tano = new JTextField(4) ;
- tra = new JTextField(6);
- tnota1 = new JTextField(5);
- tnota2 = new JTextField(5);
- btInsere = new JButton("Insere");
- btExibe = new JButton("Exibe");
- //----------------------------------------------------------------
- // criar os JPanel e implementar os componentes
- // Painel Norte
- painelNorte = new JPanel(new FlowLayout());
- painelNorte.add(rdPessoa);
- painelNorte.add(rdEstudante);
- add(painelNorte, BorderLayout.NORTH);
- // Painel Norte
- // Painel Centro
- painelPessoa = new JPanel(new GridLayout(2,2,5,5));
- painelPessoa.add(lnome); painelPessoa.add(tnome);
- painelPessoa.add(lano); painelPessoa.add(tano);
- painelEstudante = new JPanel(new GridLayout(3,2,5,5));
- painelEstudante.add(lra); painelEstudante.add(tra);
- painelEstudante.add(lnota1); painelEstudante.add(tnota1);
- painelEstudante.add(lnota2); painelEstudante.add(tnota2);
- painelCentro = new JPanel(new GridLayout(2,1,5,5));
- painelCentro.add(painelPessoa);
- painelCentro.add(painelEstudante);
- add(painelCentro, BorderLayout.CENTER);
- // Painel Centro
- // Painel Sul
- painelSul = new JPanel(new FlowLayout());
- painelSul.add(btInsere);
- painelSul.add(btExibe);
- add(painelSul, BorderLayout.SOUTH);
- // Painel Sul
- // Botões de ação
- BotoesRadio botaoAcao = new BotoesRadio();
- rdPessoa.addItemListener(botaoAcao);
- rdEstudante.addItemListener(botaoAcao);
- // Ação dos botoes (insere e exibe)
- btExibe.addActionListener(new ActionListener(){
- public void actionPerformed(ActionEvent e){
- String texto = "\n Lista de Pessoa e Estudantes \n";
- int i = 1;
- for(Pessoa p:lista){
- texto += "Posição: " + i + p.toString() + "\n\n";
- i++;
- }
- JTextArea area = new JTextArea(texto,11,10);
- JOptionPane.showMessageDialog(null, area);
- }
- }
- );
- btInsere.addActionListener(new ActionListener(){
- public void actionPerformed(ActionEvent e){
- double nota1, nota2, media;
- int ano;
- String nome, a, data, ra, n1, n2;
- Pessoa p;
- Estudante est;
- nome = tnome.getText();
- a = tano.getText();
- try{
- ano = Integer.parseInt(a);
- }catch(NumberFormatException erro){
- ano = 1990;
- }
- if(rdPessoa.isSelected()){
- p = new Pessoa(nome, ano);
- lista.add(p);
- }
- if(rdPessoa.isSelected()){
- ra = tra.getText();
- n1 = tnota1.getText();
- n2 = tnota2.getText();
- try{
- nota1 = Double.parseDouble(n1);
- nota2 = Double.parseDouble(n2);
- }catch(NumberFormatException erro){
- nota1 = 0;
- nota2 = 0;
- }
- est = new Estudante(nome, ano, ra, nota1, nota2);
- lista.add(est);
- }
- JOptionPane.showMessageDialog(null, "Cadastro realizado com sucesso!");
- tnome.setText(" ");
- tano.setText(" ");
- tra.setText(" ");
- tnota1.setText(" ");
- tnota2.setText(" ");
- rdPessoa.requestFocus();
- }
- }
- );
- //----------------------------------------------------------------
- }
- private class BotoesRadio implements ItemListener{
- @Override
- public void itemStateChanged(ItemEvent evento) {
- // TODO Auto-generated method stub
- if(evento.getSource() == rdPessoa){
- painelPessoa.setVisible(true);
- painelEstudante.setVisible(false);
- }
- else if(evento.getSource() == rdEstudante){
- painelEstudante.setVisible(true);
- painelPessoa.setVisible(true);
- }
- }
- }
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- TelaGraficaHeranca x = new TelaGraficaHeranca();
- x.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- x.setSize(400,300);
- x.setVisible(true);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement