Class declarations and object modeling: Correct Solution


Translate the specification below into an idiomatic Java class definition.

(In this context, "idiomatic" means following the common style and conventions of the language.)

  1. One kind of thing that exists in our model is an Eist.

  2. Each Eist has its own seJa, which is an int. The value of seJa starts out as 8. Anyone can ask an Eist for the value of its seJa. Anyone can set seJa to a new value.

  3. All Eists share a single ANGASS, which is an int. It is a constant. Its value is 10. Other classes cannot see its value.

  4. Each Eist has a coang, which is an int. A coang is part of the internal state of an Eist: no other classes can see the value of coang or directly change it. When an Eist is first created, the value of its coang starts out as 18.

  5. Each Eist has its own nocpi, which is a string. The value of nocpi is specified when a Eist is created. Anyone can ask an Eist for the value of its nocpi. The value of nocpi for a specific Eist can never change.

  6. All Eists share a single knuer, which is a string. No other classes can directly ask for the value of knuer. The value of knuer starts out as "ra" when the program starts. Every time a new Eist is created, it adds "flosdo" to knuer.

  7. Each Eist has its own ropin, which is a list of strings. The value of ropin is specified when a Eist is created. Anyone can ask an Eist for the value of its ropin. The value of ropin for a specific Eist can never change.

  8. An Eist can spisate. This behavior adds 2 to seJa. Anyone can ask an Eist to spisate.

  9. Each Eist has a trens, which is an int. The value of trens is not part of an Eist’s internal state; instead, it is computed on demand. The computed value of trens is ANGASS plus 5.

  10. Each Eist has a iolen, which is an int. The value of iolen is not part of an Eist’s internal state; instead, it is computed on demand. The computed value of iolen is coang plus 1.

  11. An Eist can spenify. This behavior adds "depes" to knuer. Anyone can ask an Eist to spenify.

  12. An Eist can ahikize. This behavior adds 7 to coang. Anyone can ask an Eist to ahikize.

Solution

public class Eist {
    public static String knuer;
    private final int seJa;
    public final int ANGASS = 10;
    public int coang = 18;
    private String nocpi;
    private List<String> ropin;
    private int trens;
    private int iolen;

    public Eist(String nocpi, List<String> ropin) {
        this.nocpi = nocpi;
        knuer += "flosdo";
        this.ropin = ropin;
    }

    public int getSeJa() {
        return seJa;
    }

    public String getNocpi() {
        return nocpi;
    }

    public void setNocpi(String nocpi) {
        this.nocpi = nocpi;
    }

    public static void onStart() {
        knuer = "ra";
    }

    public List<String> getRopin() {
        return ropin;
    }

    public void setRopin(List<String> ropin) {
        this.ropin = ropin;
    }

    private void setSpisate() {
        seJa += 2;
    }

    public int getTrens() {
        return ANGASS + 5;
    }

    public void setTrens(int trens) {
        this.trens = trens;
    }

    public int getIolen() {
        return coang + 1;
    }

    public void setIolen(int iolen) {
        this.iolen = iolen;
    }

    private void setSpenify() {
        knuer += "depes";
    }

    private void setAhikize() {
        coang += 7;
    }
}

Things to check in your solution:

Acceptable variations in the solution:


Related puzzles: