Question

Write two recursive methods that validate if the given string follows allowed patterns: isIdentifier method checks...

Write two recursive methods that validate if the given string follows allowed patterns:

  1. isIdentifier method checks if the given string is a valid java identifier:
    • must start with a letter,
    • the subsequent characters must be letters or digits
    • the length of a valid identifier must be at least one
  2. isDashDotCode method checks if the given string is a valid word in the following pattern:
    • the characters must be dots or dashes
    • the minimum length of a valid word is one
    • only the following patterns are allowed:
word -> dash OR dot followed by word OR word followed by dash
public class ValidatePatterns
{
    /**
     * Write a recursive method that checks if the given string is a valid java identifier:
     *      must start with a letter,
     *      the subsequent characters must be letters or digits
     *      the length of a valid identifier must be at least one
     */
    private static boolean isIdentifier(String str)
    {
        //TODO Project #2a
        // utilize String methods like: length, substring and charAt
        // utilize Character.isLetter and Character.isLetterOrDigit



        return false; //THIS IS A STUB
    }


    /**
     * Write a recursive method that validates if the given string is a valid word in the following pattern:
     *    the characters must be dots or dashes
     *    only the following patterns are allowed:
     *    word -> dash
     *            OR dot followed by word
     *            OR word followed by dash
     */
    private static boolean isDashDotCode(String str)
    {
        //TODO Project #2b
        // utilize String methods like substring and charAt

        return false; // THIS IS A STUB
    }

    public static void main(String[] args)
    {
        String toCheck = "AB5";
        System.out.println("Is \"" + toCheck + "\" a valid identifier ? --> "
                + (isIdentifier(toCheck) ? "YES" : "NO"));
        toCheck = "A?B5";
        System.out.println("Is \"" + toCheck + "\" a valid identifier ? --> "
                + (isIdentifier(toCheck) ? "YES" : "NO"));
        toCheck = "5AB5";
        System.out.println("Is \"" + toCheck + "\" a valid identifier ? --> "
                + (isIdentifier(toCheck) ? "YES" : "NO"));
        toCheck = "ABcdefg";
        System.out.println("Is \"" + toCheck + "\" a valid identifier ? --> "
                + (isIdentifier(toCheck) ? "YES" : "NO"));
        toCheck = "12345A";
        System.out.println("Is \"" + toCheck + "\" a valid identifier ? --> "
                + (isIdentifier(toCheck) ? "YES" : "NO"));
        toCheck = "A12345";
        System.out.println("Is \"" + toCheck + "\" a valid identifier ? --> "
                + (isIdentifier(toCheck) ? "YES" : "NO"));
        toCheck = "";
        System.out.println("Is \"" + toCheck + "\" a valid identifier ? --> "
                + (isIdentifier(toCheck) ? "YES" : "NO"));

        System.out.println();
        toCheck = "---";
        System.out.println("Is \"" + toCheck + "\" a valid DashDot code ? --> "
                + (isDashDotCode(toCheck) ? "YES" : "NO"));
        toCheck = ".--";
        System.out.println("Is \"" + toCheck + "\" a valid DashDot code ? --> "
                + (isDashDotCode(toCheck) ? "YES" : "NO"));
        toCheck = "..-";
        System.out.println("Is \"" + toCheck + "\" a valid DashDot code ? --> "
                + (isDashDotCode(toCheck) ? "YES" : "NO"));
        toCheck = "--.";
        System.out.println("Is \"" + toCheck + "\" a valid DashDot code ? --> "
                + (isDashDotCode(toCheck) ? "YES" : "NO"));
        toCheck = "-.-";
        System.out.println("Is \"" + toCheck + "\" a valid DashDot code ? --> "
                + (isDashDotCode(toCheck) ? "YES" : "NO"));

        toCheck = "-..";
        System.out.println("Is \"" + toCheck + "\" a valid DashDot code ? --> "
                + (isDashDotCode(toCheck) ? "YES" : "NO"));
        toCheck = ".-.";
        System.out.println("Is \"" + toCheck + "\" a valid DashDot code ? --> "
                + (isDashDotCode(toCheck) ? "YES" : "NO"));
        toCheck = "...";
        System.out.println("Is \"" + toCheck + "\" a valid DashDot code ? --> "
                + (isDashDotCode(toCheck) ? "YES" : "NO"));
        toCheck = "+..";
        System.out.println("Is \"" + toCheck + "\" a valid DashDot code ? --> "
                + (isDashDotCode(toCheck) ? "YES" : "NO"));
        toCheck = ".-.-";
        System.out.println("Is \"" + toCheck + "\" a valid DashDot code ? --> "
                + (isDashDotCode(toCheck) ? "YES" : "NO"));
        toCheck = "..--";
        System.out.println("Is \"" + toCheck + "\" a valid DashDot code ? --> "
                + (isDashDotCode(toCheck) ? "YES" : "NO"));
        toCheck = "-";
        System.out.println("Is \"" + toCheck + "\" a valid DashDot code ? --> "
                + (isDashDotCode(toCheck) ? "YES" : "NO"));
        toCheck = ".";
        System.out.println("Is \"" + toCheck + "\" a valid DashDot code ? --> "
                + (isDashDotCode(toCheck) ? "YES" : "NO"));
        toCheck = "...----";
        System.out.println("Is \"" + toCheck + "\" a valid DashDot code ? --> "
                + (isDashDotCode(toCheck) ? "YES" : "NO"));
        toCheck = "";
        System.out.println("Is \"" + toCheck + "\" a valid DashDot code ? --> "
                + (isDashDotCode(toCheck) ? "YES" : "NO"));
    }
}

See the sample run:

Is "AB5" a valid identifier ? --> YES

Is "A?B5" a valid identifier ? --> NO

Is "5AB5" a valid identifier ? --> NO

Is "ABcdefg" a valid identifier ? --> YES

Is "12345A" a valid identifier ? --> NO

Is "A12345" a valid identifier ? --> YES

Is "" a valid identifier ? --> NO

Is "---" a valid DashDot code ? --> YES

Is ".--" a valid DashDot code ? --> YES

Is "..-" a valid DashDot code ? --> YES

Is "--." a valid DashDot code ? --> NO

Is "-.-" a valid DashDot code ? --> NO

Is "-.." a valid DashDot code ? --> NO

Is ".-." a valid DashDot code ? --> NO

Is "..." a valid DashDot code ? --> NO

Is "+.." a valid DashDot code ? --> NO

Is ".-.-" a valid DashDot code ? --> NO

Is "..--" a valid DashDot code ? --> YES

Is "-" a valid DashDot code ? --> YES

Is "." a valid DashDot code ? --> NO

Is "...----" a valid DashDot code ? --> YES

Is "" a valid DashDot code ? --> NO

Process finished with exit code 0

0 0
Add a comment Improve this question Transcribed image text
Answer #1
public class ValidatePatterns
{
    /**
     * Write a recursive method that checks if the given string is a valid java identifier:
     *      must start with a letter,
     *      the subsequent characters must be letters or digits
     *      the length of a valid identifier must be at least one
     */
    private static boolean isIdentifier(String str)
    {
        //TODO Project #2a
        // utilize String methods like: length, substring and charAt
        // utilize Character.isLetter and Character.isLetterOrDigit
        int length = str.length();
        if (length == 0)
            return false;

        char c = str.charAt(length - 1);

        if (length == 1) {
            if (Character.isLetter(c))
                return true;
            else
                return false;
        } else {
            if (Character.isLetterOrDigit(c))
                return isIdentifier(str.substring(0, length - 1));
            else
                return false;
        }

    }

    private  static boolean isDashDotCode(String str) {
        return isDashDotCodeBase(str, false, '\0');
    }
    /**
     * Write a recursive method that validates if the given string is a valid word in the following pattern:
     *    the characters must be dots or dashes
     *    only the following patterns are allowed:
     *    word -> dash
     *            OR dot followed by word
     *            OR word followed by dash
     */
    private static boolean isDashDotCodeBase(String str, boolean switched, char previous)
    {
        //TODO Project #2b
        // utilize String methods like substring and charAt
        int length = str.length();
        // store state, i.e. whether pattern has been switched one time

        if (length == 0) {
            if (previous == '\0')
                return false;
            else
                return true;
        }

        else {
            char a = str.charAt(0);
            if (a != '.' && a != '-')
                return false;
            if (previous != '\0' && a != previous) {
                if (switched)
                    return false;
                return isDashDotCodeBase(str.substring(1), true, a);
            } else {
                return isDashDotCodeBase(str.substring(1), switched, a);
            }
        }
    }

    public static void main(String[] args)
    {
        String toCheck = "AB5";
        System.out.println("Is \"" + toCheck + "\" a valid identifier ? --> "
                + (isIdentifier(toCheck) ? "YES" : "NO"));
        toCheck = "A?B5";
        System.out.println("Is \"" + toCheck + "\" a valid identifier ? --> "
                + (isIdentifier(toCheck) ? "YES" : "NO"));
        toCheck = "5AB5";
        System.out.println("Is \"" + toCheck + "\" a valid identifier ? --> "
                + (isIdentifier(toCheck) ? "YES" : "NO"));
        toCheck = "ABcdefg";
        System.out.println("Is \"" + toCheck + "\" a valid identifier ? --> "
                + (isIdentifier(toCheck) ? "YES" : "NO"));
        toCheck = "12345A";
        System.out.println("Is \"" + toCheck + "\" a valid identifier ? --> "
                + (isIdentifier(toCheck) ? "YES" : "NO"));
        toCheck = "A12345";
        System.out.println("Is \"" + toCheck + "\" a valid identifier ? --> "
                + (isIdentifier(toCheck) ? "YES" : "NO"));
        toCheck = "";
        System.out.println("Is \"" + toCheck + "\" a valid identifier ? --> "
                + (isIdentifier(toCheck) ? "YES" : "NO"));

        System.out.println();
        toCheck = "---";
        System.out.println("Is \"" + toCheck + "\" a valid DashDot code ? --> "
                + (isDashDotCode(toCheck) ? "YES" : "NO"));
        toCheck = ".--";
        System.out.println("Is \"" + toCheck + "\" a valid DashDot code ? --> "
                + (isDashDotCode(toCheck) ? "YES" : "NO"));
        toCheck = "..-";
        System.out.println("Is \"" + toCheck + "\" a valid DashDot code ? --> "
                + (isDashDotCode(toCheck) ? "YES" : "NO"));
        toCheck = "--.";
        System.out.println("Is \"" + toCheck + "\" a valid DashDot code ? --> "
                + (isDashDotCode(toCheck) ? "YES" : "NO"));
        toCheck = "-.-";
        System.out.println("Is \"" + toCheck + "\" a valid DashDot code ? --> "
                + (isDashDotCode(toCheck) ? "YES" : "NO"));

        toCheck = "-..";
        System.out.println("Is \"" + toCheck + "\" a valid DashDot code ? --> "
                + (isDashDotCode(toCheck) ? "YES" : "NO"));
        toCheck = ".-.";
        System.out.println("Is \"" + toCheck + "\" a valid DashDot code ? --> "
                + (isDashDotCode(toCheck) ? "YES" : "NO"));
        toCheck = "...";
        System.out.println("Is \"" + toCheck + "\" a valid DashDot code ? --> "
                + (isDashDotCode(toCheck) ? "YES" : "NO"));
        toCheck = "+..";
        System.out.println("Is \"" + toCheck + "\" a valid DashDot code ? --> "
                + (isDashDotCode(toCheck) ? "YES" : "NO"));
        toCheck = ".-.-";
        System.out.println("Is \"" + toCheck + "\" a valid DashDot code ? --> "
                + (isDashDotCode(toCheck) ? "YES" : "NO"));
        toCheck = "..--";
        System.out.println("Is \"" + toCheck + "\" a valid DashDot code ? --> "
                + (isDashDotCode(toCheck) ? "YES" : "NO"));
        toCheck = "-";
        System.out.println("Is \"" + toCheck + "\" a valid DashDot code ? --> "
                + (isDashDotCode(toCheck) ? "YES" : "NO"));
        toCheck = ".";
        System.out.println("Is \"" + toCheck + "\" a valid DashDot code ? --> "
                + (isDashDotCode(toCheck) ? "YES" : "NO"));
        toCheck = "...----";
        System.out.println("Is \"" + toCheck + "\" a valid DashDot code ? --> "
                + (isDashDotCode(toCheck) ? "YES" : "NO"));
        toCheck = "";
        System.out.println("Is \"" + toCheck + "\" a valid DashDot code ? --> "
                + (isDashDotCode(toCheck) ? "YES" : "NO"));
    }
}
Add a comment
Know the answer?
Add Answer to:
Write two recursive methods that validate if the given string follows allowed patterns: isIdentifier method checks...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • language is java Restrictions: You are not allowed to use anything from the String, StringBuilder, or...

    language is java Restrictions: You are not allowed to use anything from the String, StringBuilder, or Wrapper classes. In general, you may not use anything from any other Java classes, unless otherwise specified. You are not allowed to use String literals in your code ("this is a string literal"). You are not allowed to use String objects in your code. The methods must be implemented by manipulating the data field array, The CSString Class: NOTE: Pay very careful attention to...

  • Assignment 14.3: Valid Email (10 pts) image source Write a program that takes as input an...

    Assignment 14.3: Valid Email (10 pts) image source Write a program that takes as input an email address, and reports to the user whether or not the email address is valid. For the purposes of this assignment, we will consider a valid email address to be one that contains an @ symbol The program must allow the user to input as many email addresses as desired until the user enters "q" to quit. For each email entered, the program should...

  • For this lab you will write a Java program that plays a simple Guess The Word...

    For this lab you will write a Java program that plays a simple Guess The Word game. The program will prompt the user to enter the name of a file containing a list of words. These words mustbe stored in an ArrayList, and the program will not know how many words are in the file before it starts putting them in the list. When all of the words have been read from the file, the program randomly chooses one word...

  • !!!!!!!Java!!!!! When you are confident that your methods work properly and that you can generate random...

    !!!!!!!Java!!!!! When you are confident that your methods work properly and that you can generate random text with my generateText method, you can move on to the second step. Create a third class called Generator within the cs1410 package. Make class. This class should have a main method that provides a user interface for random text generation. Your interface should work as follows: Main should bring up an input dialog with which the user can enter the desired analysis level...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT