Advertisement
Guest User

4°AulaPOO_19_03_2019

a guest
Mar 19th, 2019
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.03 KB | None | 0 0
  1. public class Pessoa {
  2. private String nome;
  3. private int anoNasc;
  4.  
  5. public Pessoa(){
  6. this.nome = "";
  7. this.anoNasc = 0;
  8. }
  9.  
  10. public Pessoa(String name, int ano){
  11. this.nome = name;
  12. this.anoNasc = ano;
  13. }
  14.  
  15. public void setNome(String name){
  16. this.nome = name;
  17. }
  18.  
  19. public String getNome(){
  20. return this.nome;
  21. }
  22.  
  23. public void setAnoNasc(int ano){
  24. this.anoNasc = ano;
  25. }
  26.  
  27. public int getAnoNasc(){
  28. return this.anoNasc;
  29. }
  30.  
  31. public String toString(){
  32. String saida;
  33. saida = this.nome + String.format("Ano de nascimento: %d \nIdade: %d", this.anoNasc, calculaIdade()) + "\n";
  34. return saida;
  35. }
  36.  
  37. public int calculaIdade(){
  38. int idade;
  39. idade = 2019 - this.anoNasc;
  40. return idade;
  41. }
  42. }
  43. -----------------------------------------------------------------------------------------------
  44.  
  45.  
  46. public class Estudante extends Pessoa {
  47. private String rgAcademico;
  48. private double nota1, nota2, media;
  49.  
  50. public Estudante(){
  51. super();
  52. this.rgAcademico = "";
  53. this.nota1 = 0;
  54. this.nota2 = 0;
  55. this.media = 0;
  56. }
  57.  
  58. public Estudante(String name, int ano, String ra, double n1, double n2){
  59. super(name, ano);
  60. this.rgAcademico = ra;
  61. this.nota1 = n1;
  62. this.nota2 = n2;
  63. this.media = (n1+n2)/2.;
  64. }
  65.  
  66. public void setRegAcademico(String ra){
  67. this.rgAcademico = ra;
  68. }
  69.  
  70. public String getRegAcademico(){
  71. return this.rgAcademico;
  72. }
  73.  
  74. public void setNota1(double n1){
  75. this.nota1 = n1;
  76. }
  77.  
  78. public double getNota1(){
  79. return this.nota1;
  80. }
  81.  
  82. public void setNota2(double n2){
  83. this.nota2 = n2;
  84. }
  85.  
  86. public double getNota2(){
  87. return this.nota2;
  88. }
  89.  
  90. public void setMedia(double mid){
  91. this.media = calculaMedia();
  92. }
  93.  
  94. public double getMedia(){
  95. return this.media;
  96. }
  97.  
  98. public double calculaMedia(){
  99. return( (this.nota1 + this.nota2) / 2.);
  100. }
  101.  
  102. public String situcao(){
  103. double media;
  104. media = calculaMedia();
  105. if(media >= 6)
  106. return ("Aprovado!");
  107. else
  108. return ("Reprovado!");
  109. }
  110.  
  111. public String toString(){
  112. String saida;
  113. saida = super.toString() + "Registro Academico: " + this.rgAcademico +
  114. String.format("Media: %f", this.calculaMedia()) + this.situcao() + "\n";
  115. return saida;
  116. }
  117. }
  118.  
  119. ---------------------------------------------------------------------------------------------
  120. import java.awt.*;
  121. import javax.swing.*;
  122. import java.util.*;
  123. import java.awt.event.*;
  124.  
  125. public class TelaGraficaHeranca extends JFrame {
  126.  
  127. private static final long serialVersionUID = 1L;
  128.  
  129. LinkedList <Pessoa> lista = new LinkedList <Pessoa>();
  130.  
  131. JLabel lnome, lano, lra, lnota1, lnota2;
  132. JTextField tnome, tano, tra, tnota1, tnota2;
  133. JButton btInsere, btExibe;
  134. JRadioButton rdPessoa, rdEstudante;
  135. ButtonGroup grupo;
  136. JPanel painelPessoa, painelEstudante, painelNorte, painelCentro, painelSul;
  137.  
  138. public TelaGraficaHeranca(){
  139. super(" Herança Pessoa-Estudante");
  140. setLayout(new BorderLayout());
  141.  
  142. // Criar os componentes RadioButton
  143. grupo = new ButtonGroup();
  144. grupo.add(rdPessoa);
  145. grupo.add(rdEstudante);
  146.  
  147. lnome = new JLabel("Nome", SwingConstants.RIGHT);
  148. lano = new JLabel("Ano", SwingConstants.RIGHT);
  149. lra = new JLabel("Registro Academico", SwingConstants.RIGHT);
  150. lnota1 = new JLabel("Nota 1", SwingConstants.RIGHT);
  151. lnota2 = new JLabel("Nota 2", SwingConstants.RIGHT);
  152.  
  153. tnome = new JTextField(40);
  154. tano = new JTextField(4) ;
  155. tra = new JTextField(6);
  156. tnota1 = new JTextField(5);
  157. tnota2 = new JTextField(5);
  158.  
  159. btInsere = new JButton("Insere");
  160. btExibe = new JButton("Exibe");
  161. //----------------------------------------------------------------
  162.  
  163. // criar os JPanel e implementar os componentes
  164.  
  165. // Painel Norte
  166. painelNorte = new JPanel(new FlowLayout());
  167. painelNorte.add(rdPessoa);
  168. painelNorte.add(rdEstudante);
  169. add(painelNorte, BorderLayout.NORTH);
  170. // Painel Norte
  171.  
  172. // Painel Centro
  173. painelPessoa = new JPanel(new GridLayout(2,2,5,5));
  174. painelPessoa.add(lnome); painelPessoa.add(tnome);
  175. painelPessoa.add(lano); painelPessoa.add(tano);
  176.  
  177. painelEstudante = new JPanel(new GridLayout(3,2,5,5));
  178. painelEstudante.add(lra); painelEstudante.add(tra);
  179. painelEstudante.add(lnota1); painelEstudante.add(tnota1);
  180. painelEstudante.add(lnota2); painelEstudante.add(tnota2);
  181.  
  182. painelCentro = new JPanel(new GridLayout(2,1,5,5));
  183. painelCentro.add(painelPessoa);
  184. painelCentro.add(painelEstudante);
  185. add(painelCentro, BorderLayout.CENTER);
  186. // Painel Centro
  187.  
  188. // Painel Sul
  189. painelSul = new JPanel(new FlowLayout());
  190. painelSul.add(btInsere);
  191. painelSul.add(btExibe);
  192. add(painelSul, BorderLayout.SOUTH);
  193. // Painel Sul
  194.  
  195. // Botões de ação
  196. BotoesRadio botaoAcao = new BotoesRadio();
  197. rdPessoa.addItemListener(botaoAcao);
  198. rdEstudante.addItemListener(botaoAcao);
  199.  
  200. // Ação dos botoes (insere e exibe)
  201.  
  202. btExibe.addActionListener(new ActionListener(){
  203. public void actionPerformed(ActionEvent e){
  204. String texto = "\n Lista de Pessoa e Estudantes \n";
  205. int i = 1;
  206. for(Pessoa p:lista){
  207. texto += "Posição: " + i + p.toString() + "\n\n";
  208. i++;
  209. }
  210. JTextArea area = new JTextArea(texto,11,10);
  211. JOptionPane.showMessageDialog(null, area);
  212. }
  213. }
  214. );
  215.  
  216. btInsere.addActionListener(new ActionListener(){
  217. public void actionPerformed(ActionEvent e){
  218. double nota1, nota2, media;
  219. int ano;
  220. String nome, a, data, ra, n1, n2;
  221. Pessoa p;
  222. Estudante est;
  223.  
  224. nome = tnome.getText();
  225. a = tano.getText();
  226. try{
  227. ano = Integer.parseInt(a);
  228. }catch(NumberFormatException erro){
  229. ano = 1990;
  230. }
  231. if(rdPessoa.isSelected()){
  232. p = new Pessoa(nome, ano);
  233. lista.add(p);
  234. }
  235. if(rdPessoa.isSelected()){
  236. ra = tra.getText();
  237. n1 = tnota1.getText();
  238. n2 = tnota2.getText();
  239. try{
  240. nota1 = Double.parseDouble(n1);
  241. nota2 = Double.parseDouble(n2);
  242. }catch(NumberFormatException erro){
  243. nota1 = 0;
  244. nota2 = 0;
  245. }
  246. est = new Estudante(nome, ano, ra, nota1, nota2);
  247. lista.add(est);
  248. }
  249. JOptionPane.showMessageDialog(null, "Cadastro realizado com sucesso!");
  250. tnome.setText(" ");
  251. tano.setText(" ");
  252. tra.setText(" ");
  253. tnota1.setText(" ");
  254. tnota2.setText(" ");
  255. rdPessoa.requestFocus();
  256. }
  257. }
  258. );
  259.  
  260. //----------------------------------------------------------------
  261. }
  262.  
  263. private class BotoesRadio implements ItemListener{
  264.  
  265. @Override
  266. public void itemStateChanged(ItemEvent evento) {
  267. // TODO Auto-generated method stub
  268.  
  269. if(evento.getSource() == rdPessoa){
  270. painelPessoa.setVisible(true);
  271. painelEstudante.setVisible(false);
  272. }
  273. else if(evento.getSource() == rdEstudante){
  274. painelEstudante.setVisible(true);
  275. painelPessoa.setVisible(true);
  276. }
  277. }
  278.  
  279. }
  280.  
  281.  
  282.  
  283. public static void main(String[] args) {
  284. // TODO Auto-generated method stub
  285. TelaGraficaHeranca x = new TelaGraficaHeranca();
  286. x.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  287. x.setSize(400,300);
  288. x.setVisible(true);
  289. }
  290.  
  291. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement