Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Problem 1 Consider the deterministic finite automaton shown in Figure . startSQ,

ID: 3701619 • Letter: P

Question

Problem 1 Consider the deterministic finite automaton shown in Figure . startSQ, Qs 1 Q2 Figure 1: Deterministic finite automaton for Problem 1 Define a Java program DFACheck which accepts the name of a file on the command line, and for every string in the file outputs both the string itself, and whether or not it is accepted by the deterministic finite automaton shown in Figure You may assume that the file contains a single string per line, and that the only delimiter is the newline ' ' character.

Explanation / Answer

CODE:-

import java.io.*;

public class DFACheck {
   static boolean accept(String s) //checking acceptance of the string s
    {
   int currState = 0;
   for(int i = 0; i < s.length(); i++)
        {
       currState = transition(currState,s.charAt(i));
       if(currState == -1)
            return false;
        }
   if (currState == 3)
        return true;
   return false;
    }
    static int transition(int a,char c) // transition function of the DFA
    {
   if(a == 0 && c == 'a')
        return 2;
   else if(a == 0 && c == 'b')
        return 1;
   else if(a == 1 && c == 'a')
        return 2;
   else if (a == 1 && c == 'b')
        return 3;
   else if (a == 2 && c == 'a')
        return 3;
   else if (a == 2 && c == 'b')
        return 1;
   else if (a == 3 && c == 'a')
        return 0;
   else if ( a == 3 && c == 'b')
        return 0;
   else
        return -1;
    }
    public static void main(String [] args) {

        // The name of the file to open is passed as command line argument args[0]

        // This will reference one line at a time
        String line = null;

        try {
            // FileReader reads text files in the default encoding.
            FileReader fileReader =
                new FileReader(args[0]);

            // Always wrap FileReader in BufferedReader.
            BufferedReader bufferedReader =
                new BufferedReader(fileReader);

            while((line = bufferedReader.readLine()) != null) {
                System.out.print(line);
       if(accept(line))
            System.out.println(": Accept");
       else
            System.out.println(": Reject");
            }

            // Always close files.
            bufferedReader.close();       
        }
        catch(FileNotFoundException ex) {
            System.out.println(
                "Unable to open file '" +
                args[0] + "'");              
        }
        catch(IOException ex) {
            System.out.println(
                "Error reading file '"
                + args[0] + "'");                
            // Or we could just do this:
            // ex.printStackTrace();
        }
    }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote