Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Complete Java code gets thumbs up. Write an exception named InvalidSquareExcepti

ID: 3598037 • Letter: C

Question

Complete Java code gets thumbs up.

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.

Decide whether your InvalidSquareException should be a checked or unchecked exception and implement it appropriately.

In the InvalidSquareException class expalin why either a checked or unchecked exception should be used.

Make a Square class, a class to represent squares on a chess board. Square should be instantiable and have the following constructors and methods:

a public constructor Square(char file, char rank) which uses a file name such as 'a' and rank name such as '1' to initialize instance variables that store the file and rank (as chars), and optionally the String name of the square that would be returned by toString() (see below). Ideally, this constructor should delegate to the other constructor, described below.

a public constructor Square(String name) which uses a square name such as "a1" to initialize the instance variables described in the other constructor above.

a public instance method toString() which returns a String representation of the square name, e.g., "a1".

a properly written equals method that overrides the equals method from java.lang.Object and returns true for Square objects that have the same file and rank values, false otherwise.

Now modify Square class with the following changes:

The constructors should should throw an InvalidSquareException exception if the file is not in the range [ 'a' , 'h' ] or the rank is not in the range [ '1' , '8' ].

Add public getter methods for the file and rank fields.


For each class include comments describing what is being done by them.

Classes will be tested with code similar to the code below. 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());

////Help////

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());

o Decide whether your InvalidSquareException should be a checked or unchecked exception and implement it appropriately o In the JavaDoc comment for your InvalidSquareException class write a sentence or two justifying your choice of checked or unchecked exception.

Explanation / Answer

package com.chegg.expert;

public class ChessBoard {

public static void main(String[] args) throws InvalidSquareException {

Square s1 = new Square("a8");

System.out.println(s1.getFile());

System.out.println(s1.getRank());

System.out.println(s1.toString());

System.out.println(s1.equals(new Square("a8")));

System.out.println(s1.assertEquals(s1.getRank(), '8'));

}

}

class InvalidSquareException extends Exception{

/**

* This is an Checked Exception which I'm using to check the file and rank of the squares.

* This checking better to be done at compile time rather than checking at run-time because

* no errors will come during the game

*

* This Exception will be thrown whenever file is not in betweem a and h or rank is not in

* between 1 and 8.

*

*/

private static final long serialVersionUID = 1L;

InvalidSquareException(String msg){ //Constructor

super(msg); //Calling super class constructor

}

}

//This the method to create objects of the Squares in the chess board

class Square{

private static char file, rank;

public Square(char file, char rank) throws InvalidSquareException { //Validation

if((int)file>104 || (int)file<97 || rank>56 || rank<49) {

throw new InvalidSquareException("Invalid file and/or rank!!!");

}

//Initialization

this.file = file;

this.rank = rank;

}

//Overloaded constructor

public Square(String sq) throws InvalidSquareException {

new Square(sq.charAt(0), sq.charAt(1));

}

//Getter methods for file and rank

public char getFile() {

return (char)this.file;

}

public char getRank() {

return (char)this.rank;

}

//Eqauls method

public boolean equals(Object o) {

if(this.file == ((Square) o).file && this.rank == ((Square) o).rank ) {

return true;

}

return false;

}

//toString method

public String toString() {

return ""+this.file+this.rank;

}

//assertEquals method

public static boolean assertEquals(char c1, char c2) {

if(c1==c2) {

return true;

}

return false;

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote