go to previous page   go to home page   go to next page

Answer:

There should be a buffer in which to save characters, and integers day, month, and year.


Inserting Statements

Here is our Software Design Document:

date recognizing FSA

And here is our host automaton. It needs some additional statements to convert the string to a MyDate object.


  MyDate convert( String str )
  {

    int  reject= 10;       // rejection state
    int  state = 1;        // the current state
    char current;          // the current character
    int  index = 0 ;       // index of the current character

    String buffer = null;  // collect digits of a number here
    int  day=0, month=0, year=0;
    
    while ( index < str.length()  && state != reject  )
    {
      current = str.charAt( index++ ) ;

      if      ( state==1 && current >=  '0' && current <= '9')
      {
        state = 2; buffer = "" +  ;  // don't just click! think about this!
      }
      else if ( state==2 && current=='/' )
      {
        state = 4; day = Integer.parseInt(  ); 
      }
      else if ( state==2 && current >=  '0' && current <= '9' )
      {
        state = 3; buffer +=  ; 
      }
      else if ( state==3 && current=='/' )
      {
        state = 4; day = Integer.parseInt(  ); 
      }
      else if ( state==4 && current >=  '0' && current <= '9' )
      {
        state = 5;
      }
      else if ( state==5 && current=='/' )
      {
        state = 7;
      }
      else if ( state==5 && current >=  '0' && current <= '9' )
      {
        state = 6;
      }
      else if ( state==6 && current=='/' )
      {
        state = 7;
      }
      else if ( state==7 && current >=  '0' && current <= '9' )
      {
        state = 8;
      }
      else if ( state==8 && current >=  '0' && current <= '9' )
      {
        state = 9;
      }
      else
        state = reject ;
    }

    if ( index == str.length()  && state ==9  )
      return new MyDate( day, month, year )  ;
    else
      return null ;
}


QUESTION 16:

Add statements for the first four transitions. (More statements are neeed. They will be added later.)