Some Final Considerations


Development Cycle Fifth Iteration

All that’s left to code is the move() method. All the other supporting pieces are now in place. You can change the robot rat’s direction by turning left or right, and change the pen’s position up or down. The move() method will use all this functionality to model the movement of the robot rat across the floor.

As you get into the planning phase of this iteration you may discover you need more than just the move() method. For instance, you’ll need some way to get the number of spaces to move the robot rat from the user.

Plan (Fifth Iteration)

Table 3-10 lists the design considerations for this iteration of the development cycle.

Table 3-1: Fifth Iteration Design Considerations

Check-Off

Design Consideration

Design Decision

 

move method

Write the code for the move() method. The move() method will use the direction and pen_position fields to make move decisions. The move() method will need a way to get the number of spaces to move from the user.

You will have to add the current_row and current_col fields to the RobotRat class. These fields will be used to preserve the robot rat’s floor position information between moves.

 

Getting the number of spaces to move from the user

When the user moves the robot rat they will need to enter the number of spaces so the move can be executed. This can be handled by writing a new method called getSpaces(). The getSpaces() method can be a slight modification of the readChar() method as it will read a text string from the user and convert it to an integer. The getSpaces() method will return an integer value and take no parameters.

 

Add current_row & current_col fields

current_row and current_col will be declared with the rest of the RobotRat fields and initialized in the constructor.

The first feature from the list to be coded and tested should be the getSpaces() method. The readChar() method provides an example of some of the functionality you’ll require, namely, read a text string from the console. This text string must then be converted into an integer value. Java provides a set of helper classes that will perform the conversion for you. In this instance you’ll need to use the services of the java.lang.Integer class.

The move() method will require you to do some intense study of the mechanics of a robot rat move. A picture like figure 3.3 is very helpful in this particular case because it enables you to work out the moves of a robot rat upon the floor. You should work out the moves along the north, south, east, and west directions and note how to execute a move in terms of the robot rat’s current_row, current_col, and direction fields.

It’s a good idea to place the robot rat in a starting position. A good starting position is on current_row = 0, current_col = 0, and direction = EAST. These attributes can be initialized to the required values or states in the RobotRat constructor method.

Once you get the move mechanics worked out you can write a pseudocode description of the move() method like the one presented below in example 3.11.

Example 3.11: move() method pseudocode

image from book
 1     get spaces to move from user 1     if pen_position is UP do the following 1        if direction is NORTH move RobotRat NORTH -- do not mark floor 1        if direction is SOUTH move RobotRat SOUTH -- do not mark floor 1        if direction is EAST move RobotRat EAST -- do not mark floor 1        if direction is WEST move RobotRat WEST -- do not mark floor 1     if pen_position is DOWN 1        if direction is NORTH move RobotRat NORTH -- mark floor 1        if direction is SOUTH move RobotRat SOUTH -- mark floor 1        if direction is EAST move RobotRat EAST -- mark floor 1        if direction is WEST move RobotRat WEST -- mark floor
image from book

With your feature planning complete you can move to the code phase.

Code (Fifth Iteration)

Example 3.12 gives the source code for the getSpaces() method.

Example 3.12: getSpaces() method

image from book
 1     private int getSpaces(){ 2       int temp = 0; 3       try{ 4          temp = Integer.parseInt(console.readLine()); 5       }catch(Exception e){ 6          System.out.println("Spaces has been set to zero!"); 7        } 8     return temp; 9     }
image from book

The getSpaces() method utilizes the Integer wrapper class’s parseInt() method on line 4 along with the con-sole.readLine() method to read a text string from the console and convert it into an integer value. If the parseInt() method throws an exception a message is sent to the console stating the value of spaces has been set to zero and the temp variable is returned. The getSpaces() method can now be used in the move() method as is shown in example 3.13.

Example 3.13: move() method

image from book
 1     private void move(){ 2       System.out.print("Please enter spaces to move: "); 3       int spaces = getSpaces(); 4       switch(pen_position){ 5         case UP: switch(direction){ 6                  case NORTH:  if((current_row - spaces) <= 0) 7                                 current_row = 0; 8                               else current_row = current_row - spaces; 9                              break; 10                   case SOUTH:  if((current_row + spaces) >= (floor[0].length -1)) 11                                 current_row = (floor[0].length -1); 12                                else current_row = current_row + spaces; 13                               break; 14                   case EAST:  if((current_col + spaces) >= (floor.length -1)) 15                                 current_col = (floor.length -1); 16                               else current_col = current_col + spaces; 17                               break; 18                   case WEST:  if((current_col - spaces) <= 0) 19                                current_col = 0; 20                                else current_col = current_col - spaces; 21                                break; 22        } 23        break; 24        case DOWN: switch(direction){ 25                     case NORTH: if((current_row - spaces) <= 0){ 26                                  while(current_row >= 1) 27                                  floor[current_row--][current_col] = true; 28                                } 29                              else while(spaces-- > 0) 30                                floor[current_row--][current_col] = true; 31                              break; 32                     case SOUTH: if((current_row + spaces)> = (floor[0].length -1)){ 33                                  while(current_row < (floor[0].length-1)) 34                                   floor[current_row++][current_col] = true; 35                            } 36                                 else while(spaces-- > 0) 37                                   floor[current_row++][current_col] = true; 38                             break; 39                      case EAST:  if((current_col + spaces)>= (floor.length -1)){ 40                                    while(current_col < (floor.length-1)) 41                                      floor[current_row][current_col++] = true; 42                            } 43                                 else while(spaces-- > 0) 44                                        floor[current_row][current_col++] = true; 45                             break; 46                   case WEST: if((current_col - spaces) <= 0){ 47                                 while(current_col >= 1) 48                                 floor[current_row][current_col--] = true; 49                          } 50                              else while(spaces-- > 0) 51                               floor[current_row][current_col--] = true; 52                             break; 53                   } 54            break; 55          } 56     }
image from book

Test (Fifth Iteration)

This will be the most extensive test session of the RobotRat project yet. You must test the move() method in all directions and ensure you are properly handling move requests that attempt to go beyond the bounds of the floor array.

Figure 3-12 shows the RobotRat project being run and a user entering a number of spaces to move. Figure 3-13 shows two floor patterns printed to the console.

image from book
Figure 3-12: Testing the getSpaces() and move() Methods

image from book
Figure 3-13: Two Floor Patterns Printed To The Console

Integrate/Test (Fifth Iteration)

At this point in the development cycle you will want to test the entire RobotRat project. Move the robot rat with the pen up and pen down. Move in all directions and try making and printing different floor patterns. The emphasis in this phase is to test all RobotRat functionality and note the effects the addition of the latest features had on existing functionality.




Java For Artists(c) The Art, Philosophy, and Science of Object-Oriented Programming
Java For Artists: The Art, Philosophy, And Science Of Object-Oriented Programming
ISBN: 1932504052
EAN: 2147483647
Year: 2007
Pages: 452

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