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 a Pacbean.

  2. All Pacbeans share a single ueri, which is a list of strings. No other classes can directly ask for the value of ueri. The value of ueri starts out as an empty mutable list when the program starts. Every time a new Pacbean is created, it adds "iansse" to ueri.

  3. Each Pacbean has its own aish, which is a string. The value of aish is specified when a Pacbean is created. Anyone can ask a Pacbean for the value of its aish. The value of aish for a specific Pacbean can never change.

  4. All Pacbeans share a single FEL_IBI, which is a string. It is a constant. Its value is "peec". Other classes can see its value.

  5. Each Pacbean has a leEgia, which is a list of strings. A leEgia is part of the internal state of a Pacbean: no other classes can see the value of leEgia or directly change it. When a Pacbean is first created, the value of its leEgia starts out as an empty mutable list.

  6. Each Pacbean has a kimi, which is a string. The value of kimi is not part of a Pacbean’s internal state; instead, it is computed on demand. The computed value of kimi is the first element of ueri.

  7. A Pacbean can eepize. This behavior adds "rios" to ueri. Anyone can ask a Pacbean to eepize.

  8. Each Pacbean has a olDisse, which is an int. The value of olDisse is not part of a Pacbean’s internal state; instead, it is computed on demand. The computed value of olDisse is the size of leEgia.

Solution

public class Pacbean {
    public static List<String> ueri;
    private static String FEL_IBI = "peec";
    private String aish;
    public List<String> leEgia = new ArrayList<>();
    private String kimi;
    private int olDisse;

    public Pacbean(String aish) {
        ueri.add("iansse");
        this.aish = aish;
    }

    public static void onStart() {
        ueri = new ArrayList<>();
    }

    public String getAish() {
        return aish;
    }

    public void setAish(String aish) {
        this.aish = aish;
    }

    public String getKimi() {
        return ueri.get(0);
    }

    public void setKimi(String kimi) {
        this.kimi = kimi;
    }

    private void setEepize() {
        ueri.add("rios");
    }

    public int getOlDisse() {
        return leEgia.size();
    }

    public void setOlDisse(int olDisse) {
        this.olDisse = olDisse;
    }
}

Things to check in your solution:

Acceptable variations in the solution:


Related puzzles: