In this “mini-homework” assignment you will practice
You need to add invalid square checking to your square class and ensure that it works properly before continuing to rewrite your PGN reader.
Write or modify the following classes:
Write an exception named InvalidSquareException
. The message returned by getMessage
should be the square that’s invalid, e.g. a9
or i1
– a square that doesn’t exist on the chess board.
InvalidSquareException
should be a checked or unchecked exception and implement it appropriately.InvalidSquareException
class write a sentence or two justifying your choice of checked or unchecked exception.Modify your Square
class from HW2 to make the following changes:
InvalidSquareException
exception if the file is not in the range ['a'
, 'h'
] or the rank is not in the range ['1'
, '8'
].For each class include Javadoc comments as described in the CS 1331 style guide.
We will test your classes with code similar to the following. You should too. (Assume fail(String)
and assertEquals(T, T)
are implemented appropriately in these examples.)
try {
new Square("a1");
} catch (InvalidSquareException e) {
fail("InvalidSquareException for valid square: " + e.getMessage());
}
try {
String invalidSquare = "a9";
new Square(invalidSquare);
fail("No InvalidSquareException for invalid square: " + invalidSquare);
} catch (InvalidSquareException e) {
// Success
}
Square s = new Square("f7");
assertEquals('f', s.getFile());
assertEquals('7', s.getRank());
Square s2 = new Square('e', '4');
assertEquals("e4", s2.toString());
InvalidSquareException
is-a appropriate Throwable
InvalidSquareException
being checked or uncheckedSquare
throws InvalidSquareException
for square with invalid file (both constructors)Square
throws InvalidSquareException
for square with invalid rank (both constructors)Square
does not throw InvalidSquareException
for square with valid file (both constructors)Square
does not throw InvalidSquareException
for square with valid rank (both constructors)InvalidSquareException.getMessage
returns correct value when thrown from Square
constructorSquare
getter for file has correct visibility, return type, name, parameter list, and returns correct valueSquare
getter for rank has correct visibility, return type, name, parameter list, and returns correct valueSquare.toString
returns correct valueCheckstyle deduction will be capped at 30 points for this homework.
Submit each of your Java source files on T-Square as separate attachments. When you’re ready, double-check that you have submitted and not just saved a draft.
Practice safe submission! Verify that your HW files were truly submitted correctly, the upload was successful, and that your program runs with no syntax or runtime errors. It is solely your responsibility to turn in your homework and practice this safe submission safeguard.
This procedure helps guard against a few things.