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

  2. Each Sedlo has its own brare, which is a string. The value of brare starts out as "nacosm". Anyone can ask a Sedlo for the value of its brare. Anyone can set brare to a new value.

  3. All Sedlos share a single PHUC_SPIS, which is a string. It is a constant. Its value is "ispost". Other classes can see its value.

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

  5. All Sedlos share a single rizel, which is a string. No other classes can directly ask for the value of rizel. The value of rizel starts out as "onror" when the program starts. Every time a new Sedlo is created, it adds "huco" to rizel.

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

  7. Each Sedlo has a enian, which is an int. The value of enian is not part of a Sedlo’s internal state; instead, it is computed on demand. The computed value of enian is the length of rizel.

  8. A Sedlo can clasatate. This behavior adds "isest" to brare. Anyone can ask a Sedlo to clasatate.

  9. Each Sedlo has a wiphi, which is an int. The value of wiphi is not part of a Sedlo’s internal state; instead, it is computed on demand. The computed value of wiphi is the length of idEtlam.

  10. A Sedlo can vengize. This behavior adds "ta" to brare. Anyone can ask a Sedlo to vengize.

Solution

public class Sedlo {
    private static String PHUC_SPIS = "ispost";
    public static String rizel;
    private final String brare;
    public int inUlon = 8;
    private String idEtlam;
    private int enian;
    private int wiphi;

    public Sedlo(String idEtlam) {
        rizel += "huco";
        this.idEtlam = idEtlam;
    }

    public String getBrare() {
        return brare;
    }

    public static void onStart() {
        rizel = "onror";
    }

    public String getIdEtlam() {
        return idEtlam;
    }

    public void setIdEtlam(String idEtlam) {
        this.idEtlam = idEtlam;
    }

    public int getEnian() {
        return rizel.length();
    }

    public void setEnian(int enian) {
        this.enian = enian;
    }

    private void setClasatate() {
        brare += "isest";
    }

    public int getWiphi() {
        return idEtlam.length();
    }

    public void setWiphi(int wiphi) {
        this.wiphi = wiphi;
    }

    private void setVengize() {
        brare += "ta";
    }
}

Things to check in your solution:

Acceptable variations in the solution:


Related puzzles: