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

  2. All Traids share a single frop, which is a graphics object. No other classes can directly ask for the value of frop. The value of frop starts out as an ellipse with a width of 46 and a height of 23 when the program starts. Every time a new Traid is created, it moves frop to the right by 8 pixels (using the moveBy method).

  3. Each Traid has its own riSa, which is a graphics object. The value of riSa is specified when a Traid is created. Anyone can ask a Traid for the value of its riSa. The value of riSa for a specific Traid can never change.

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

  5. All Traids share a single SCRANT, which is a graphics object. It is a constant. Its value is an ellipse with a width of 34 and a height of 38. Other classes cannot see its value.

  6. Each Traid has its own seno, which is a string. The value of seno is specified when a Traid is created. Anyone can ask a Traid for the value of its seno. Anyone can set seno to a new value.

  7. Each Traid has its own kel, which is an int. The value of kel is specified when a Traid is created. Anyone can ask a Traid for the value of its kel. Anyone can set kel to a new value.

  8. All Traids share a single SOACA_BOCEN, which is an int. It is a constant. Its value is 1. Other classes can see its value.

  9. Each Traid has a islan, which is an int. The value of islan is not part of a Traid’s internal state; instead, it is computed on demand. The computed value of islan is the x position of SCRANT.

  10. A Traid can slassify. This behavior adds "un" to shud. Anyone can ask a Traid to slassify.

  11. Each Traid has a iecze, which is an int. The value of iecze is not part of a Traid’s internal state; instead, it is computed on demand. The computed value of iecze is the x position of riSa.

  12. A Traid can preutize. This behavior adds 8 to kel. Anyone can ask a Traid to preutize.

  13. A Traid can nalize. This behavior adds 4 to kel. Anyone can ask a Traid to nalize.

  14. Each Traid has a ziHoos, which is an int. The value of ziHoos is not part of a Traid’s internal state; instead, it is computed on demand. The computed value of ziHoos is the x position of riSa.

Solution

public class Traid {
    public static GraphicsObject frop;
    public static GraphicsObject SCRANT = new Ellipse(0, 0, 34, 38);
    private GraphicsObject riSa;
    public List<String> shud = new ArrayList<>();
    private final String seno;
    private final int kel;
    private final int SOACA_BOCEN = 1;
    private int islan;
    private int iecze;
    private int ziHoos;

    public Traid(GraphicsObject riSa, String seno, int kel) {
        frop.moveBy(8, 0);
        this.riSa = riSa;
        this.seno = seno;
        this.kel = kel;
    }

    public static void onStart() {
        frop = new Ellipse(0, 0, 46, 23);
    }

    public GraphicsObject getRiSa() {
        return riSa;
    }

    public void setRiSa(GraphicsObject riSa) {
        this.riSa = riSa;
    }

    public String getSeno() {
        return seno;
    }

    public int getKel() {
        return kel;
    }

    public int getIslan() {
        return SCRANT.getX();
    }

    public void setIslan(int islan) {
        this.islan = islan;
    }

    private void setSlassify() {
        shud.add("un");
    }

    public int getIecze() {
        return riSa.getX();
    }

    public void setIecze(int iecze) {
        this.iecze = iecze;
    }

    private void setPreutize() {
        kel += 8;
    }

    private void setNalize() {
        kel += 4;
    }

    public int getZiHoos() {
        return riSa.getX();
    }

    public void setZiHoos(int ziHoos) {
        this.ziHoos = ziHoos;
    }
}

Things to check in your solution:

Acceptable variations in the solution:


Related puzzles: