[java] DFA
Viewer
*** This page was generated with the meta tag "noindex, nofollow". This happened because you selected this option before saving or the system detected it as spam. This means that this page will never get into the search engines and the search bot will not crawl it. There is nothing to worry about, you can still share it with anyone.
- package contohdfa;
- import java.util.Scanner;
- public class ContohDFA {
- static int dfa = 0;
- static void q0(char c){
- if(c == 'a'){
- dfa = 1;
- }else if(c == '0' || c == '1'){
- dfa = 2;
- }else{
- dfa = -1;
- }
- }
- static void q1(char c){
- if(c == 'a' || c == '0' || c == '1'){
- dfa = 1;
- }else{
- dfa = -1;
- }
- }
- static void q2(char c){
- if(c == 'a' || c == '0' || c == '1'){
- dfa = 2;
- }else{
- dfa = -1;
- }
- }
- static boolean isAccepted(char input[]){
- int i;
- for (i = 0; i < input.length; i++) {
- if(dfa == 0)
- q0(input[i]);
- else if(dfa == 1)
- q1(input[i]);
- else if(dfa == 2)
- q2(input[i]);
- else
- return false;
- }
- if(dfa == 1)
- return true;
- else
- return false;
- }
- public static void main(String[] args) {
- Scanner scan = new Scanner(System.in);
- String input = scan.nextLine();
- char arrayInput[] = input.toCharArray();
- if(isAccepted(arrayInput))
- System.out.println("Accepted");
- else
- System.out.println("Not Accepted");
- }
- }
Editor
You can edit this paste and save as new:
File Description
- DFA
- Paste Code
- 10 Apr-2021
- 1.4 Kb
You can Share it:
Latest Code Pastes