Exercises


  1. Create a CharacterTest class. Don't forget to add it to the AllSuites class. Observe the zero tests failure. Then add a test named testWhitespace. This test should demonstrate that the new line character, the tab character, and the space character all return true for Character.isWhitespace. Express that other characters return false. Can you find another character that returns true?

  2. Java has certain naming restrictions on identifiersthe names you give to methods, classes, variables, and other entities. For example, you cannot use the caret (^) in an identifier name. The Character class contains methods that designate whether or not a character can be used in an identifier. Consult your API documentation to understand these methods. Then add tests to the CharacterTest class to discover some of the rules regarding Java identifiers.

  3. Assert that a black pawn's printable representation is the uppercase character 'P', and a white pawn's is the lowercase character 'p'. For the time being, you can accomplish this by adding a second parameter to the Pawn constructor. However, note that this creates a redundancy in representation. You'll improve upon the solution later.

  4. (This exercise and Exercise 5 are closely related. You may suspend refactoring until you have completed Exercise 5.) When a client creates a Board object, they should be able to assume that the board is already initialized, with pieces in place. You will need to modify the Board tests and code accordingly. Start by changing the assertions on the number of pieces available at time of board creation: There should be 16. Delete testAddPawns; it is not useful in its current form.

  5. Add an initialize method to Board. The initialize method should add pawns to create two ranks: a rank for white pawns (the second rank) and a rank for black pawns (the seventh rank). To store a rank, use an ArrayList whose contents are Pawn objects. You declare such a list as ArrayList<Pawn>.

    Add an assertion to testCreate that the second rank looks like this: "pppppppp". Assert that the seventh rank looks like this: "PPPPPPPP". Use a StringBuilder and a for loop to gather the printable representation for the pieces in each rank.

    Ensure your solution is as well refactored as you are capable of. Expect a good amount of duplication in adding pawns to the ranks and in other areas of Board. You will learn to eliminate this duplication in later lessons.

  6. Assert that the board looks like this at initial setup, with a dot character (a period) representing an empty square (rank 8 is the top row, rank 1 is the bottom row):

     ........ PPPPPPPP ........ ........ ........ ........ pppppppp ........ 

    Remember to ensure portability in your tests and in your board-printing method by using the system properties appropriately.

  7. If you have implemented your chess board code and tests to this point using String concatenation, change the code to use the StringBuilder class. If you have used StringBuilder as the primary basis for your solution, change your code to use String concatenation. Name some differences in the structure and readability of the code.

  8. Modify your test to display the board on the console. Ensure that it appears as expected. If not, correct the test and fix the code.

  9. You will need to revisit this code in later exercises to remove even more duplication when they learn about loops and other Java constructs.

  10. Create an Ant build file to compile your entire project and run all of your tests with a single command.



Agile Java. Crafting Code with Test-Driven Development
Agile Javaв„ў: Crafting Code with Test-Driven Development
ISBN: 0131482394
EAN: 2147483647
Year: 2003
Pages: 391
Authors: Jeff Langr

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net