Exercises


  1. To see the different loop types in action, code the factorial algorithm (without using recursion). The definition of factorial:

     if n is 0, n factorial = 1. if n is 1, n factorial = 1. if n is greater than one, n factorial is 1 * 2 * 3 * ... up to n 

    Code the tests, then create a factorial method using a while loop. Next, modify the same method to pass the tests without using the while keyword, but instead using the C-style for loop. Finally, recode it again using the do-while loop. Notice the changes you must make in order to use the different loop structures. Also note that you only need one loop structure to solve any looping problem.

    1. Why would you prefer one loop structure over the others?

    2. Which is shortest?

    3. Which is the most appropriate to this problem? Why?

    4. Replace the loop with

       while (true) { 

    and get the test to pass using the break keyword to end the loop.

  2. Demonstrate your understanding of the continue keyword. Create a method to return a string containing the numbers from 1 to n. Separate each pair of numbers with a single space. Append an asterisk to the numbers divisible by five. For example, setting n to 12 would result in the string:

     "1 2 3 4 5* 6 7 8 9 10* 11 12". 

  3. a) Break the results of the string from the previous exercise into a Vector of substrings. Split the string on the space character. The string "1 2 3 4 5* 6 7" would split into a Vector containing the strings "1", "2", "3", "4", "5*", "6", and "7".

    b) Iterate through the elements of the vector using an Enumeration and recreate the string from Exercise #2.

    c) Remove all parameterized type information, note the compiler errors, and correct the code where necessary.

  4. In an earlier exercise, you wrote a test to ensure that the list of valid moves for a given piece contained a set of squares. Simplify this assertion to something like:

     public void testKingMoveNotOnEdge() {    Piece piece = Piece.createBlackKing();    board.put("d3", piece);    assertContains(piece.getPossibleMoves("d3", board),       "c4", "d4", "e4", "c3", "e3", "c2", "d2", "e2"); } 

    Refactor other tests to use this mechanism.

  5. You may have noticed that the chess board you have spent so much time on is really a two-dimensional array of squares. Back up your Board class. Modify Board to use a two-dimensional array as its underlying data structure.

    One bit of difficulty you may encounter is due to the fact that client code (such as Game and Pawn) uses the implementation specifics of the board. For this exercise, modify the client code to loop through the ranks and files of the two-dimensional array. This is a less-than-ideal situation: Normally you want to design a solid public interface to your class, one that does not change. When you change the implementation details of the Board class, the Game class should not be affected.

    The alternative would be to dynamically convert the Piece two--dimensional array to a List of List of Piece objects each time a client wanted to iterate through it. But the best solution is to not require the client to do the iteration themselves. One sophisticated (and somewhat complex) solution to accomplish this involves the visitor design pattern. See the book Design Patterns[11] for further information. For now, exposing the board's underlying two-dimensional array will -suffice.

    [11] [Gamma1995].

    Compare and contrast the two Board versions. Which is easier to read and understand? Which involves the least code?

  6. Make the Board class iterable, so that you can access the pieces (not including "no piece" objects) using a for-each loop. A complex solution would involve creating a separate Iterator class that tracks both a rank and file index. A simpler solution would involve looping through the board's matrix and adding each piece to a List object. You could then return the iterator from the List object.

    Once you have made the class iterable, go back and change any code that loops through all pieces to use a for-each loop. Look for opportunities to refactor code so that you use the for-each loop as much as possible. For example, if you haven't already, modify your code so that each Piece object stores its current rank and file.

  7. Read the javadoc API for the ArrayList class and determine a way to simplify the last example in the chapterthe one that splits the student name and returns a List of name parts. It is possible to recode the split method without the use of a loop at all! Hint: Look at the constructors for ArrayList.



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