← Unit 3 · all exercises
Saved in this browser only

A loaded die

Write a Die class holding a number of sides and a current face starting at 1. setFace changes the face, but only to a value from 1 up to the number of sides. getFace returns it.

class Die { Die(int sides); void setFace(int face); int getFace(); }

Die.java
Loading editor…
Results

Examples

  • Die d = new Die(6); d.setFace(4); d.getFace() → 4
  • Die d = new Die(6); d.setFace(7); d.getFace() → 1too high, so ignored

There are also 4 hidden tests you can't see. Solve the problem, not the examples.