Lab 7: Object Hierarchy

In this lab, you will practice creating classes in an object hierarchy.

Download the file ChessBoard.java. ChessBoard is a generic chess board. The chess board works by letting the user click on a piece and a square. If it is legal to move the piece to that square, the chess board does so. ChessBoard has the following useful methods:

You should not make any modifications to ChessBoard in this lab.

Create an abstract class ChessPiece that holds the routines generic to a chess piece. The class should have the following:

Now create a subclass of ChessPiece called KnightPiece. The KnightPiece should have a constructor that takes a ChessBoard board and a Color color. The piece should call the ChessPiece constructor with these values plus the text "N" for kNight. The KnightPiece needs a legalMove that determines if it is legal to move the piece from its current position (oldRow and oldCol to its new position newRow and newCol). The move is legal if it is L-shaped (up one space and over two or up two spaces and over one) and the new location is not occupied by a piece of the same color.

Important: Your KnightPiece class should not save the ChessBoard, Color, or its text as private variables because these are stored in the parent class. If you need access to these values, you should use the appropriate getter methods.

You can test your code by creating a ChessBoard object, a KnightPiece object, and then calling the add method of KnightPiece (inherited from ChessPiece) to place the knight on the chess board.

When you are finished, email your code to me.