Home > Articles > Programming > Java

  • Print
  • + Share This
This chapter is from the book

Exercises

  1. Add a test to TestPawn that creates a pawn without a color. Why does this generate a compile error? (Hint: Think about default constructors.) Fix the compiler error by adding a second constructor that constructs a white pawn by default.
  2. Make the constants for the two colors static and move them onto the Pawn class.
  3. Pawns aren't very useful without a board. Use a test to define a Board class. Assert that the board starts with zero pieces on it. Follow the TDD sequence: Write the smallest test possible. Prove failure with a red bar or a compile error. Incrementally add small bits of code to obtain a clean compile or green bar.
  4. Develop code to allow pawns to be added to the board. In a test, add a black and white pawn to the board. Each time you add a pawn, assert that the piece count is correct. Also, each time you add a pawn, obtain a list of pieces from the board and ensure that it contains the expected pawn objects.
  5. Write javadoc for each of the production classes and methods you have created so far. Be careful in doing so: Do not duplicate information that your methods already impart! The javadoc should be supplementary information.
  6. Move the four tests and classes you have created into a package of their own. Name this package chess. Resolve compilation failures and get to a green bar again. Also, replace fully qualified class names for List and ArrayList by using an import statement.
  7. Move TestPawn and Pawn into a package named pieces and resolve any problems discovered along the way.
  8. Ensure that nothing other than a Pawn can be added to the board. Try adding a new Integer("7") to the list of pawns and see the resulting compiler error.
  9. Create a test suite that runs each test class.
  10. Look through the code you have written so far. Ensure there is no duplication anywhere in the code. Remember that the test code is code as well. Use the setUp method if appropriate.
  • + Share This
  • 🔖 Save To Your Account