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

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

  3. All BriOprecs share a single PRIST_PRIOG, which is a list of strings. It is a constant. Its value is ["haba", "cindiw", "ehe"]. Other classes can see its value.

  4. All BriOprecs share a single hos, which is an int. No other classes can directly ask for the value of hos. The value of hos starts out as 19 when the program starts. Every time a new BriOprec is created, it adds 5 to hos.

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

  6. Each BriOprec has a gring, which is an int. The value of gring is not part of a BriOprec’s internal state; instead, it is computed on demand. The computed value of gring is sto plus 9.

  7. A BriOprec can cealify. This behavior adds 7 to tuphe. Anyone can ask a BriOprec to cealify.

  8. A BriOprec can jasize. This behavior adds 5 to tuphe. Anyone can ask a BriOprec to jasize.

Solution

public class BriOprec {
    private static List<String> PRIST_PRIOG = List.of("haba", "cindiw", "ehe");
    public static int hos;
    public int tuphe = 2;
    private int sto;
    private int gring;

    public BriOprec(int sto) {
        hos += 5;
        this.sto = sto;
    }

    public static void onStart() {
        hos = 19;
    }

    public int getSto() {
        return sto;
    }

    public void setSto(int sto) {
        this.sto = sto;
    }

    public int getGring() {
        return sto + 9;
    }

    public void setGring(int gring) {
        this.gring = gring;
    }

    private void setCealify() {
        tuphe += 7;
    }

    private void setJasize() {
        tuphe += 5;
    }
}

Things to check in your solution:

Acceptable variations in the solution:


Related puzzles: