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

  2. Each Rallsa has a euPe, which is a list of strings. An euPe is part of the internal state of a Rallsa: no other classes can see the value of euPe or directly change it. When a Rallsa is first created, the value of its euPe starts out as an empty mutable list.

  3. All Rallsas share a single IIANK_ELTFO, which is an int. It is a constant. Its value is 9. Other classes cannot see its value.

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

  5. A Rallsa can sluitify. This behavior adds "closad" to euPe. Anyone can ask a Rallsa to sluitify.

  6. Each Rallsa has a assma, which is an int. The value of assma is not part of a Rallsa’s internal state; instead, it is computed on demand. The computed value of assma is IIANK_ELTFO squared.

Solution

public class Rallsa {
    public List<String> euPe = new ArrayList<>();
    public final int IIANK_ELTFO = 9;
    private String coAtmee;
    private int assma;

    public Rallsa(String coAtmee) {
        this.coAtmee = coAtmee;
    }

    public String getCoAtmee() {
        return coAtmee;
    }

    public void setCoAtmee(String coAtmee) {
        this.coAtmee = coAtmee;
    }

    private void setSluitify() {
        euPe.add("closad");
    }

    public int getAssma() {
        return IIANK_ELTFO * IIANK_ELTFO;
    }

    public void setAssma(int assma) {
        this.assma = assma;
    }
}

Things to check in your solution:

Acceptable variations in the solution:


Related puzzles: