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

1. Write a Java program to read in a DSFM, then decides if an input string is ac

ID: 3726538 • Letter: 1

Question

1. Write a Java program to read in a DSFM, then decides if an input string is accepted by the DSFM or not. You can assume the input DSFM is legal, and states are named with single letters. Note . The program has 2 parts: l. Read in the start tate, the final states, and the transitions of the input DSFM. (no need to read| K or since the transitions would imply those) -Use a Vector String to store the set of final states. . Use a Vector to store the set of tansitions. 2. Determine if an input string is accepted or rejected based on algorithm dfsnSimulate. String methods such as sta With, and substring are useful . The following run is for the DSFM deseribed in Ex2-4-1: nter final states, 1 on each line. Enter to end; nter trons itions, 1 *n·ach line with no ws 1 state, letter,state. Enter 0,8,0 to end: b.t nter input string. enter done to endia nter input string, enter done to end:ab ccept er input string, nter, dare to ®nd:ahb eject nter input string. enter done to end:aba ccept nter input string, enter done to erd :abda ccept nter input string. enter done to end:abbb nter input string, enter done to end:b ccept er input #tring, enter done to end:hb eject

Explanation / Answer

Below is the complete and tested java code for DSFM:

package knapsack;
import java.util.*;

/**
*
* @author Administrator
*/
public class DFSM {
public static void main(String[] args)
{
Vector<String> fs=new Vector();
Vector<String> ts=new Vector();
StringTokenizer st;
String input="";
String fstate="";
String str="";
String str1="";
Scanner sc=new Scanner(System.in);
System.out.println("Enter Start state");
String sstart= sc.next();
System.out.println("Enter final states 0 to quit");
  
while(!(str=sc.next()).equalsIgnoreCase("0"))
{
  
fstate=str;
  
  
fs.add(fstate);
  
  
}
  
System.out.println("|Enter Transitions:state,letter,state,enter (0,0,0) to end");
  
while(!(str1=sc.next()).equalsIgnoreCase("0,0,0"))
{
  

  
  
ts.add(str1);
  
  
}
boolean flag=false;
System.out.println("|Enter input string");
input=sc.next();
  
String modify="";
String strfinal="";
for(int i=0;i<ts.size();i++)
{
  
st=new StringTokenizer(ts.get(i),",");
while(st.hasMoreTokens())
{
modify=modify+st.nextToken();
}
strfinal=strfinal+modify.charAt(1);
  
if(strfinal.contains(input))
{
  
flag=true;
}
  
modify="";
}
if((input.startsWith(sstart) & input.endsWith(fstate)) | input.startsWith(sstart) | flag)
{
  
  
System.out.println("Accepted");
}
  
else
{
System.out.println("reject");
}
  
  

}
  
}

O/P

Enter Start state
s
Enter final states 0 to quit
s
t
0
|Enter Transitions:state,letter,state,enter (0,0,0) to end
s,a,s
s,b,t
t,a,t
0,0,0
|Enter input string
abb
reject

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