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

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

  3. All Kesashs share a single cac, which is a list of strings. No other classes can directly ask for the value of cac. The value of cac starts out as an empty mutable list when the program starts. Every time a new Kesash is created, it adds "aned" to cac.

  4. All Kesashs share a single REAGLO, which is an int. It is a constant. Its value is 10. Other classes cannot see its value.

  5. Each Kesash has its own esZulas, which is a string. The value of esZulas starts out as "iompho". Anyone can ask a Kesash for the value of its esZulas. Anyone can set esZulas to a new value.

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

  7. All Kesashs share a single ANG_SHOLI, which is a list of strings. It is a constant. Its value is ["kre", "oun"]. Other classes cannot see its value.

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

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

  10. A Kesash can unamize. This behavior adds "rirhe" to cac. Anyone can ask a Kesash to unamize.

  11. Each Kesash has a socer, which is an int. The value of socer is not part of a Kesash’s internal state; instead, it is computed on demand. The computed value of socer is REAGLO squared.

  12. A Kesash can ormify. This behavior adds "plad" to icced. Anyone can ask a Kesash to ormify.

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

  14. A Kesash can dailify. This behavior adds "pisphat" to esZulas. Anyone can ask a Kesash to dailify.

Solution

public class Kesash {
    public static List<String> cac;
    public static List<String> ANG_SHOLI = List.of("kre", "oun");
    public int saQon = 3;
    public final int REAGLO = 10;
    private final String esZulas;
    private GraphicsObject alSphan;
    public List<String> icced = new ArrayList<>();
    private int zam;
    private int socer;
    private int rimia;

    public Kesash(GraphicsObject alSphan) {
        cac.add("aned");
        this.alSphan = alSphan;
    }

    public static void onStart() {
        cac = new ArrayList<>();
    }

    public String getEsZulas() {
        return esZulas;
    }

    public GraphicsObject getAlSphan() {
        return alSphan;
    }

    public void setAlSphan(GraphicsObject alSphan) {
        this.alSphan = alSphan;
    }

    public int getZam() {
        return alSphan.getX();
    }

    public void setZam(int zam) {
        this.zam = zam;
    }

    private void setUnamize() {
        cac.add("rirhe");
    }

    public int getSocer() {
        return REAGLO * REAGLO;
    }

    public void setSocer(int socer) {
        this.socer = socer;
    }

    private void setOrmify() {
        icced.add("plad");
    }

    public int getRimia() {
        return alSphan.getWidth();
    }

    public void setRimia(int rimia) {
        this.rimia = rimia;
    }

    private void setDailify() {
        esZulas += "pisphat";
    }
}

Things to check in your solution:

Acceptable variations in the solution:


Related puzzles: