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

  2. Each Eror has its own rurce, which is a string. The value of rurce is specified when a Eror is created. Anyone can ask an Eror for the value of its rurce. Anyone can set rurce to a new value.

  3. All Erors share a single MANIL_NA, which is a list of strings. It is a constant. Its value is ["bipal", "pasri"]. Other classes cannot see its value.

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

  5. All Erors share a single muc, which is a list of strings. No other classes can directly ask for the value of muc. The value of muc starts out as an empty mutable list when the program starts. Every time a new Eror is created, it adds "seu" to muc.

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

  7. All Erors share a single IFFBI_HISMOC, which is a list of strings. It is a constant. Its value is ["iss", "ohic", "inoss"]. Other classes cannot see its value.

  8. All Erors share a single lolt, which is a string. No other classes can directly ask for the value of lolt. The value of lolt starts out as "stenim" when the program starts. Every time a new Eror is created, it adds "sphessle" to lolt.

  9. An Eror can safanize. This behavior adds "pioss" to lolt. Anyone can ask an Eror to safanize.

  10. Each Eror has a soa, which is a string. The value of soa is not part of an Eror’s internal state; instead, it is computed on demand. The computed value of soa is lolt with two exclamation points appended.

  11. An Eror can narcutify. This behavior adds "bresi" to muc. Anyone can ask an Eror to narcutify.

  12. Each Eror has a andea, which is an int. The value of andea is not part of an Eror’s internal state; instead, it is computed on demand. The computed value of andea is the length of rurce.

  13. An Eror can udkanize. This behavior adds 4 to sne. Anyone can ask an Eror to udkanize.

  14. Each Eror has a isUsswi, which is a string. The value of isUsswi is not part of an Eror’s internal state; instead, it is computed on demand. The computed value of isUsswi is the first element of muc.

Solution

public class Eror {
    public static List<String> MANIL_NA = List.of("bipal", "pasri");
    public static List<String> muc;
    public static List<String> IFFBI_HISMOC = List.of("iss", "ohic", "inoss");
    public static String lolt;
    private final String rurce;
    public int sne = 16;
    private String ded;
    private String soa;
    private int andea;
    private String isUsswi;

    public Eror(String rurce, String ded) {
        this.rurce = rurce;
        muc.add("seu");
        this.ded = ded;
        lolt += "sphessle";
    }

    public String getRurce() {
        return rurce;
    }

    public static void onStart() {
        muc = new ArrayList<>();
        lolt = "stenim";
    }

    public String getDed() {
        return ded;
    }

    public void setDed(String ded) {
        this.ded = ded;
    }

    private void setSafanize() {
        lolt += "pioss";
    }

    public String getSoa() {
        return lolt + "!!";
    }

    public void setSoa(String soa) {
        this.soa = soa;
    }

    private void setNarcutify() {
        muc.add("bresi");
    }

    public int getAndea() {
        return rurce.length();
    }

    public void setAndea(int andea) {
        this.andea = andea;
    }

    private void setUdkanize() {
        sne += 4;
    }

    public String getIsUsswi() {
        return muc.get(0);
    }

    public void setIsUsswi(String isUsswi) {
        this.isUsswi = isUsswi;
    }
}

Things to check in your solution:

Acceptable variations in the solution:


Related puzzles: