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

Has how to call a method inside a System.out.println(" ");?

Follow 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**/ 


    }
Author: Maniero, 2017-05-13

2 answers

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. It is receiving parameters that are not even needed, but in the call does not use them. Not returning a value and only then can you use that value somewhere else. The builder 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());
    }
}

See working on ideone. E no repl.it. also put in the GitHub for future reference .

 5
Author: Maniero, 2020-12-01 18:15:26

Yes it has but in this case it will not work because its methods are as void vc would have to put the MSM methods type of the return of it and is clado add a return, I'll show you how it would look middle class for example

public float  Media(float n1, float n2){
float Media;
Media = (n1+n2)/2;
return Media;
   }
 1
Author: Douglas Sabino, 2017-05-13 20:31:19