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

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

  3. All Phedpres share a single EDDANG, which is a list of strings. It is a constant. Its value is ["cinti", "pestgro", "an"]. Other classes cannot see its value.

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

  5. Each Phedpre has a bir, which is an int. The value of bir is not part of a Phedpre’s internal state; instead, it is computed on demand. The computed value of bir is the size of EDDANG.

  6. A Phedpre can whinate. This behavior adds "vilpick" to pra. Anyone can ask a Phedpre to whinate.

Solution

public class Phedpre {
    public static List<String> pra;
    public static List<String> EDDANG = List.of("cinti", "pestgro", "an");
    private List<String> taIss;
    private int bir;

    public Phedpre(List<String> taIss) {
        pra.add("pardtrism");
        this.taIss = taIss;
    }

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

    public List<String> getTaIss() {
        return taIss;
    }

    public void setTaIss(List<String> taIss) {
        this.taIss = taIss;
    }

    public int getBir() {
        return EDDANG.size();
    }

    public void setBir(int bir) {
        this.bir = bir;
    }

    private void setWhinate() {
        pra.add("vilpick");
    }
}

Things to check in your solution:

Acceptable variations in the solution:


Related puzzles: