I don't understand what I failed in this introductory exercise to OOP in Java

Question:

I'm learning Java and doing a simple introductory OOP exercise. It gives me an error but I am not able to understand where I am failing, I use Netbeans as IDE. Here the class code:

public class Empleado {

// Decimos qué atributos tendrá nuestra clase
private String nombre;
private String apellido;
private int edad;
private double sueldo;

// Utilizamos los constructores para inicializar dichos atributos
public Empleado(String nombre, String apellido, int edad, double sueldo) {

    this.nombre = nombre;
    this.apellido = apellido;
    this.edad = edad;
    this.sueldo = sueldo;

}

//Le meto get y set para poder hacer cosas con dichos atributos
public String getNombre() {
    return nombre;
}

public void setNombre() {
    this.nombre = nombre;
}

public String getApellido() {
    return apellido;
}

public void setApellido() {
    this.apellido = apellido;
}

public int getEdad() {
    return edad;
}

public void setEdad() {
    this.edad = edad;
}

public double getSueldo() {
    return sueldo;
}

public void setSueldo() {
    this.sueldo = sueldo;
}

/**
 *
 * @return
 */
@Override
public String toString () {
    String texto = "El empleado se llama " + nombre + " " + apellido + " y tiene " + edad + " años y cobra un sueldo de " + sueldo + " euros"; 
    return texto;
}
}

And here the code of the main method:

public class Main {
public static void main(String[] args) {
    Empleado trabajador1 = new Empleado ("Pablo", "Fernández", 27, 1050);
    Empleado trabajador2 = new Empleado ("César", "Romero", 26, 1300);

    System.out.println(trabajador1.toString());
    System.out.println(trabajador2.toString());

    trabajador1.setNombre("Lucía");
    trabajador2.setEdad(23);

It gives me an error in the setters and it does not let me execute, it tells me that

"method setName in class employee cannot be applied to given types; required: no arguments; found: String; reason: actual and formal argument lists differ in lenght"

However, as much as I search the Internet, I can't find the error, since at a syntactic level I think I'm doing it right. Any ideas? Thank you.

Answer:

The setters are not receiving any parameters:

void setNombre() {
    this.nombre=nombre;
}

They should all be something like

void setNombre(String n) {
    this.nombre=n;
}

The code compiles into those classes because this. is optional, really your setters are equivalent to

void setNombre() {
    this.nombre=this.nombre; //realmente no hace nada
}

Veteran Tip: If you're using Eclipse, write your classes like this:

public class Empleado {

    // Decimos qué atributos tendrá nuestra clase
    private String nombre;
    private String apellido;
    private int edad;
    private double sueldo;
}

And then press Alt + Shift + S . In the menu that appears choose Generate Setters and Getters

Scroll to Top