lots of exercises in java... from https://github.com/exercism/java

BoardCoordinate.java 983B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. public final class BoardCoordinate {
  2. private final int rank;
  3. private final int file;
  4. public BoardCoordinate(final int rank, final int file) throws IllegalArgumentException {
  5. this.rank = rank;
  6. this.file = file;
  7. validateInputs();
  8. }
  9. public int getRank() {
  10. return rank;
  11. }
  12. public int getFile() {
  13. return file;
  14. }
  15. private void validateInputs() throws IllegalArgumentException {
  16. validateCoordinateComponent(rank, "rank");
  17. validateCoordinateComponent(file, "file");
  18. }
  19. private void validateCoordinateComponent(final int value, final String componentName)
  20. throws IllegalArgumentException {
  21. if (value < 0) {
  22. throw new IllegalArgumentException("Coordinate must have positive " + componentName + ".");
  23. }
  24. if (value > 7) {
  25. throw new IllegalArgumentException("Coordinate must have " + componentName + " <= 7.");
  26. }
  27. }
  28. }