Search code examples
javagetter-setter

My professor skiped a variable in the setter, did he make a mistake in the code he wrote?


Our professor has given us exercises, and given us the code how they should've been done, so there's something I don't get, I have sent him an email a week ago, but he didn't reply, so I'm turning to stackoverflow, where I don't get is in the comments:

public class Futbollisti {

private String emri, pozita;
private int mosha;

public Futbollisti(String e, String p, int m){
    emri = e;
    pozita = p;
    mosha = m;

}
public String getEmri(){
    return emri;

}
public String getPozita(){
    return pozita;
}
public int getMosha(){
    return mosha;
}
public void setPozita(String p){ /*right about here, why did he skip the 
'emri', he went straight to pozita, but didn't set emri, did he do a 
mistake? */
    pozita = p;

}
public void setMosha(int m){
    mosha = m;
}
public String toString(){
    return emri + " : " + mosha + " - " + pozita;

}
public boolean equals(Object obj){
    if(obj instanceof Futbollisti){
    Futbollisti f = (Futbollisti) obj;
        return emri.equals(f.getEmri())
                && pozita.equals(f.getPozita())
                && mosha == f.getMosha();

}
    return false;
}
}

In the exercise it said to make a constructor that accepts those variables (the emri, pozita, mosha), and initialize them, then offer the get and set methods for the attributes needed, and offer a method that represents a string in an object of 'Futbollisti' class in the format: 'emri : mosha - pozita' And, to offer a method for comparing 2 objects of Futbollsti, which all were done, but I don't know if he should've left out the 'emri'. So is the code correct or did he make a mistake?


Solution

  • Setter methods are omitted if the developer wants to make a field read-only.

    By the description of your requirements:

    make a constructor that accepts those variables (the emri, pozita, mosha), and initialize them, then offer the get and set methods for the attributes needed

    it's not likely a read-only field, so he made a mistake by forgetting the setter.