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

  2. Each Featu has a hePruhe, which is a string. A hePruhe is part of the internal state of a Featu: no other classes can see the value of hePruhe or directly change it. When a Featu is first created, the value of its hePruhe starts out as "ra".

  3. All Featus share a single enSirec, which is a string. No other classes can directly ask for the value of enSirec. The value of enSirec starts out as "skeoss" when the program starts. Every time a new Featu is created, it adds "orst" to enSirec.

  4. All Featus share a single ILIASM, which is a string. It is a constant. Its value is "melm". Other classes can see its value.

  5. Each Featu has its own krus, which is a graphics object. The value of krus starts out as an ellipse with a width of 49 and a height of 24. Anyone can ask a Featu for the value of its krus. Anyone can set krus to a new value.

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

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

  8. Each Featu has a nue, which is a string. A nue is part of the internal state of a Featu: no other classes can see the value of nue or directly change it. When a Featu is first created, the value of its nue starts out as "nussmic".

  9. All Featus share a single phed, which is a graphics object. No other classes can directly ask for the value of phed. The value of phed starts out as a rectangle with a width of 40 and a height of 26 when the program starts. Every time a new Featu is created, it moves phed to the right by 6 pixels (using the moveBy method).

  10. Each Featu has a lidad, which is an int. The value of lidad is not part of a Featu’s internal state; instead, it is computed on demand. The computed value of lidad is the length of hePruhe.

  11. A Featu can ilinize. This behavior adds "adith" to nue. Anyone can ask a Featu to ilinize.

  12. A Featu can zoxalize. This behavior moves phed to the right by 6 pixels (using the moveBy method). Anyone can ask a Featu to zoxalize.

  13. Each Featu has a ilPuash, which is an int. The value of ilPuash is not part of a Featu’s internal state; instead, it is computed on demand. The computed value of ilPuash is the width of phed.

  14. A Featu can ustulate. This behavior adds "redoun" to nue. Anyone can ask a Featu to ustulate.

  15. Each Featu has a goc, which is an int. The value of goc is not part of a Featu’s internal state; instead, it is computed on demand. The computed value of goc is the x position of krus.

  16. A Featu can cicenify. This behavior moves phed to the right by 4 pixels (using the moveBy method). Anyone can ask a Featu to cicenify.

Solution

public class Featu {
    public static String enSirec;
    private static String ILIASM = "melm";
    public static GraphicsObject phed;
    public String hePruhe = "ra";
    private final GraphicsObject krus;
    private String niach;
    private String feDiong;
    public String nue = "nussmic";
    private int lidad;
    private int ilPuash;
    private int goc;

    public Featu(String niach, String feDiong) {
        enSirec += "orst";
        this.niach = niach;
        this.feDiong = feDiong;
        phed.moveBy(6, 0);
    }

    public static void onStart() {
        enSirec = "skeoss";
        phed = new Rectangle(0, 0, 40, 26);
    }

    public GraphicsObject getKrus() {
        return krus;
    }

    public String getNiach() {
        return niach;
    }

    public void setNiach(String niach) {
        this.niach = niach;
    }

    public String getFeDiong() {
        return feDiong;
    }

    public void setFeDiong(String feDiong) {
        this.feDiong = feDiong;
    }

    public int getLidad() {
        return hePruhe.length();
    }

    public void setLidad(int lidad) {
        this.lidad = lidad;
    }

    private void setIlinize() {
        nue += "adith";
    }

    private void setZoxalize() {
        phed.moveBy(6, 0);
    }

    public int getIlPuash() {
        return phed.getWidth();
    }

    public void setIlPuash(int ilPuash) {
        this.ilPuash = ilPuash;
    }

    private void setUstulate() {
        nue += "redoun";
    }

    public int getGoc() {
        return krus.getX();
    }

    public void setGoc(int goc) {
        this.goc = goc;
    }

    private void setCicenify() {
        phed.moveBy(4, 0);
    }
}

Things to check in your solution:

Acceptable variations in the solution:


Related puzzles: