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

I want a Java code about a simple state machine. Constructing a program that val

ID: 3755804 • Letter: I

Question

I want a Java code about a simple state machine.

Constructing a program that validates input using a state machine.

Taking 2 command line parameters – a state machine filename, then a filename of inputs to validate. For example:

run validator myStateMachine.txt myInputs.txt

That is the command line in Drjava.

State Machine: The state machine will come from a text file in the following space delimited format:

stateNumber inputCharacter transitionsToState

For example:

0 A 1

0 a 1

1 B 2

1 b 2

2 C 999

There are three terminal situations:

999 – success – when this state is reached, print “Success” and end.

failure (no transitions match) – print “Failure at position ___, found character _.”

failure (input string ends early) – print “Input string ended before success transition.”

Note – each state can have ANY NUMBER (1 or more) of transitions to other states. The transitions do not have to happen in any specific order (for example – state 1 could transition forwards to state 4 and state 5 could transition backward to state 2). States will be sorted in the text file (0,1,2,3,4, etc.).

Input File: The inputs to validate is some number (1 or more) of lines of text. Example:

abc

AbC

Both file have no space line.

Explanation / Answer

Java Code:


import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
class Validator
{
public static void main(String args[]) throws IOException
{


Scanner scanner = new Scanner(new File(args[0]));


Map<StateTransition, Integer> stateTransitions = new HashMap<StateTransition, Integer>();

while(scanner.hasNextLine())
{
stateTransitions.put(new StateTransition(scanner.nextInt(), scanner.next().toCharArray()[0]), scanner.nextInt());
}

scanner = new Scanner(new File(args[1]));
String inputString = scanner.next();
scanner.close();

Integer currentState = 0;
int i=0;
System.out.println("Testing with input string : " + inputString);

for(i=0; i<inputString.length(); i++)
{
currentState = stateTransitions.get(new StateTransition(currentState, inputString.charAt(i)));

if(currentState == null)
{
System.out.println("Failure at position " + i + " found character " + inputString.charAt(i));
break;
}

else if(currentState == 999)
{
System.out.println("Success");
break;
}
}

if(i == inputString.length())
{
System.out.println("Input string ended before success transition");
}
}
}


StateTransition.java
class StateTransition
{

int currentState;
char transitionInput;
public StateTransition(int currentState, char transitionInput)
{
this.currentState = currentState;
this.transitionInput = transitionInput;
}
@Override
public int hashCode()
{
return this.currentState + this.transitionInput;
}
@Override
public boolean equals(Object obj)
{
StateTransition st = (StateTransition)obj;
return (this.currentState == st.currentState && this.transitionInput == st.transitionInput);
}
}

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