Advertisement
Guest User

OQ

a guest
Oct 20th, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.87 KB | None | 0 0
  1.  
  2. package turma;
  3.  
  4.  
  5. public class main {
  6.  
  7.  
  8.       public static void main(String[] args) {
  9.        
  10.        Classe prog1 = Classe.criarLista(6);
  11.        prog1.cadastrarAluno(20, "Marcelo");
  12.        prog1.listarTurma();
  13.        
  14.     }
  15.    
  16. }
  17.  
  18.  
  19.  
  20.  
  21. package turma;
  22.  
  23.  
  24. public  class Classe {
  25.    
  26.    private Aluno[] lista;
  27.    
  28.    public Classe(Aluno[] lista ){
  29.        this.lista=lista;
  30.              
  31.    }
  32.    
  33.     public static Classe criarLista(int num){
  34.         Aluno[] l = new Aluno[num];
  35.        
  36.         Classe t = new Classe(l);
  37.          return t;
  38.     }
  39.    
  40.     public void cadastrarAluno(int matricula, String nome){
  41.         for (int i = 0; i < lista.length; i++) {
  42.             if(lista[i]==null){
  43.                 Aluno a = new Aluno(matricula,nome);
  44.                 lista[i]=a;
  45.                 break;
  46.             }else{
  47.                 System.out.println("lista cheia!");
  48.             }
  49.         }
  50.        
  51.     }
  52.    
  53.    
  54.     public void listarTurma(){
  55.         for (int i = 0; i < lista.length; i++) {
  56.            
  57.            
  58.             if (lista[i] != null){
  59.                 System.out.println(lista[i].toString());
  60.         }else{
  61.                 break;
  62.             }
  63.        
  64.        
  65.         }
  66.        
  67.     }
  68.    
  69. }
  70.  
  71.  
  72.  
  73.  
  74.  
  75.  
  76. package turma;
  77.  
  78.  
  79. public class Aluno {
  80.    
  81.    private int matricula;
  82.    private String nome;
  83.    public Aluno(int matricula, String nome){
  84.        this.matricula=matricula;
  85.        this.nome=nome;
  86.            
  87.    }
  88. public int getMatricula(){
  89.     return this.matricula;
  90. }
  91. public void setMatricula(int mat){
  92.     this.matricula = mat;
  93. }
  94. public String getNome(){
  95.     return this.nome;
  96.    
  97. }
  98. public void setNome (){
  99.     this.nome = nome;
  100. }
  101. public String toString(){
  102.     return "\nNome: " + this.nome + "\nMatricula: " + this.matricula;
  103.  
  104.    
  105.  
  106. }
  107.  
  108.  
  109.  
  110.  
  111.  
  112.  
  113.  
  114.  
  115.  
  116.  
  117.  
  118.  
  119.  
  120.  
  121.  
  122.  
  123.  
  124.  
  125.  
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement