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

  2. All QosCioasss share a single CLUGEO, which is a list of strings. It is a constant. Its value is ["ed", "elci"]. Other classes cannot see its value.

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

  4. Each QosCioass has a cloci, which is an int. The value of cloci is not part of a QosCioass’s internal state; instead, it is computed on demand. The computed value of cloci is the length of xior.

Solution

public class QosCioass {
    public static List<String> CLUGEO = List.of("ed", "elci");
    private String xior;
    private int cloci;

    public QosCioass(String xior) {
        this.xior = xior;
    }

    public String getXior() {
        return xior;
    }

    public void setXior(String xior) {
        this.xior = xior;
    }

    public int getCloci() {
        return xior.length();
    }

    public void setCloci(int cloci) {
        this.cloci = cloci;
    }
}

Things to check in your solution:

Acceptable variations in the solution:


Related puzzles: