Write two recursive methods that validate if the given string follows allowed patterns:
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
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"));
}
}
Write two recursive methods that validate if the given string follows allowed patterns: isIdentifier method checks...
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...
!!!!!!!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...