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

Answer:

That was fun!


Adding a Few More Statements

Now, with the aid of the design document, add some more statements:

date FST

  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;
    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 = "" + current ; 
      }
      else if ( state==2 && current=='/' )
      {
        state = 4; day = Integer.parseInt( buffer ); 
      }
      else if ( state==2 && current >=  '0' && current <= '9' )
      {
        state = 3; buffer +=  current ;  
      }
      else if ( state==3 && current=='/' )
      {
        state = 4; day = Integer.parseInt( buffer ); 
      }
      else if ( state==4 && current >=  '0' && current <= '9' )
      {
        state = 5; buffer = "" + ;
      }
      else if ( state==5 && current=='/' )
      {
        state = 7; month = Integer.parseInt(  ); 
      }
      else if ( state==5 && current >=  '0' && current <= '9' )
      {
        state = 6; buffer = + ;
      }
      else if ( state==6 && current=='/' )
      {
        state = 7;  month = Integer.parseInt(  ); 
      }
      else if ( state==7 && current >=  '0' && current <= '9' )
      {
        state = 8;  buffer = "" + ;
      }
      else if ( state==8 && current >=  '0' && current <= '9' )
      {
        state = 9;  buffer = "" + ;
        year = Integer.parseInt(  ); 
      }
      else
        state = reject ;
    }

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

QUESTION 17:

Add the final statements.