CS 151 Fall 2007 Lab 10

Inheritance

 

The main goal for this lab is to practice working with inheritance by creating and using super and subclasses.  We will do this in the context of the strategy game Chess.  Our superclass will be for a generic chess piece and then we will create subclasses for the different types of pieces.

 

  1. Open BlueJ either by selecting it from the Dock or navigating to it in the Applications folder and double clicking it.

  2. Mount your network directory/folder so that you can access the lab after you leave Olin 323.  Go to the Finder and from the Go menu select Connect to Server....  Type afp://fileserver1 or choose it from the list of selections and select the Personal volume when prompted.

  3. From the Project menu select New Project.  Select your computer from the drop down menu.  Then scroll down and open the Volumes folder and then the Personal folder inside it.  Scroll down the long list of folders until your find yours.  Create a project called "Lab10" in your personal network folder.

  4. Create a class called ChessPiece and remove the default instance variable and method that BlueJ puts in the class.

  5. In the class create a private instance variable to store the piece's color.  Also add constants BLACK and WHITE.  Possible type choices for color include boolean, String, char, and int.  Since the color of a piece never changes once it is created you can make the color instance variable final (not static final, just final).  That will prevent it from being accidentally changed.

 

  1. Define one or two private integer instance variables to hold the piece's position on the board.  You can have separate variables for the row and column positions, or a single array with two entries.  if you choose the array option, be sure to clearly document which entry holds which value. 

 

  1. Add a constructor to the class that takes three parameters: the row and column where the piece resides on the board and the color to make the piece.

 

  1. Create a moveTo() method that takes two parameters: a new row and a new column that specify the new position for the piece on the board.  Leave the body of the method blank.  We will override this method in each of the subclasses we create.  If we don't add it to this class, it will create problems when we try to call the method for an arbitrary piece on the board.  What if anything should the method return?

 

  1. Create a subclass of the ChessPiece class called ChessRook.  Don't forget to add the keyword extends in the class declaration line.  In chess, the Rook or castle can move any number of spaces horizontally (in the same row) or vertically (in the same column).  For simplicity, assume the Rook is the only piece on the board.  Add a constructor to the ChessRook class that calls its superclass (ChessPiece) constructor you created a few steps ago.  Remember, you use the super keyword to do this and put any parameters in parenthesis after super.  super has to be the first command in the constructor, but you can have comments before it.

    In ChessRook override the moveTo() method from the ChessPiece superclass.  You do this by redefining the method in
    the subclass ChessRook and filling in the body of the method.  Be sure to have the same return type, parameter types, and parameter order as in the superclass ChessPieceOnly.  Otherwise you end up creating another version of the method which leads to unwanted.  The method should change the location of the piece only if moving from the current location to the new location is a legal Rook move.  For example, if the rook is at position (2, 2), moving to (2, 5) or (0, 2) is a legal move.  However, moving to (3, 3) is not a legal move.  If you didn't define get and set methods in the ChessPiece class, you will need to do it now.

 

  1. Compile and test.  Make sure the Rook works as specified.

 

  1. Create another class called ChessBishop.  The bishop piece can only move diagonally.  For example a bishop can move from (1, 2) to (0,1) or to (5,6) but not to (3,2).  Override the ChessPiece method again in ChessBishop to capture this constraint.  What condition do you want to check for in your moveTo() method?  Compile and test the class.

 

  1. Create a class called ChessPieceTester that only has the static main method.  In that method create a 2-dimensional 8 x 8 array of ChessPieces that is initially all nulls (each cell contains a null pointer).  Create a new rook and a new bishop and place them on the board without creating any intermediate ChessRook or ChessBishop type variables.  For example, use a command like
     
    board[0][0] = new ChessRook(0,0,ChessPiece.BLACK);

    Then, call the move method for piece at that position on the board, e.g.

     
    board[0][0].move(4,0);

    Since both are subclasses of ChessPiece and the method is defined in that class this all works.

 

  1. Comment out (using /* and */) the moveTo method in the ChessPiece class.  Compile the ChessPiece class.  Then try to compile the tester class.  What happens and why?  Remove the comments and restore ChessPiece to its previous state.

 

  1. Time permitting, try to create classes for king, knight/horse, and queen.  The king can move one space in any of the 8 possible directions.  Can you use a single, simple test to check for a valid king move?  Hint: think of distance and use the static distance method in the java.awt.geom.Point2D class.  Similar logic will work for queen and knight.

 

  1. Still have time?  How would you model a pawn's movement?  It can only go forward (increasing or decreasing) the row its in, and diagonally ahead one position to take another piece.  Something to think about, but you need not try to implement it.