Question
Write an abstract Java class ChessPiece that represents a chess piece, and create concrete ( i.e. , non-abstract) subclasses Rook, Bishop, and Knight. These classes
Write an abstract Java class ChessPiece that represents a chess piece, and create concrete (i.e., non-abstract) subclasses Rook, Bishop, and Knight. These classes should all have constructors that take two arguments, row and column, which should be integers between 1 and 8, and should all have no-argument accessor methods getRow and getColumn that return the row and column number, respectively.
All three concrete classes should also provide a method
public boolean validMove(int toRow, int toColumn)
that returns true if and only if it is valid to move that piece to the specified square. It is invalid to move a piece to the square it is already on, or one that is off the board. Do not consider any other pieces that may be on the chess board in deciding what squares a piece may move to.
The toString method for all three concrete classes should return a string "piece at (row,column)", where piece is either Rook, Bishop or Knight, and row and column comprise the location of the piece. For example, toString for a rook at row 3 column 4 would return Rook at (3,4).
Recall that in chess, rooks move either vertically or horizontally as far as the edge of the board. Bishops move diagonally (either northeast, northwest, southeast, or southwest) as far as the edge of the board. Knights may move one square vertically and two squares horizontally, or one square horizontally and two squares vertically. The rook, bishop, and knight moves, respectively, may be visualised as:
A key requirement is that ChessPiece should be a Java type that supports the four methods above. For example, it must be possible to do this:
ChessPiece piece = new Knight(6,2); System.out.println(piece);
to print out Knight at (6,2). Note that the class of the piece variable is ChessPiece. Declaring it to be of type Knight is not good enough.
654321 654321 654321Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started