Java returning 0 in multiplication

Asked

Viewed 112 times

1

The code is even simple to calculate a seller’s yield by multiplying in the case of 150000 * 0.1 it returns 0

code /

public class FuncionarioComissionado {
//Strings private para poder usar Get/Set
//Atributos
private String nome;
private String CPF;
private double taxaComissao;
private double vendasBrutas;
private double rendimentos;


// Criação de um construtor para a classe FuncionarioComissionado
// Construtor
public FuncionarioComissionado(String nome, String CPF, double taxaComissao, double vendasBrutas) {
	this.nome = nome;
	this.CPF = CPF;
	this.taxaComissao = taxaComissao;
	this.vendasBrutas = vendasBrutas;
}
// Calculo dos Rendimentos
//Metodo
private void Rendimentos(){
	rendimentos = (vendasBrutas * (taxaComissao / 100));
}

// Criação de Get's para cada Atributo
// Metodos
public double getRendimentos() {
	return rendimentos;
}
public String getNome() {
	return nome;
}
public String getCPF() {
	return CPF;
}
public double getTaxaComissao() {
	return taxaComissao / 100;
}
public double getVendasBrutas() {
	return vendasBrutas;
}
}

test /

FuncionarioComissionado f1 = new FuncionarioComissionado("Guilherme", "12345678921",10,125000);
    System.out.println(f1.getNome());
    System.out.println(f1.getCPF());
    System.out.println(f1.getTaxaComissao());
    System.out.println(f1.getVendasBrutas());
    System.out.println(f1.getRendimentos());
  • At what point the error occurs?

  • trying to figure out but the error would be getRequests returning 0 instead of (125000 * 0.1)

  • You are not calculating yields, you are only returning an empty value of the variable.

  • This account never comes to pass, because you just don’t do it.

  • Wouldn’t that make the account? private void Yields(){ Yields = (saleBrutas * (taxCompled / 100));

  • Reread my last 2 comments again.

  • ah ! Facepalm, I did not invoke the T_T method

  • Good in class son worked but in this same calling method returned 0

Show 3 more comments

1 answer

3


Returns 0 because your method getRendimentos() is simply returning the initialization value of the variable rendimentos, at any time of the code you perform calculation.

I suggest two modifications to the code:

  • do not start method names with uppercase letter, java convention asks that only the name of the constructor can be capitalized, and the other methods follow the format camelcase;

  • if you need to store the proceeds separately, first call the method you calculate and then call the getter from it.

With these changes, the class stays this way:

class FuncionarioComissionado {
    //Strings private para poder usar Get/Set
    //Atributos
    private String nome;
    private String CPF;
    private double taxaComissao;
    private double vendasBrutas;
    private double rendimentos;


    // Criação de um construtor para a classe FuncionarioComissionado
    // Construtor
    public FuncionarioComissionado(String nome, String CPF, double taxaComissao, double vendasBrutas) {
        this.nome = nome;
        this.CPF = CPF;
        this.taxaComissao = taxaComissao;
        this.vendasBrutas = vendasBrutas;
    }

    // Calculo dos Rendimentos
    //Metodo
    public void calculaRendimentos(){
        rendimentos = (vendasBrutas * (taxaComissao / 100));
    }

    // Criação de Get's para cada Atributo
    // Metodos
    public double getRendimentos() {
        return rendimentos;
    }
    public String getNome() {
        return nome;
    }
    public String getCPF() {
        return CPF;
    }
    public double getTaxaComissao() {
        return taxaComissao / 100;
    }
    public double getVendasBrutas() {
        return vendasBrutas;
    }
}

The test:

class Teste {

    public static void main (String[] args) {

    FuncionarioComissionado f1 = new FuncionarioComissionado("Guilherme", "12345678921",10,125000);
    System.out.println(f1.getNome());
    System.out.println(f1.getCPF());
    System.out.println(f1.getTaxaComissao());
    System.out.println(f1.getVendasBrutas());
    f1.calculaRendimentos();
    System.out.println(f1.getRendimentos());

    }
}

See working on ideone

  • 1

    Thank you I understood what was missing with the help of your comments, but I was wrong in the construction of the code, the () in F1.calculations();

  • It would be even better to calculate the earnings in the builder, as there is no reason to leave this for later when a specific method is called.

  • Ideal would be but I don’t know how to do this >.<

  • @guilhermecabrals Just put the line rendimentos = (vendasBrutas * (taxaComissao / 100)); at the end of the constructor and then you can delete the method calculaRendimentos (or Rendimentos according to its original code) and all calls to it.

Browser other questions tagged

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