EVEN-EVEN Simulation

Simulate the EVEN-EVEN automaton found in the book in code. Don’t just “solve the problem”—implement the machine (i.e., there should be evidence of the states and transitions in your code). Input at least the strings below. The output should echo the string in quotes and indicate whether it is in the language EVEN-EVEN or not.

a, abab, aabbab, baab, <the empty string>

My solution was written in JAVA and consists of approximately 80 lines of code. Please feel free to comment on my code. Are there things that I could have done better? Are there things that I could explain further. Remember this is my code, and that I have displayed it here for others to learn from. Don’t simply copy it and call it your own. I own copyright, and because I show the code does not mean that I have given up my rights to this code.

 1  /**
 2   * EVEN-EVEN Simulation
 3   * Written By: Dallin Jones
 4   * CS3240 - Computational Theory
 5   * For use by him and his professor (and person he dubs as the grader)
 6   * It is not to be used by any one else. 
 7   */
 8
 9  package com.dallinjones.cs3240.p1;
10
11  import java.io.*;
12
13  public class AppLauncher {
14    /**
15     * The transtion table works as follows:
16     *   Each row in the table represents a state from 0-3
17     *   State 0 Even-Even (Accepting state defined by [0][0]
18     *   State 1 Even-Odd (Not-Accepting state defined by [1][0]
19     *   State 2 Odd-Even (Not Accepting state defined by [2][0]
20     *   State 3 Odd-Odd (Not Accepting state defined by [3][0]
21     *   
22     *   Columns 1 and 2 show which state to move to for input in that column
23     *   The input given is currentState - 'a' + 1 (this shoudl give us 1 or 2
24     *   for a or b respectivley)  
25     */
26    private static int[][] transitionTable = {
27      {1, 1, 2},
28      {0, 0, 3},
29      {0, 3, 0},
30      {0, 2, 1}
31    };
32
33    public static int simulateEvenEven(String testString) {
34      int currentState = 0;
35      testString = testString.toLowerCase();
36      char[] brokenUp = testString.toCharArray();
37
38      for (int i = 0; i < brokenUp.length; i++) {
39        if ( (brokenUp[i] == 'a') || (brokenUp[i] == 'b') ) {
40          currentState = transitionTable[currentState][brokenUp[i] - 'a' + 1];
41        } else {
42          return 0;
43        }
44      }
45      return transitionTable[currentState][0];
46    }
47
48    /**
49     * @param args
50     */
51    public static void main(String[] args) {
52      boolean exit = false;
53      BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
54      String inputLine = null;
55
56      System.out.println("EVEN-EVEN Simulation\nDallin Jones\nCS3240\n");
57      while (!exit) {
58        try {
59          System.out.print("Type 'exit' to quit or enter a string >");
60          inputLine = input.readLine();
61          if (inputLine.equals("exit")) {
62            exit = true;
63          } else {
64          int returnValue = simulateEvenEven(inputLine);
65            System.out.print("'" + inputLine + "' ");
66            if (returnValue != 0) {
67              System.out.println("- Valid string");
68            } else {
69              System.out.println("- Invalid string");
70            }
71          }
72        } catch (IOException ioe) {
73          System.out.println("IOExpection caught while trying to read an input string!\n");
74          System.exit(1);
75        }
76      }
77      System.out.println("Exiting Program...");
78    }
79  }

Leave a Reply


Switch to our mobile site