Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7446599
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T12:21:34+00:00 2026-05-29T12:21:34+00:00

I was given the assignment to implement and test a language recognizer object, provided

  • 0

I was given the assignment to “implement and test a language “recognizer” object, provided to you through a Java interface defined at the end of this document. A language recognizer accepts strings of characters and determines whether or not they are in the language.”

The language is as follows:

L = {a*b} union {ab*}, or restated in English, L is the set of all strings of either (1) zero or more as (a*) followed by a b, or (2) an a followed by zero or more bs (b*).

I’ve made some progress, but I’m stuck.

Here’s the interface:

/** The Recognizer interface provides a recognizer for the
* language L below.
*
* Let Sigma = {a,b} = the input character set.
*
* Let L = {ab*} union {a*b} be the language (set of
* legal strings) recognized by this recognizer.
*
* Let S = s1s2...sn be the string of n characters already
* input by this recognizer.
*
* Recognizer constructor must ensure: S' = < >
*/
interface Recognizer {
/**
* require: c in Sigma
*
* ensure: S' = S ^ c
*
* param c
*/
public void nextChar(char c);
/**
* Checks if input string S is in language L.
*
* return (S in L)
*/
public boolean isIn();
/**
* ensure: S' = < >
*/
public void reset();
}

Here’s my structure:

import java.util.*;

public class LanguageVector implements Recognizer {

    int element = 0;
    int a = 0;
    int b = 0;

    Vector<Character> v = new Vector<Character>();

    public void nextChar(char c) {
            v.add(c);
        }

    public boolean isIn(){
            boolean isTrue = true;
            for(int i=0;i<v.size();i++) {
                if (v.size() == 1){
                    if (v.firstElement() == 'a' || v.firstElement() =='b'){
                        isTrue = true;
                        }
                    else
                        isTrue = false;
                }
                else if (v.firstElement() == 'a'){
                        if (v.lastElement() == 'a')
                            isTrue = false;
                        else if (v.lastElement() == 'b')
                            while (v.elementAt(element)== 'a' ){
                                a++;
                                element++;
                                System.out.println(element);
                            }
                            while (v.elementAt(element)== 'b'){
                                b++;
                                element++;
                                System.out.println(element);
                                }
                            if (v.elementAt(element)!= 'b'){
                                isTrue = false;
                            }
                            else if (a > 1 && b > 1){
                                isTrue = false;
                            }
                            else
                                isTrue = true;
                    }
                else if (v.firstElement() == 'b'){
                        isTrue = false;
                        }
                else
                    isTrue = false;
            }
            return isTrue;
            }


    public void reset(){
                v.clear();
            }

}

And here’s my testing class:

import java.util.*;

public class LanguageTester {
    /**
     * @param args
     */
    public static void main(String[] args) {
        Recognizer r = new LanguageVector();

        r.nextChar('a');

        r.nextChar('a');

        r.nextChar('a');

        r.nextChar('b');

        if (r.isIn())
            System.out.println("string is in L");
        else
            System.out.println("string is not in L");
        System.out.println("End of test");
        r.reset();
    }
}

When I run, I get the following output:

1
2
3
Exception in thread "main" 4
java.lang.ArrayIndexOutOfBoundsException: 4 >= 4
    at java.util.Vector.elementAt(Unknown Source)
    at LanguageVector.isIn(LanguageVector.java:34)
    at LanguageTester.main(LanguageTester.java:18)

Why is this happening?

Also, how can I use user input, turn it into a vector, and use that within this structure now?

Forgive me if this question is too lengthy, I wasn’t sure how to narrow it down without leaving important details out. Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T12:21:35+00:00Added an answer on May 29, 2026 at 12:21 pm

    When it occurs?

    Out of bounds exception is occurred when you try to access an array with index that exceeded its length. maximum index of a java array is (length -1)
    for example:

    String [] stringArray = new String[10];
    stringArray[10]
    // the code above will produce an out of bounds exception, because the it bigger than length -1, which is 10 - 1 = 9.
    

    If you don’t know the size or length of an array, you can know it from stringArray.length.

    How to handle it?

    You should make sure that your program doesn’t access an array with index bigger than length – 1.
    example:

    for(int i=0;i<stringArray.lenght;i++) {
        //write your code here
    }
    

    the above code will guarantee that stringArray will never be accessed beyond its maximum index.

    Your Case

    In your case, 4 >= 4 itself says you are trying to access 5th element i.e. elementAt(4) however size of your Vector of 4.

    Array is based on 0 index i.e. if your length is 4 you will have data at as Vector[0], Vector[1], Vector[2], Vector[3].
    Also read this for more info…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My professor has given me this assignment. Implement a generic function called Max, which
My assignment is to implement a multithreaded web server in Java but i have
I have a Assignment question about Java Generics. The given class is Product.java. It
I have an assignment to implement a Ram-Lak filter, but nearly no information given
I'm working on a java object oriented expression tree assignment where I need to
I had been given an assignment to implement ArrayList and LinkedList without using generics.
My boss have given me assignment to find how a web based application developed
For a homework assignment I was given a Card class that has enumerated types
Given a Python object of any kind, is there an easy way to get
Given the URL (single line): http://test.example.com/dir/subdir/file.html How can I extract the following parts using

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.