Question

In: Computer Science

Can you provide source code in Java that 1. accepts a two-way acceptor via file; 2....

Can you provide source code in Java that
1. accepts a two-way acceptor via file;
2. and validate if the input string is part of the language

Solutions

Expert Solution

DFA (Deterministic Finite Automaton or Acceptor) is a finite state machine that accepts or rejects strings of symbols. DFA accepts the string if it reaches the final state and rejects otherwise.

Now the problem is, provided a string as input character by character and we have to check whether the string starts and ends with ‘a’. We can only store the current character, since there is no concept of memory and hence the DFA cannot store the string provided. Otherwise, we could have just checked the first and last character for this problem. The input set for this problem is (a, b).

We cannot store anything accept the current character, which make this program a little different and tough than other string related problems.

Examples:

Input : a b a b a
Output : Yes
Explanation : (a b a b a) starts and
end with 'a'

Input : a b a b b
Output : No
Explanation : (a b a b b) starts with
'a' but doesn't end with 'a'

We first build a DFA for this problem. Making DFA is like making a flowchart for this program and then implement it in any language. You should have the knowledge of DFA and Finite Automata.

The DFA for given problem is:-

Program:-
// JAVA Program to DFA that accept Strings

// which starts and end with 'a' over input(a, b)
import java.util.*;
  
class GFG
{
  
public static void main(String[] args)
{
// for producing different random
// numbers every time.
Random r = new Random();
  
// random length of String from 1 - 16
// we are taking input from input stream,
// we can take delimiter to end the String
int max = 1 + r.nextInt()*10 % 15;
  
// generating random String and processing it
int i = 0;
while (i < max)
{
  
// producing random character over
// input alphabet (a, b)
char c = (char) ('a' + r.nextInt()*10 % 2);
System.out.print(c+ " ");
i++;
  
// first character is 'a'
if (c == 'a')
{
  
// if there is only 1 character
// i.e. 'a'
if (i == max)
System.out.print("YES\n");
  
while (i < max)
{
c = (char) ('a' + r.nextInt()*10 % 2);
System.out.print(c+ " ");
i++;
  
// if character is 'a' and it
// is the last character
if (c == 'a' && i == max)
{
System.out.print("\nYES\n");
}
  
// if character is 'b' and it
// is the last character
else if (i == max)
{
System.out.print("\nNO\n");
}
}
}
  
// first character is 'b' so no matter
// what the String is, it is not going
// to be accepted
else
{
while (i < max)
{
c = (char) ('a' + r.nextInt()*10 % 2);
System.out.print(c+ " ");
i++;
}
System.out.print("\nNO\n");
}
}
}
}
  
Sample Output:-

a a b a a
YES


Related Solutions

Your hardcopy submission will consist of these two things: Source code (your java file). Screenshot of...
Your hardcopy submission will consist of these two things: Source code (your java file). Screenshot of Eclipse showing output of program. (JPG or PNG format only) Write a program to play the game of Stud Black Jack. This is like regular Black Jack except each player gets 2 cards only and cannot draw additional cards. Create an array of 52 integers. Initialize the array with the values 0-51 (each value representing a card in a deck of cards) in your...
Can you provide java code for a program that prompts the user by asking "How many...
Can you provide java code for a program that prompts the user by asking "How many one mile races have you ran?". After the user inputs how many one mile races have they run it then prompts the user to input how many seconds it took for them to finish each race. So for example, if the user ran 6 races then the user will have to input 6 race times. Is there a way when you prompt the user...
The code file is attached. Portion of the code is given (1, 2, 3, 4). You...
The code file is attached. Portion of the code is given (1, 2, 3, 4). You have to write the code for the remaining items (5, 6, 7). #include <iostream> using namespace std; struct node { int data; node* next; }; node* head=NULL; void appendNode(int value) { node *newNode, *curr; newNode = new node(); newNode->data=value; newNode->next=NULL; if (!head) head=newNode; else { curr=head; while (curr->next) curr = curr->next; curr->next = newNode; } } void insertNode(int value) { node *newNode, *curr, *previous;...
Would you make separated this code by one .h file and two .c file? **********code************* #include...
Would you make separated this code by one .h file and two .c file? **********code************* #include <stdlib.h> #include <stdbool.h> #include <stdio.h> #include<time.h> // Prints out the rules of the game of "craps". void print_game_rule(void) { printf("Rules of the game of CRAPS\n"); printf("--------------------------\n"); printf("A player rolls two dice.Each die has six faces.\n"); printf("These faces contain 1, 2, 3, 4, 5, and 6 spots.\n"); printf("After the dice have come to rest, the sum of the spots\n on the two upward faces is...
please write the java code so it can run on jGRASP Thanks! 1 /** 2 *...
please write the java code so it can run on jGRASP Thanks! 1 /** 2 * PassArray 3 * @Sherri Vaseashta 4 * @Version 1 5 * @see 6 */ 7 import java.util.Scanner; 8 9 /** 10 This program demonstrates passing an array 11 as an argument to a method 12 */13 14 public class PassArray 15 { 16 public static void main(String[] args) 17 { 18 19 final int ARRAY_SIZE = 4; //Size of the array 20 // Create...
Java and please have screenshots with source code and output \item[(1)] A palindrome is a string...
Java and please have screenshots with source code and output \item[(1)] A palindrome is a string that reads the same forwards as backwards. Using only a fixed number of stacks and queues, the stack and queue ADT functions, and a fixed number of int and char variables, write an algorithm to determine if a string is a palindrome. Assume that the string is read from standard input one character at a time. The algorithm should output true or false as...
please write the java code so it can run on jGRASP Thanks! CODE 1 1 /**...
please write the java code so it can run on jGRASP Thanks! CODE 1 1 /** 2 * SameArray2.java 3 * @author Sherri Vaseashta4 * @version1 5 * @see 6 */ 7 import java.util.Scanner;8 public class SameArray29{ 10 public static void main(String[] args) 11 { 12 int[] array1 = {2, 4, 6, 8, 10}; 13 int[] array2 = new int[5]; //initializing array2 14 15 //copies the content of array1 and array2 16 for (int arrayCounter = 0; arrayCounter < 5;...
can you please convert this python code into java? Python code is as shown below: #...
can you please convert this python code into java? Python code is as shown below: # recursive function def row_puzzle_rec(row, pos, visited):    # if the element at the current position is 0 we have reached our goal    if row[pos] == 0:        possible = True    else:        # make a copy of the visited array        visited = visited[:]        # if the element at the current position has been already visited then...
(java) Part 1 Write a method named compare that accepts two String arrays as parameters and...
(java) Part 1 Write a method named compare that accepts two String arrays as parameters and returns a boolean. The method should return true if both arrays contain the same values (case sensitive), otherwise returns false. Note - the two arrays must be the same length and the values must be in the same order to return true. Part  2 Write a method named generateArray that accepts an int size as its parameter and returns an int[] array where each element...
Describe the rules governing identifiers. Can identifiers be re-used in the same Java class file? Provide...
Describe the rules governing identifiers. Can identifiers be re-used in the same Java class file? Provide three examples of different kinds of invalid identifiers. Java
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT