I’m having trouble adding a new student to my arraylist

Asked

Viewed 111 times

2

I am reviewing previous java matter and in the middle of reviewing arraylists I found it difficult to add a student to the list.

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

I am unable to identify the problem. The scripts I have are as follows::

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();

        }

    }

Script-wise:

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());   
        }    
    } 
}        

1 answer

3


Apparently the method novoAluno wait for a guy Aluno and not 5 types String. Modify as below, passing a student object with the information:

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

or to make it more educational:

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();

    }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.