Assessed Exercise 1, Task 1 |
Introduction. In the lectures we saw how a finite state automaton (FSA) can be represented by a 2-dimensional matrix. In this task you should write a simple FSA that takes as input a matrix and then decides the language given by the matrix. Note that the specification below is wordy, but the task is very simple: my example solution is 11 lines of plain Java code, and it's not importing any libraries. Task. You are asked to
produce a Java program that takes as input a 2-dimensional
matrix together with an initial and terminal state
(representing an FSA). Your submission should return a Java
algorithm instantiating the interface Let's make this more precise. As characters we only allow integers. States are also integers. More precisely:
The signature that characterises the input that your submission needs to process is: interface Matrix2D { public int initialState (); public int terminalState (); public int nextState ( int currentState, int character ); } You can think
of class Task1 { public static Language create ( Matrix2D matrix2D ) { return ...; } }
In other words, you must submit a working implementation of
the class interface Language { public Boolean decide ( int [] input ) throws Task1Exception; }
The
If the class Task1Exception extends Exception { public String msg; public Task1Exception ( String _msg ) { msg = _msg; } } You can find this code here.
You can assume that all
instances of
Note that
Note also that you do not have to supply classes
instantiating the Examples. Consider the
alphabet {0, 1} and the language of all binary strings over
this alphabet. The following instance
of class BinaryNumbers implements Matrix2D { private int [] [] fsaTable = new int [] [] { { 0, 0 } }; public int initialState () { return 0; }; public int terminalState () { return 0; }; public int nextState ( int currentState, int character ) { return fsaTable [ currentState ] [ character ]; } }
Clearly ![]() As a second example, again
over the alphabet {0, 1}, consider the language of all
binary strings divisible by 4. The following instance
of class BinaryNumbersDivisibleBy4 implements Matrix2D { private int [] [] fsaTable = new int [] [] { { 0, 1 }, { 2, 1 }, { 0, 1 } }; public int initialState () { return 0; }; public int terminalState () { return 0; }; public int nextState ( int currentState, int character ) { return fsaTable [ currentState ] [ character ]; } }
Clearly ![]() Note that your submission might be tested with input over larger alphabets of integers. You may also encounter automata where initial and terminal state are not identical. The necessary Java files for all three tasks can be found for convenient download here: Warning. Your code
needs to compile against the signatures given to you
here. Do not change them. You are welcome to add new
classes. For your convenience you can
find a file |