Static method with object as parameter

1

I am creating a very simple system for registering students. I made a Aluno class as follows:

public class Aluno {
    private String nome;
    private String matricula;
    private String curso;
    private int periodo;
    private String[] disciplinasMatriculadas;
    private int idade;
    private String endereco;

    public Aluno(String nome, String matricula, String curso, int periodo,
            int quantidadeDisciplinasPermitidas) {
        super();
        this.nome = nome;
        this.matricula = matricula;
        this.curso = curso;
        this.periodo = periodo;
        disciplinasMatriculadas = new String[quantidadeDisciplinasPermitidas];
    }

    public String getNome() {
        return nome;
    }

    public void setNome(String nome) {
        this.nome = nome;
    }

    public String getMatricula() {
        return matricula;
    }

    public void setMatricula(String matricula) {
        this.matricula = matricula;
    }

    public String getCurso() {
        return curso;
    }

    public void setCurso(String curso) {
        this.curso = curso;
    }

    public int getPeriodo() {
        return periodo;
    }

    public void setPeriodo(int periodo) {
        this.periodo = periodo;
    }


    public int getIdade() {
        return idade;
    }

    public void setIdade(int idade) {
        this.idade = idade;
    }

    public String getEndereco() {
        return endereco;
    }

    public void setEndereco(String endereco) {
        this.endereco = endereco;
    }

    int d = 0;
    public String fazMatricula(String disciplina){
        if(d == disciplinasMatriculadas.length - 1){
            return "Quantidade de disciplinas excedida. O limite de matriculas para esse aluno é de " +
        disciplinasMatriculadas.length + " disciplina(s). Se desejar, cancele sua matricula em uma das disciplinas" +
                    " e faça uma nova matricula.";
        } else if(disciplinasMatriculadas.length == 0){
            return "Este aluno não pode ser matriculado em nenhuma disciplina. Por favor, fale com a secretaria.";
        } else{
            disciplinasMatriculadas[d] = disciplina;
            d++;
            return "Matricula na disciplina \"" + disciplina + "\" realizada com sucesso!";
        }
    }

    public String cancelaMatricula(String disciplina){
        boolean disciplinaMatriculada = false;
        int j = 0;
        for(int i = 0; i < disciplinasMatriculadas.length - 1; i++){
            if(disciplinasMatriculadas[i].equals(disciplina)){
                disciplinaMatriculada = true;
                continue;
            }else if(i == disciplinasMatriculadas.length - 1 && disciplinaMatriculada == false){
                System.out.println("Aluno não está matriculado na disciplina \"" + disciplina + 
                        "\", portanto não é possivel cancelar essa matricula"); 
                break;
            }else{
                disciplinasMatriculadas[j] = disciplinasMatriculadas[i];
                j++;
            }
        }return "Cancelamento da matricula na disciplina \"" + disciplina + "\" realizado com sucesso!";
    }

    public String imprime(){
        return "-----------------------------------------------------\nNome do aluno: " + nome + "\nMatricula: " +
    matricula + "\nCurso: " + curso + "\nPeriodo: " + periodo + "\nDisciplinas Matriculadas: " + 
                disciplinasMatriculadas.toString() + "\n-----------------------------------------------------\n";
    }

Then I did a main method called SystemAcademic which should serve for the user to choose an option and perform the desired operation.

import java.util.Scanner;

public class SistemaAcademico {

    public static void main(String[] args) {
        Aluno alunos[];

        Scanner s = new Scanner(System.in);
        System.out.println("Informe a quantidade de alunos que será cadastrada:");
        alunos = new Aluno[s.nextInt()];
        System.out.println("Digite o número da opção escolhida:");
        System.out.println("1 - Cadastrar aluno;");
        System.out.println("2 - Excluir aluno por nome;");
        System.out.println("3 - Listar alunos;");
        System.out.println("4 - Matricular aluno em disciplina;");
        System.out.println("5 - Cancelar disciplina;");
        System.out.println("6 - Imprimir lista de alunos e disciplinas matriculadas");


    }

}

I should associate each of the options with some methods, but I crashed the first one because I did not understand the signature that was requested.

public static void cadastrarAluno(Aluno aluno){}

The student object passed as a parameter left me confused, I do not know how to use it. In my understanding, I should have a method with no parameters, instantiate a aluno object at the beginning and ask the user through prints on the screen to inform the value of each attribute of my object. >

I do not know if I left my doubt clear, but as it is the user who will decide to execute the method, I do not know how this parameter will be passed.

    
asked by anonymous 24.05.2016 / 17:02

1 answer

1

The exercise requirement seems to make little or no sense. If the method is to enroll the student, he should do the whole, he should not demand that he pass an object to him. I would rather not use this signature and just return the object created within this method (if that is required). I would go by the way you are thinking and justify decision. If the teacher wants to test his ability to initialize the object outside the method, he should do a well-written exercise. I hope at least it's just another bad internet course and nothing official.

If you insist on this, you can create an empty object (in a possibly invalid state), or create it with dummy data (which I think is worse).

One way:

Aluno aluno = null;
cadastrarAluno(aluno);

Here you meet the signature requirement. Within the method you have to create the object in the received parameter

If you want to create the object before calling without the method being forced to initialize the variable (something worse) you can do it:

Aluno aluno = new Aluno();
cadastrarAluno(aluno);

But in this case you would need to have a parameterless constructor in the class, which does not happen now. You may have a requirement preventing this. I think it's bad that you have this constructor because it allows you to have an object in an invalid state and you would even have trouble identifying what is happening. The null at least makes clear the invalidity of the state.

The last option would be:

Aluno aluno = new Aluno(null, null, null, 0, 0);
cadastrarAluno(aluno);

Obviously, the student's parameter in the method already works as a local variable declared in the method. Its initialization will occur in the method call, so you do not need to create a variable to manipulate this object, this variable is already the parameter.

Try to understand what the variable is and what the value is. Contrary to popular belief among beginners in programming, they are different things. An argument passing to a parameter is passing a value.

The new operator is used to create an object, that is, create a value by reference.

The best signature would be:

public static Aluno cadastrarAluno()
    
24.05.2016 / 17:42