assi1final_edited

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 18

1)

import java.util.Scanner;

class anbm {
static int dfa = 0;
static void start(char c) {
if (c == 'a') {
dfa = 1;
} else if (c == 'b') {
dfa = 3;
} else {
dfa = -1;
}
}
static void state1(char c) {
if (c == 'a') {
dfa = 2;
} else if (c == 'b') {
dfa = 4;
} else {
dfa = -1;
}
}
static void state2(char c) {
if (c == 'b') {
dfa = 3;
} else if (c == 'a') {
dfa = 1;
} else {
dfa = -1;
}
}
static void state3(char c) {
if (c == 'b') {
dfa = 3;
} else if (c == 'a') {
dfa = 4;
} else {
dfa = -1;
}
}
static void state4(char c) {
dfa = -1;
}

Dept of ISE | SCEM, Mangaluru


static boolean isAccepted(String str) {

int len = str.length();

for (int i = 0; i < len; i++) {


if (dfa == 0)
start(str.charAt(i));
else if (dfa == 1)
state1(str.charAt(i));
else if (dfa == 2)
state2(str.charAt(i));
else if (dfa == 3)
state3(str.charAt(i));
else if (dfa == 4)
state4(str.charAt(i));
else
return false;
}
return dfa == 3;
}

public static void main(String[] args) {


Scanner scanner = new Scanner(System.in);
String input;
do {
System.out.println("Enter a string (or 'no' to quit):");
input = scanner.nextLine();
if (!input.equalsIgnoreCase("no")) {
if (isAccepted(input)) {
System.out.println("ACCEPTED");
} else {
System.out.println("NOT ACCEPTED");
}
}
} while (!input.equalsIgnoreCase("no"));
scanner.close();
}
}

Dept of ISE | SCEM, Mangaluru


Fig 1. Output of 1st program

1. Test case: "aabbb"


• Output: NOT ACCEPTED
• Analysis: In this string, there are two 'a's followed by three 'b's. This violates the pattern
of 'anbn' where the number of 'a's should be equal to the number of 'b's. Hence, it is not
accepted.

2. Test case: "aaabbb"


• Output: NOT ACCEPTED
• Analysis: This string consists of three 'a's followed by three 'b's. While the count of
'a's matches the count of 'b's, they are not in the correct alternating pattern 'anbn'.
Hence, it is not accepted.

3. Test case: "aaaa"


• Output: NOT ACCEPTED
• Analysis: This string consists of four 'a's and no 'b's. Since there are no 'b's, it doesn't
satisfy the pattern 'anbn'. Hence, it is not accepted.

Dept of ISE | SCEM, Mangaluru


2)

import java.util.Scanner;
class aeb{
static int dfa = 0;

static void start(char c)


{
if (c == 'a') {
dfa = 10;
}
else if (c == 'b') {
dfa = 1;
}
else {
dfa = -1;
}
}
static void state01(char c)
{
if (c == 'a') {
dfa = 11;
}
else if (c == 'b') {
dfa = 2;
}
else {
dfa = -1;
}
}
static void state02(char c)
{
if (c == 'b') {
dfa = 0;
}
else if (c == 'a') {
dfa = 12;
}
else {
dfa = -1;
}
}
static void state10(char c)
{
if (c == 'b') {

Dept of ISE | SCEM, Mangaluru


dfa = 11;
}

else if (c == 'a') {
dfa = 20;
}
else {
dfa = -1;
}
}
static void state11(char c)
{
if (c == 'b') {
dfa = 12;
}
else if (c == 'a') {
dfa = 21;
}
else {
dfa = -1;
}
}
static void state12(char c)
{
if (c == 'b') {
dfa = 10;
}
else if (c == 'a') {
dfa = 22;
}
else {
dfa = -1;
}
}
static void state20(char c)
{
if (c == 'b') {
dfa = 21;
}
else if (c == 'a') {
dfa = 0;
}
else {
dfa = -1;
}
}
static void state21(char c)
{

Dept of ISE | SCEM, Mangaluru


if (c == 'b') {
dfa = 22;

}
else if (c == 'a') {
dfa = 1;
}
else {
dfa = -1;
}
}
static void state22(char c)
{
if (c == 'b') {
dfa = 20;
}
else if (c == 'a') {
dfa = 2;
}
else {
dfa = -1;
}
}

static boolean isAccepted(String st)


{
int i, len = st.length();
char[] str = st.toCharArray();
for (i = 0; i < len; i++) {
if (dfa == 0)
start(str[i]);

else if (dfa == 1)
state01(str[i]);

else if (dfa == 2)
state02(str[i]);

else if (dfa == 10)


state10(str[i]);

else if (dfa == 11)


state11(str[i]);

else if (dfa == 12)


state12(str[i]);

Dept of ISE | SCEM, Mangaluru


else if (dfa == 20)
state20(str[i]);

else if (dfa == 21)


state21(str[i]);

else if (dfa == 22)


state22(str[i]);

else
return false;
}
if (dfa == 0 || dfa == 11 || dfa == 22)
return true;
else
return false;
}

public static void main(String[] args) {


Scanner scanner = new Scanner(System.in);
String input;
do {
System.out.println("Enter a string (or 'no' to quit):");
input = scanner.nextLine();
if (!input.equalsIgnoreCase("no")) {
if (isAccepted(input)) {
System.out.println("ACCEPTED");
} else {
System.out.println("NOT ACCEPTED");
}
}
} while (!input.equalsIgnoreCase("no"));
scanner.close();
}
}

Dept of ISE | SCEM, Mangaluru


Fig 2. Output of 2nd program

1. Test Case 1: "aabbb"


• Output: ACCEPTED
• Analysis: This string follows a pattern where it starts with two 'a's followed by three
'b's. It reaches state 0 after processing the string, which is one of the accepting states.
Hence, it is accepted.

2. Test Case 2: "aabb"


• Output: ACCEPTED
• Analysis: This string consists of two 'a's followed by two 'b's. It follows the pattern and
reaches state 11, which is an accepting state. Hence, it is accepted.

3. Test Case 3: "aaaabbbb"


• Output: ACCEPTED
• Analysis: This string begins with four 'a's followed by four 'b's. It follows the pattern
and reaches state 22, which is an accepting state. Hence, it is accepted.

Dept of ISE | SCEM, Mangaluru


3)

import java.util.Scanner;

public class se {
static boolean q1(String s, int i) {
if (i == s.length()) {
return true;
}
if (s.charAt(i) == 'a')
return q1(s, i + 1);
else
return q2(s, i + 1);
}
static boolean q2(String s, int i) {
if (i == s.length()) {
return false;
}
if (s.charAt(i) == 'a')
return q1(s, i + 1);
else
return q2(s, i + 1);
}
static boolean q3(String s, int i) {
if (i == s.length()) {
return true;
}
if (s.charAt(i) == 'a')
return q4(s, i + 1);
else
return q3(s, i + 1);
}
static boolean q4(String s, int i) {
if (i == s.length()) {
return false;
}
if (s.charAt(i) == 'a')
return q4(s, i + 1);
else
return q3(s, i + 1);
}
static boolean q0(String s, int i) {
if (i == s.length()) {
return false;
}
if (s.charAt(i) == 'a')
return q1(s, i + 1);

Dept of ISE | SCEM, Mangaluru


else
return q3(s, i + 1);
}

static boolean isAccepted(String s) {


return q0(s, 0);
}

public static void main(String[] args) {


Scanner scanner = new Scanner(System.in);
String input;
do {
System.out.println("Enter a string (or 'no' to quit):");
input = scanner.nextLine();
if (!input.equalsIgnoreCase("no")) {
if (isAccepted(input)) {
System.out.println("ACCEPTED");
} else {
System.out.println("NOT ACCEPTED");
}
}
} while (!input.equalsIgnoreCase("no"));
scanner.close();
}
}

Fig 3. Output of 3rd program

Dept of ISE | SCEM, Mangaluru


1. Test Case 1: "aabbba"
• Output: ACCEPTED
• Analysis: This string is accepted because it starts and ends with the same character 'a',
which satisfies the language defined by the DFA. Hence, it is accepted.

2. Test Case 2: "bb"


• Output: ACCEPTED
• Analysis: This string is accepted because it starts and ends with the same character 'b',
which satisfies the language defined by the DFA. Hence, it is accepted.

3. Test Case 3: "ab"


• Output: NOT ACCEPTED
• Analysis: This string is not accepted because it starts with 'a' and ends with 'b'. In the
language defined by the DFA, strings must start and end with the same character, either
'a' or 'b'. Since this string violates that rule, it is not accepted.

Dept of ISE | SCEM, Mangaluru


4)

import java.util.*;

public class stend{


static void checkstateA(String n)
{
if (n.charAt(0) == '0')
stateB(n.substring(1));
else
stateD(n.substring(1));
}
static void stateB(String n)
{
if (n.length() == 0)
System.out.println("string not accepted");
else
{
if (n.charAt(0) == '1')
stateC(n.substring(1));
else
stateD(n.substring(1));
}
}
static void stateC(String n)
{
System.out.println("String accepted");
}
static void stateD(String n)
{
if (n.length() == 0)
System.out.println("string not accepted");
else
{
if (n.charAt(0) == '1')
stateD(n.substring(1));

else
stateE(n.substring(1));
}
}
static void stateE(String n)
{
if (n.length() == 0)
System.out.println("string not accepted");
else
{

Dept of ISE | SCEM, Mangaluru


if(n.charAt(0) == '0')
stateE(n.substring(1));
stateF(n.substring(1));
}
}
static void stateF(String n)
{
if (n.length() == 0)
System.out.println("string accepted");
else
{
if (n.charAt(0) == '1')
stateD(n.substring(1));
else
stateE(n.substring(1));
}
}
public static void main(String args[])
{
Scanner scanner = new Scanner(System.in);
String input;

do {
System.out.println("Enter a string (or 'no' to quit):");
input = scanner.nextLine();
if (!input.equalsIgnoreCase("no")) {
checkstateA(input);
}
} while (!input.equalsIgnoreCase("no"));

scanner.close();
}
}

Dept of ISE | SCEM, Mangaluru


Fig 4. Output of 4th program

1. Test Case 1: "010000"


• Output: String accepted
• Analysis: This input string is accepted because it starts with "01", which matches the
pattern required by the DFA to reach an accepting state.

2. Test Case 2: "1100111"


• Output: String not accepted
• Analysis: This input string is not accepted because it does not start or end with "01",
violating the pattern required by the DFA to reach an accepting state.

3. Test Case 3: "011101"


• Output: String accepted
• Analysis: This input string is accepted because it contains "01", which matches the
pattern required by the DFA to reach an accepting state.

Dept of ISE | SCEM, Mangaluru


5)

import java.util.*;

public class abefb{


static int startStateQ0(char s) {
int state;
if (s == 'a')
state = 1;
else if (s == 'b')
state = 2;
else
state = -1;
return state;
}
static int firstStateQ1(char s) {
int state;
if (s == 'a')
state = 1;
else if (s == 'b')
state = 2;
else
state = -1;
return state;
}
static int secondStateQ2(char s) {
int state;
if (s == 'b')
state = 2;
else if (s == 'a')
state = 3;
else
state = -1;
return state;
}
static int thirdStateQ3(char s) {
int state = 3;
return state;
}
static boolean isAcceptedString(String str) {
int state = 0;
for (int i = 0; i < str.length(); i++) {
if (state == 0)
state = startStateQ0(str.charAt(i));
else if (state == 1)
state = firstStateQ1(str.charAt(i));
else if (state == 2)

Dept of ISE | SCEM, Mangaluru


state = secondStateQ2(str.charAt(i));
else if (state == 3)
state = thirdStateQ3(str.charAt(i));
else
return false;
}
return (state == 1 || state == 2);
}

public static void main(String args[]) {


Scanner scanner = new Scanner(System.in);
String input;

do {
System.out.println("Enter a string (or 'no' to quit):");
input = scanner.nextLine();
if (!input.equalsIgnoreCase("no")) {
if (isAcceptedString(input))
System.out.println("ACCEPTED");
else
System.out.println("NOT ACCEPTED");
}
} while (!input.equalsIgnoreCase("no"));

scanner.close();
}
}

Dept of ISE | SCEM, Mangaluru


Fig 5. Output of 5th program

1. Test Case 1: "ab"


• Output: ACCEPTED
• Analysis: This input string is accepted because it starts with 'a'. According to the
language defined by the DFA, strings that start with 'a' are accepted.

2. Test Case 2: "aba"


• Output: NOT ACCEPTED
• Analysis: This input string is not accepted because it violates the language pattern. The
DFA expects strings to start with 'a', but after 'a', encountering 'b' and then another 'a' is
not allowed.

3. Test Case 3: "baaa"


• Output: NOT ACCEPTED
• Analysis: This input string is not accepted because it starts with 'b'. According to the
language defined by the DFA, only strings that start with 'a' are accepted.

4. Test Case 4: "aaaab"


• Output: ACCEPTED

Dept of ISE | SCEM, Mangaluru


• Analysis: This input string is accepted because it starts with 'a'. According to the
language defined by the DFA, strings that start with 'a' are accepted. Additionally, the
presence of 'b' after 'a's does not affect the acceptance because the DFA only checks the
initial character.

Dept of ISE | SCEM, Mangaluru

You might also like