Development Cycle Fourth Iteration


Development Cycle Third Iteration

OK. The class structure is in place and the menu is printing to the screen. The next piece of the project to work on should be the processing of menu commands. Here’s where you can employ the technique of method stubbing so you can worry about the details later.

Plan (Third Iteration)

Table 3.8 lists the design considerations for this iteration.

Table 3-8: Third Iteration Design Considerations

Check-Off

Design Consideration

Design Decision

 

Read the user’s desired menu command from the console

The System.out.println() method makes it easy to write text to the console but you’ll have to build a method that reads text from the console. The System.in object is an InputStream object. This InputStream object can be used to create an InputStreamReader object, which can be used to create a BufferedReader object. The InputStreamReader and BufferedReader classes are found in the java.io package.

Once you get the text string from the console you will only want to use the first letter of the string. The String class provides a charAt() method that returns the character located at some index location in the string.

Write a user-defined method named readChar() that puts these pieces together to read the user’s input as a string and return the first character.

 

Execute the user’s selected menu command

This will be accomplished with a user-defined method named pro-cessMenuChoice(). This method will return void and take no arguments.

The body of the processMenuChoice() method will utilize a switch statement that acts on the menu command entered by the user. Each case of the switch statement will call a user-defined method to execute the required functionality. These methods will be named: setPenUp(), setPenDown(), turnLeft(), turnRight(), move(), print-Floor(), and exit().

 

Use method stubbing to test process-MenuChoice() method

The user-defined methods mentioned above with the exception of exit() will be stubbed out. The only functionality they will provide will be to print a short test message to the console.

The exit() method will call the System.exit() method to exit the program.

This looks like it will keep you busy for a while. You will actually move between the code and test phase repeatedly in this iteration, almost as if it were a mini development spiral in and of itself. The best place to start is at the top of the list.

Code (Third Iteration)

Starting with the first feature on the list, reading the user’s command from the console, you will need to import the java.io package into the image from book RobotRat.java file so you don’t have to fully qualify the names of the InputStreamReader and BufferedReader classes.

Example 3.3 gives the code for the image from book RobotRat.java file with the readChar() and processMenuChoice() methods completed.

Example 3.3: RobotRat.java (3rd Iteration)

image from book
 1    import java.io.*; 2 3    public class RobotRat { 4       /** 5        * private attributes 6        **/ 7          private BufferedReader console = null; 8 9          public RobotRat(){ 10            //Initialize RobotRat attributes 11            console = new BufferedReader(new InputStreamReader(System.in)); 12         } 13 14         public void run(){ 15           while(true){ 16             printMenu(); 17             processMenuChoice(); 18           } 19         } 20 21         public void printMenu(){ 22           System.out.println("\n\n"); 23           System.out.println("      Robot Rat Control Menu"); 24           System.out.println("\n"); 25           System.out.println("   1. Pen Up"); 26           System.out.println("   2. Pen Down"); 27           System.out.println("   3. Turn Right"); 28           System.out.println("   4. Turn Left"); 29           System.out.println("   5. Move Forward"); 30           System.out.println("   6. Print Floor"); 31           System.out.println("   7. Exit"); 32           System.out.println("\n\n\n"); 33        } 34 35        public char readChar(){ 36           String s = null; 37           System.out.print("Please select from the menu: "); 38           try{ 39             s = console.readLine(); 40           }catch(Exception ignored){} 41           return s.charAt(0); 42        } 43 44        public void processMenuChoice(){ 45 46          switch(readChar()){ 47            case '1': setPenUp(); 48                      break; 49            case '2': setPenDown(); 50                      break; 51            case '3': turnRight(); 52                      break; 53            case '4': turnLeft(); 54                      break; 55            case '5': move(); 56                      break; 57            case '6': printFloor(); 58                      break; 59            case '7': exit(); 60            default: printErrorMessage(); 61          } 62        } 63 64       public void setPenUp(){System.out.println("SetPenUp() method");} 65       public void setPenDown(){System.out.println("SetPenDown() method");} 66       public void turnRight(){System.out.println("turnRight() method");} 67       public void turnLeft(){System.out.println("turnLeft() method");} 68       public void move(){System.out.println("move() method");} 69       public void printFloor(){System.out.println("printFloor() method");} 70 71       public void exit(){ 72        System.exit(0); 73       } 74 75       public void printErrorMessage(){ 76        System.out.println("Please enter a valid menu choice!"); 77       } 78 79       public static void main(String[] args){ 80        RobotRat rr = new RobotRat(); 81        rr.run(); 82      } 83   }
image from book

There are a couple of important points to note in the code example above. First, the readChar() and processMenu-Choice() methods have been added. The stub methods appear on lines 64 through 69. Each stub method prints a short message to the screen giving the name of the method called. The exit() method appears on line 71 and uses the Sys-tem.exit() method to do its work.

There are two additional utility methods: printErrorMessage() which starts on line 75, and the run() method that begins on line 14. The printErrorMessage() method is called by the default case of the switch statement located in the processMenuChoice() method. It’s a good idea to give the user some feedback when they enter an invalid menu choice. This is an example of the type of functionality you’ll add to your programs although it is not specifically called for in a project specification.

The run() method is used to get the RobotRat program going. It repeatedly loops, with the help of a forever-repeating while statement, displaying the menu and processing user commands. The program will run until the user chooses the Exit menu item.

Notice that another line of code has been added to the main() method on line 81. All the main() method does is create a RobotRat object and assign it to the reference rr. Then, on line 81 the run() method is called via the rr reference.

Public vs. Private Methods

Since all the methods of RobotRat, with the exception of the constructor, run(), and main() methods, are called internally to the RobotRat class, they could be declared private. The constructor, run(), and main() methods are the only required public methods. This can be changed in the next iteration.

Test (Third Iteration)

The test phase will consist of running the revised RobotRat program and testing the menu commands. Each menu command entered should result in the stub method message being printed to the screen. An invalid command should result in the error message. Figure 3-8 shows a screen shot of a partial test of the RobotRat menu commands.

image from book
Figure 3-8: Testing Menu Commands

Incremental Testing

Although you could have tried to make all the required modifications to the RobotRat class at one stroke you most likely would make small changes or additions to the code and then immediately compile and test the results. For instance, you could concentrate on writing the readChar() method and test it exclusively before coding the pro-cessMenuChoice() method.

Another area where proceeding in small steps would be a good idea would be the body of the processMenuChoice() method. The switch statement could have been written one case at a time. Then, once you gain confidence that your code will work as planned you can code up the rest in short order.

Integrate/Test (Third Iteration)

Now that you are accepting and processing user menu commands you can examine the effect this has on the menu display. If you look at figure 3-8 it appears as if there might be too many spaces between the last menu item and the menu entry prompt. Thus, after the addition of a feature you’ve noted its effects on another program feature. The space issue can be adjusted in the next development cycle iteration.




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