Once you have an automaton that recognizes the data,
adding statements that perform the
right actions takes a few more steps.
Look at this method which implements the date automaton.
It returns a MyDate
object if the
argument is valid, or null
if not.
But it is not quite complete.
It still needs a few more steps.
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 while ( index < str.length() && state != reject ) { current = str.charAt( index++ ) ; if ( state==1 && current >= '0' && current <= '9') { state = 2; } else if ( state==2 && current=='/' ) { state = 4; } else if ( state==2 && current >= '0' && current <= '9' ) { state = 3; } else if ( state==3 && current=='/' ) { state = 4; } 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 ; }
Your goal is to change the automaton as little as possible.
The automaton has solved the "recognize" part of the problem.
For the most part, you should just add statements to
the automaton to finish the "compute" part of the problem.
A few changes have been made to the original automaton:
the return type MyDate
is constructed in the last
if-else
of the method.
To start, look at the code (and the envelope) and decide what new variables need to be added.