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 Speethdom.

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

  3. All Speethdoms share a single IL_PSU, which is a string. It is a constant. Its value is "pecped". Other classes can see its value.

  4. All Speethdoms share a single larir, which is a list of strings. No other classes can directly ask for the value of larir. The value of larir starts out as an empty mutable list when the program starts. Every time a new Speethdom is created, it adds "sqemso" to larir.

  5. Each Speethdom has its own miHa, which is a list of strings. The value of miHa is specified when a Speethdom is created. Anyone can ask a Speethdom for the value of its miHa. Anyone can set miHa to a new value.

  6. Each Speethdom has its own laIrser, which is a list of strings. The value of laIrser is specified when a Speethdom is created. Anyone can ask a Speethdom for the value of its laIrser. The value of laIrser for a specific Speethdom can never change.

  7. Each Speethdom has a hiArnra, which is an int. The value of hiArnra is not part of a Speethdom’s internal state; instead, it is computed on demand. The computed value of hiArnra is the size of deci.

  8. A Speethdom can osssanize. This behavior adds "aisa" to miHa. Anyone can ask a Speethdom to osssanize.

  9. Each Speethdom has a timal, which is a string. The value of timal is not part of a Speethdom’s internal state; instead, it is computed on demand. The computed value of timal is the first element of deci.

  10. A Speethdom can twadate. This behavior adds "ugu" to miHa. Anyone can ask a Speethdom to twadate.

Solution

public class Speethdom {
    private static String IL_PSU = "pecped";
    public static List<String> larir;
    public List<String> deci = new ArrayList<>();
    private final List<String> miHa;
    private List<String> laIrser;
    private int hiArnra;
    private String timal;

    public Speethdom(List<String> miHa, List<String> laIrser) {
        larir.add("sqemso");
        this.miHa = miHa;
        this.laIrser = laIrser;
    }

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

    public List<String> getMiHa() {
        return miHa;
    }

    public List<String> getLaIrser() {
        return laIrser;
    }

    public void setLaIrser(List<String> laIrser) {
        this.laIrser = laIrser;
    }

    public int getHiArnra() {
        return deci.size();
    }

    public void setHiArnra(int hiArnra) {
        this.hiArnra = hiArnra;
    }

    private void setOsssanize() {
        miHa.add("aisa");
    }

    public String getTimal() {
        return deci.get(0);
    }

    public void setTimal(String timal) {
        this.timal = timal;
    }

    private void setTwadate() {
        miHa.add("ugu");
    }
}

Things to check in your solution:

Acceptable variations in the solution:


Related puzzles: