Question: Java Build a state machine with the simple task of recognizing if a sequence is valid or invalid based on some rules. There are a

Java

Build a state machine with the simple task of recognizing if a sequence is valid or invalid based on some rules. There are a number of tasks that you will need to satisfy for the requirements: ?rst is that you must use a hash-map to represent your di?erent states and you must read the rules for that said machine using a text ?le.

public class DFA

{

/* hints for your project*/

// you can have more than one final state

private List finalStates;

// You only have one starting state in a DFA

private String Start = null;

// What state are you on now?

private String currentState = null ;

// Where are you storing your states?

private HashMap stateMap ;

/*...*/

}

For this project you will need to read from a text ?le. You will be building your state machine as you read the input. How you de?ne your input is completely up to you, you can make an object State or you can just represent them as strings. (I only care that it works)

/**

ReadData reads the data some file.

Decides the rules of the state

@var State is the current state

@var stateInput is the input from the sequence

@var State1 is the state resulted from the input

After the states are decided we put them in the stateMap HashMap

*/

private void ReadData()

{ String State= null;

String stateInput = null;

String transitionState = null;

// DFARules.txt is just the file name with your rules

// You can give it another name and another address

java.io.File file = new java.io.File("DFARulles.txt");

try {

Scanner input = new Scanner(file);

while (input.hasNext())

{

String cord = input.nextLine();

Scanner Scord = new Scanner (cord);

State= Scord.next();

// populate your state machine

// Hint: Since every machine is

// labled by a number 1-n

// use the key for each map

// the same way you would for a

// point in an x,y grid (2D)

// For example (fromstate "," input)

}

}

catch (FileNotFoundException e)

{

//We are kinda screwed then

System.out.println("No State Machine rules found. "

+ "Please look at the instructions and try again.");

} }

The Java DFA you make will take in two inputs. One input will be from a text ?le and the other will be in the form of comandline input (string) to test if a sequence satis?es the DFA rules. The text ?le can be located in the same ?le as the java ?les so that you can retain a static address and not have to prompt the user for the rules ?le. It would be nicer to prompt the user for the ?le but that is a bit too much to do.

Java Build a state machine with the simple task of recognizing if

For simplicity the user would enter the state then the input and the next transition state (all three separated by space) on one line. For stating that a state is the start state the user types start before it to hint the program that it is a start state. For the ?nal states the user can type the word ?nal before them to indicate that those states are ?nal states. Each rule should be on a separate line otherwise you will have a hard time making this.

a sequence is valid or invalid based on some rules. There are

Then compile the main class preferably in jGRASP and enter a sequence you wish to test without any spaces for example: abba

Here is my code thus far :

import java.util.Scanner;

import java.lang.String;

public class DFAImplementation {

public static void main(String[] args) throws Exception {

//create the file instance

java.io.File file = new java.io.File("DFA.txt");

//it's scanner time

Scanner input = new Scanner(file);

//read it

int dfaAmount = input.nextInt();

//loop for amount of dfas

for (int dfaAmountIndex = 0; dfaAmountIndex

dfaAmountIndex++){

int numberOfStates = input.nextInt();

int startState;

int numberOfSigmaSym;

String sigma = "";

sigma = input.next();

numberOfSigmaSym = sigma.length();

String transitionLine = input.nextLine();

int[][] transition = new int[numberOfStates][numberOfSigmaSym];

startState = input.nextInt();

//checker

System.out.println(startState);

int i;

int j;

for (i=0; i

numberOfStates = i;

numberOfSigmaSym = j;

for (j=0; j

int state1 = transition[i][j];

}

}

String w;

w = input.nextLine();

for (i=0; i

char x;

x = w.charAt(i);

int state = startState;

int index = sigma.indexOf(x);

int state1 = transition [state][index];

}

int numberOfAcceptStates;

numberOfAcceptStates = input.nextInt();

int acceptState;

acceptState = input.nextInt();

// if(){

}

// else(){

}

}

//}

2 0 2 start e final 2 Text file for inputting the rules of the DFA Figure 2: An example of what the project should look like in the file being submitted. I don't want a project

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!