Q1: [JF edited] In the language of an alien race, all words take the form of Blu
ID: 3818882 • Letter: Q
Question
Q1: [JF edited] In the language of an alien race, all words take the form of Blurbs. A Blurb is a Whoozit followed by between zero and ten Whatzits. A Whoozit is the character 'x' followed by between one and ten 'y's. A Whatzit is a 'q' followed by either a 'z' or a 'd', followed by a Whoozit. Design and implement a recursive program that generates 10 random Blurbs in this alien language. (Hint: start by trying to generate Whoozits, Whatzits, and Blurbs by hand and on paper. Be sure to follow that order. Once you have a feeling for the structure of the language, then start to think about how you might program it.) [15 points]
Sample Output:
I've figured out question 1, but I am struggling with question 2 below is the code I have written for question 1:
import java.util.*;
public class LanguageChecker
{
// random number generator used by all functions
public static Random r = new Random();
public static String Blurbhelper(String blurb, int n)
{
if (n == 0)
{
return blurb;
}
else
{
return Blurbhelper(blurb + Whatzit(), n-1);
}
}
public static String Blurb()
{
String blurb = Whoozit();
int num = r.nextInt(11);
return Blurbhelper(blurb, num);
}
public static String WhoozitBegining(String x, int n)
{
if (n == 0)
{
return x;
}
else
{
return WhoozitBegining(x +"y", n-1);
}
}
public static String Whoozit()
{
String result = "x";
int num = r.nextInt(9) + 1;
return WhoozitBegining(result, num);
}
public static String Whatzithelper(String q, int n)
{
if (n == 0)
{
return q;
}
else
{
return Whatzithelper(q + (r.nextInt(2)==0 ? "z" : "d") + Whoozit(), n - 1);
}
}
public static String Whatzit()
{
String result = "q";
int num = r.nextInt(9) + 1;
return Whatzithelper(result, num);
}
// main method
public static void main(String[] args)
{
int n = 10;
while (n > 0)
{
System.out.println(Blurb());
n--;
}
}
}
I know I need to check that the first character is an x, if yes then I need to check and see if there are between 1 and 10 y's, and then check on the next characters but I am unsure of how to get this set up.
Explanation / Answer
import java.util.Random;
import java.util.Scanner;
public class LanguageChecker
{
// random number generator used by all functions
public static Random r = new Random();
public static String Blurbhelper(String blurb, int n)
{
if (n == 0)
{
return blurb;
}
else
{
return Blurbhelper(blurb + Whatzit(), n-1);
}
}
public static String Blurb()
{
String blurb = Whoozit();
int num = r.nextInt(11); // we need 0 to 10 Whatzits so changing range properly
return Blurbhelper(blurb, num);
}
public static String WhoozitBegining(String x, int n)
{
if (n == 0)
{
return x;
}
else
{
return WhoozitBegining(x +"y", n-1);
}
}
public static String Whoozit()
{
// A Whoozit is the character 'x'
String result = "x";
int num = r.nextInt(9) + 1; // we need one to 10 y's so changing this
return WhoozitBegining(result, num);
}
public static String Whatzit()
{
String result = "q";
result = result + (r.nextInt(2)==0 ? "z" : "d");
return result + Whoozit();
}
public static boolean isValidWhatzit(String x)
{
if ((x == null) || (x.length() < 4) || (x.length() > 13))
{
return false;
}
if (x.charAt(0) != 'q' || (x.charAt(1) != 'z' && x.charAt(1) != 'd'))
{
return false;
}
return isValidWhoozit(x.substring(2));
}
public static boolean isValidWhoozit(String x)
{
if((x == null) || (x.length() > 11) || (x.length() < 2) || x.charAt(0) != 'x')
{
return false;
}
for(int i = 1; i < x.length(); i++)
{
if(x.charAt(i) != 'y')
{
return false;
}
}
return true;
}
public static boolean isValidBlurb(String x)
{
if(x == null)
{
return false;
}
int qIndex = x.indexOf('q');
if (qIndex == -1)
{
return isValidWhoozit(x);
}
else
{
if(!isValidWhoozit(x.substring(0, qIndex)))
{
return false;
}
x = x.substring(qIndex);
String [] whatsits = x.split("(?=Q)");
for(int i = 0; i < whatsits.length; i++)
{
if (!isValidWhatzit(whatsits[i]))
{
return false;
}
}
}
return true;
}
// main method
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
while (true)
{
String input;
System.out.print("Enter a blurb string to validate: ");
input = sc.nextLine();
if (input.equalsIgnoreCase("done"))
{
break;
}
if (isValidBlurb(input))
{
System.out.println("The word is fine.");
}
else
{
System.out.println("The word is a mess!");
}
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.