I can not add new student to my arraylist

2

I'm reviewing previous java stuff and in the midst of revising arraylists I found it difficult to add a student to the list.

Each student has the following fields: name, type of student, type of course, subject 1, 2 3 and ID number.

I can not identify the problem. The scripts I have are the following:

First Script:

    package aluno_br;

    public class Aluno_br {

         /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {

            Turma tm = new Turma();
            tm.novoAluno("Joca", "Regular", "Informatica", "TIC", "Ing", "Mat", 82005); 
    // ele empanca aqui mas julgo       
// que o erro está no terceiro script

            tm.listaAlunos();

        }

    }

Second Script:

package aluno_br;

import java.util.Random; 

public class Aluno {
    private String nome, tipo, curso, disciplina1, disciplina2, disciplina3;
    private int nMec, nota1, nota2,nota3;

    Aluno(String nome, String tipo, String curso, String disciplina1, String disciplina2, String disciplina3, int nMec) {
        this.nome = nome;
        this.tipo = tipo;
        this.curso = curso;
        this.disciplina1 = disciplina1;
        this.disciplina2 = disciplina2;
        this.disciplina3 = disciplina3;
        this.nMec = nMec;
    }

    public int avaliar1(){ 
        Random rand = new Random(); 
            nota1 = rand.nextInt(21); 
            System.out.println(nota1);
            setNota1(nota1);
            return nota1;
    }

    public int avaliar2(){ 
        Random rand = new Random(); 
            nota2 = rand.nextInt(21); 
            System.out.println(nota2); 
            setNota2(nota2);            
            return nota2;
    }

    public int avaliar3(){ 
        Random rand = new Random(); 
            nota3 = rand.nextInt(21); 
            System.out.println(nota3);    
            setNota3(nota3);            
            return nota3;
    }

    public void passar(){
        int media = (nota1 + nota2 + nota3)/3;
        if (media >= 10) {
            System.out.println("passado");
        } else {
            System.out.println("po ano ha mais");
        }
    }

Third Script

package aluno_br;


import java.util.ArrayList;

/**
 *
 * @author 
 */
public class Turma {
    private ArrayList<Aluno> turma;    

    public Turma(){
        turma = new ArrayList<Aluno>();
    } 

    void novoAluno (Aluno a){
        turma.add(a);
    }    

        public void listaAlunos () {
        for (Aluno a: turma) {
            System.out.print(a.getnMec()); 
            System.out.print(" ");
            System.out.print(a.getNome());   
        }    
    } 
}        
    
asked by anonymous 16.06.2017 / 01:16

1 answer

3

The novoAluno method expects a type Aluno and not 5 String types. Modify as below, passing a student object with the information:

tm.novoAluno(new Aluno("Joca", "Regular", "Informatica", "TIC", "Ing", "Mat", 82005)); 

or to stay more didactic:

public class Aluno_br {

     /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {

        Turma tm = new Turma();

        Aluno a = new Aluno("Joca", "Regular", "Informatica", "TIC", "Ing", "Mat", 82005);

        tm.novoAluno(a);

        tm.listaAlunos();

    }
}
    
16.06.2017 / 01:19