Java program to demonstrate Finite Automaton
In this tutorial, you are going to learn how to demonstrate Finite Automata using a Program in Java.
A finite automaton (FA) is a simple idealized machine used to recognize patterns within input taken from some character set.
So, if our input satisfies the pattern defined by FA then it’s going to accept it otherwise, it’ll reject it.
A finite automaton is of 2 types :
Important Points to remember
- In a DFA there’s only one valid transition from one state to another. Multiple transitions from one state to another are not possible in DFA which in case of NFA is possible
- In NFA, null transitions are also allowed i.e. it can move forward without reading symbols.
- Every DFA is an NFA but vice-versa is not true.
Now, given a set of alphabets {a,b}, we need to design an automaton which accepts a string starting with starts with a and ends with b.
So, the regular expression can be derivated as a(a*)(b*)b.
The Java code for the above regular expression is:
import java.util.*; import java.lang.*; import java.io.*; class CodeSpeedy { public static void main (String[] args) throws java.lang.Exception { Scanner sc= new Scanner(System.in); System.out.println("Enter a string of {a,b} "); String str = sc.nextLine(); int n=str.length(); if ((str.charAt(0)=='a') && (str.charAt(n-1)=='b')) { System.out.println("Valid"); } else { System.out.println("Invalid") ; } } }
Output : Enter a string of {a,b} ababbab Valid
The string ‘ababbab’ starts with ‘a’ and ends with ‘b’. Hence, it’s valid. If the input given was ‘abba’, then it’s invalid.
Recommended Posts:
Leave a Reply