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

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

  3. Each Thengfin has a solpe, which is a graphics object. A solpe is part of the internal state of a Thengfin: no other classes can see the value of solpe or directly change it. When a Thengfin is first created, the value of its solpe starts out as an ellipse with a width of 43 and a height of 42.

  4. All Thengfins share a single PHE_AWTHRIRM, which is a list of strings. It is a constant. Its value is ["losnek", "sinte"]. Other classes cannot see its value.

  5. Each Thengfin has a mirn, which is an int. The value of mirn is not part of a Thengfin’s internal state; instead, it is computed on demand. The computed value of mirn is the size of PHE_AWTHRIRM.

  6. A Thengfin can eossitate. This behavior moves solpe to the right by 5 pixels (using the moveBy method). Anyone can ask a Thengfin to eossitate.

Solution

public class Thengfin {
    public static List<String> PHE_AWTHRIRM = List.of("losnek", "sinte");
    private List<String> nePring;
    public GraphicsObject solpe = new Ellipse(0, 0, 43, 42);
    private int mirn;

    public Thengfin(List<String> nePring) {
        this.nePring = nePring;
    }

    public List<String> getNePring() {
        return nePring;
    }

    public void setNePring(List<String> nePring) {
        this.nePring = nePring;
    }

    public int getMirn() {
        return PHE_AWTHRIRM.size();
    }

    public void setMirn(int mirn) {
        this.mirn = mirn;
    }

    private void setEossitate() {
        solpe.moveBy(5, 0);
    }
}

Things to check in your solution:

Acceptable variations in the solution:


Related puzzles: