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...
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;...
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
Please can you draw a flow chart for the following code : Program code for Payroll,java:...
Please can you draw a flow chart for the following code : Program code for Payroll,java: public class Payroll { public Payroll(String name,int ID,double payRate) { this.name=name; this.ID=ID; this.payRate=payRate; } private String name; private double payRate,hrWorked; private int ID; public Payroll() { name="John Doe"; ID=9999; payRate=15.0; hrWorked=40; } public String getName() { return name; } public int getID() { return ID; } public void setPayRate(int payRate) { this.payRate=payRate; } public void setHrWorked(double hrWorked) { this.hrWorked=hrWorked; } public double getPayRate() {...
For the following two questions you need to provide a code in C 1. Build a...
For the following two questions you need to provide a code in C 1. Build a double linked list with 5 in the first node following the instructions: Insert a node with 3 at the top of the list Insert a node with 10 at the bottom of the list Insert a node with 7 between nodes with 5 and 10 2. Start deleting nodes from the list in the following order; Delete the node with 7 Delete the node...
1.What standard methods can you use when handling file-reading and file-writing operations? a. Java b. Fragment-based...
1.What standard methods can you use when handling file-reading and file-writing operations? a. Java b. Fragment-based methods c. onClick d. DDMS 2. What would this query return? SELECT * FROM Items WHERE OrderDate >= Date('Now') a. All items in the database. b. Nothing c. Items that were ordered yesterday d. Items that were ordered today. 3. What can you use to manage files within a desired directory and create subdirectories? a. Subdirectory objects b. Directory class c. File objects d....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT