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 an Oussstad.

  2. All Oussstads share a single taCokpe, which is a list of strings. No other classes can directly ask for the value of taCokpe. The value of taCokpe starts out as an empty mutable list when the program starts. Every time a new Oussstad is created, it adds "scor" to taCokpe.

  3. Each Oussstad has its own sigi, which is an int. The value of sigi starts out as 3. Anyone can ask an Oussstad for the value of its sigi. Anyone can set sigi to a new value.

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

  5. Each Oussstad has its own giVoss, which is an int. The value of giVoss is specified when a Oussstad is created. Anyone can ask an Oussstad for the value of its giVoss. The value of giVoss for a specific Oussstad can never change.

  6. All Oussstads share a single DESSTO, which is a list of strings. It is a constant. Its value is ["halho", "sarplon"]. Other classes cannot see its value.

  7. All Oussstads share a single CLASSPI, which is a graphics object. It is a constant. Its value is a rectangle with a width of 36 and a height of 44. Other classes cannot see its value.

  8. An Oussstad can psarize. This behavior adds "fu" to taCokpe. Anyone can ask an Oussstad to psarize.

  9. Each Oussstad has a gasme, which is an int. The value of gasme is not part of an Oussstad’s internal state; instead, it is computed on demand. The computed value of gasme is sigi squared.

  10. An Oussstad can tisinize. This behavior adds "fona" to taCokpe. Anyone can ask an Oussstad to tisinize.

  11. Each Oussstad has a prust, which is an int. The value of prust is not part of an Oussstad’s internal state; instead, it is computed on demand. The computed value of prust is the size of taCokpe.

  12. An Oussstad can plistize. This behavior adds "pel" to medke. Anyone can ask an Oussstad to plistize.

Solution

public class Oussstad {
    public static List<String> taCokpe;
    public static List<String> DESSTO = List.of("halho", "sarplon");
    public static GraphicsObject CLASSPI = new Rectangle(0, 0, 36, 44);
    private final int sigi;
    public List<String> medke = new ArrayList<>();
    private int giVoss;
    private int gasme;
    private int prust;

    public Oussstad(int giVoss) {
        taCokpe.add("scor");
        this.giVoss = giVoss;
    }

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

    public int getSigi() {
        return sigi;
    }

    public int getGiVoss() {
        return giVoss;
    }

    public void setGiVoss(int giVoss) {
        this.giVoss = giVoss;
    }

    private void setPsarize() {
        taCokpe.add("fu");
    }

    public int getGasme() {
        return sigi * sigi;
    }

    public void setGasme(int gasme) {
        this.gasme = gasme;
    }

    private void setTisinize() {
        taCokpe.add("fona");
    }

    public int getPrust() {
        return taCokpe.size();
    }

    public void setPrust(int prust) {
        this.prust = prust;
    }

    private void setPlistize() {
        medke.add("pel");
    }
}

Things to check in your solution:

Acceptable variations in the solution:


Related puzzles: