How to call a method within a System.out.println?

Asked

Viewed 1,982 times

2

It has how to call a method within a System.out.println(" ");?

Follows my code:

package ExerciciosReferenciaDeObjetos;

    public class Aluno {
        private String nome;
        private int matricula;
        private float n1,n2,media;




    public void Aluno(String nome, int matricula, float n1, float n2){
        this.nome=nome;
        this.matricula=matricula;
        this.n1=n1;
        this.n2=n2;
    }

    public void  Media(float n1, float n2){
        float Media;
        Media= (n1+n2)/2;
    }


    public void Exibir(){
        System.out.println("Nome:"+this.nome);
        System.out.println("Matrícula:"+this.matricula);
        System.out.println("Nota 1:"+this.n1);
        System.out.println("Nota 2:"+this.n2);
        System.out.println("A média é:"+this.media()); /**no caso estou querendo chamar o método da média dentro do system.out... , mas está dando erro**/ 


    }
  • Did any of the answers solve your question? Do you think you can accept one of them? Check out the [tour] how to do this, if you haven’t already. You would help the community by identifying what was the best solution for you. You can accept only one of them. But you can vote on any question or answer you find useful on the entire site (when you have enough score).

2 answers

5


The code has several errors. There is the name of the method that is declared as uppercase, which is not ideal and is calling lowercase. He’s getting parameters that aren’t even needed, but the call doesn’t use them. Not returning a value and just so you can use that value somewhere else. The constructor is also wrong. I improved a few more things.

class Main {
    public static void main (String[] args) {
        Aluno aluno = new Aluno("joao", 1, 8f, 6f);
        aluno.exibir();
    }
}

class Aluno {
    private String nome;
    private int matricula;
    private float n1, n2;

    public Aluno(String nome, int matricula, float n1, float n2) {
        this.nome = nome;
        this.matricula = matricula;
        this.n1 = n1;
        this.n2 = n2;
    }
    
    public float media() {
        return (n1 + n2) / 2;
    }
    
    public void exibir() {
        System.out.println("Nome:" +nome);
        System.out.println("Matrícula:" + matricula);
        System.out.println("Nota 1:" + n1);
        System.out.println("Nota 2:" + n2);
        System.out.println("A média é:" + media());
    }
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

1

Yes there is but in that case it will not work because your methods are as void you would have to put the methods of the return msm type of it and it is clad add a Return, I will show you how would be the media class for example

public float  Media(float n1, float n2){
float Media;
Media = (n1+n2)/2;
return Media;
   }
  • after I took out the void it is giving that error in the line I call the average : "the method media() is Undefined for the type Student "

  • 1

    @italovinicius already gave a full answer and showed working, take a look.

Browser other questions tagged

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