Question
Write two recursive methods that validate if the given string follows allowed patterns: isIdentifier method checks if the given string is a valid java identifier
Write two recursive methods that validate if the given string follows allowed patterns:
- 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
- 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
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started