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

  2. All Tands share a single ALRONUSER, which is a string. It is a constant. Its value is "chra". Other classes can see its value.

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

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

  5. A Tand can gialize. This behavior adds "preplar" to soic. Anyone can ask a Tand to gialize.

  6. Each Tand has a edte, which is an int. The value of edte is not part of a Tand’s internal state; instead, it is computed on demand. The computed value of edte is the size of cri.

Solution

public class Tand {
    private static String AL_RO_NUSER = "chra";
    public static List<String> soic;
    private List<String> cri;
    private int edte;

    public Tand(List<String> cri) {
        this.cri = cri;
        soic.add("fe");
    }

    public List<String> getCri() {
        return cri;
    }

    public void setCri(List<String> cri) {
        this.cri = cri;
    }

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

    private void setGialize() {
        soic.add("preplar");
    }

    public int getEdte() {
        return cri.size();
    }

    public void setEdte(int edte) {
        this.edte = edte;
    }
}

Things to check in your solution:

Acceptable variations in the solution:


Related puzzles: