Problem returning a method’s value

Asked

Viewed 123 times

1

I am studying the book of Deitel Java how to program and I came across an exercise that asks to make a wage increase of 10%, my doubt is in the method of adjustment, my goal is to show on the console screen the salary of the two employees before the adjustment and then with the increase.

Employee:

public class Empregado {

    String nome, sobrenome;
    Double salarioMensal,convencao;

    public Empregado(String nomeE, String sobrenomeE, double salarioMensalE){
        if(salarioMensalE > 0.0){
            nome = nomeE;
            sobrenome = sobrenomeE;
            salarioMensal = salarioMensalE;
        }else{
            System.out.println("Salário menor que 0");
        }
    }

    //Método de reajuste salarial
    public void reajustaSalario(double valorDoReajuste){ 
        valorDoReajuste = salarioMensal+(salarioMensal*10/100); 
    } 

    public Double getConvencao() {
        return convencao;
    }

    public String getNome() {
        return nome;
    }

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

    public String getSobrenome() {
        return sobrenome;
    }

    public void setSobrenome(String sobrenome) {
        this.sobrenome = sobrenome;
    }

    public Double getSalarioMensal() {
        return salarioMensal;
    }

    public void setSalarioMensal(Double salarioMensal) {
        this.salarioMensal = salarioMensal;
    }
}

Waitress:

import java.util.Scanner;

public class EmpregadoTeste {
    public static void main(String[] args) {

        Empregado salario1 = new Empregado("Joao","Antonio",100.00);
        Empregado salario2 = new Empregado("Carlos","Frodo",50.00);

        System.out.printf("Empregado 1 salario: %s %s $%.2f\n",salario1.getNome(),salario1.getSobrenome(),salario1.getSalarioMensal());
        System.out.printf("Empregado 2 salario: %s %s $%.2f\n\n", salario2.getNome(),salario2.getSobrenome(),salario2.getSalarioMensal());

        //Minha dúvida procede aqui, como colocar o método do Salário reajustado?
        System.out.printf("Salario 1 atualizado: %.2f \n",        ); 
        System.out.printf("Salario 2 atualizado: %.2f ",          ); 

    }
}

1 answer

2


I believe that your problem lies in the way the method reajustaSalario is declared.
I suggest you do it this way:

public double reajustaSalario(double valorDoReajuste){ 

    return salarioMensal + (salarioMensal * valorDoReajuste/100); 

}  

It can then be used as follows:

System.out.printf("Salario 1 atualizado: %.2f \n",salario1.reajustaSalario(10));
  • One parenthesis missing at the end of the second code snippet.

  • Thank you very much my doubt was on how to call the adjustment method, just add a Re-turn. It worked !!! Hug ;)

  • @Jeangustavoprates Correction made. thanks.

Browser other questions tagged

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