Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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:

  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

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

8. Describe how cultural spaces are formed.

Answered: 1 week ago