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

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

  3. All Scastils share a single asAn, which is a string. No other classes can directly ask for the value of asAn. The value of asAn starts out as "vaph" when the program starts. Every time a new Scastil is created, it adds "besm" to asAn.

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

  5. All Scastils share a single WAMI_ER, which is an int. It is a constant. Its value is 12. Other classes cannot see its value.

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

  7. A Scastil can tasatify. This behavior adds 1 to thel. Anyone can ask a Scastil to tasatify.

  8. Each Scastil has a enIs, which is an int. The value of enIs is not part of a Scastil’s internal state; instead, it is computed on demand. The computed value of enIs is thel plus 2.

  9. Each Scastil has a pec, which is an int. The value of pec is not part of a Scastil’s internal state; instead, it is computed on demand. The computed value of pec is WAMI_ER squared.

  10. A Scastil can bacsate. This behavior adds 5 to thel. Anyone can ask a Scastil to bacsate.

Solution

public class Scastil {
    public static String asAn;
    private final String thast;
    private List<String> gran;
    public final int WAMI_ER = 12;
    public int thel = 5;
    private int enIs;
    private int pec;

    public Scastil(List<String> gran) {
        asAn += "besm";
        this.gran = gran;
    }

    public String getThast() {
        return thast;
    }

    public static void onStart() {
        asAn = "vaph";
    }

    public List<String> getGran() {
        return gran;
    }

    public void setGran(List<String> gran) {
        this.gran = gran;
    }

    private void setTasatify() {
        thel += 1;
    }

    public int getEnIs() {
        return thel + 2;
    }

    public void setEnIs(int enIs) {
        this.enIs = enIs;
    }

    public int getPec() {
        return WAMI_ER * WAMI_ER;
    }

    public void setPec(int pec) {
        this.pec = pec;
    }

    private void setBacsate() {
        thel += 5;
    }
}

Things to check in your solution:

Acceptable variations in the solution:


Related puzzles: