Q1: [JF edited] In the language of an alien race, all words take the form of Blu
ID: 3818836 • 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.)
Below is the code I have so far:
import java.util.*;
public class LanguageChecker
{
// random number generator used by all functions
public static Random r = new Random();
public static String Blurbhelper(String Whoozit, int n)
{
if (n == 0)
{
return Whoozit();
}
else
{
return Blurbhelper(Whoozit() + Whatzit(), n-1);
}
}
public static String Blurb()
{
String blurb = Whoozit();
int num = r.nextInt(9);
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);
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);
return Whatzithelper(result, num);
}
// main method
public static void main(String[] args)
{
int n = 10;
while (n > 0)
{
System.out.println(Blurb());
n--;
}
}
}
I can't seem to get it to include the Whatzit when I print out Blurbs. Below is a sample of the output I keep getting:
xyyyyy
xyyyyy
xyyyyyyyy
xyy
xyyyyyyyy
xy
xyyy
xyyyyyy
xyyyyyy
xyyyyyyyy
I can do this easily with loops but I need the methods to be RECURSIVE, not loops.
Explanation / Answer
Language Checker
import java.util.Random;
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();
}
// main method
public static void main(String[] args)
{
int n = 10;
while (n > 0)
{
System.out.println(Blurb());
n--;
}
}
}
Sample execution
xyqdxyyyyyyyyqzxyyyyyyyqdxyyyyyyyyyqzxyyqzxyyyyyyyy
xyyyyqdxyyyyqzxyyyyyqzxyyyyyyyyyqzxyyyyqdxyyqzxyyyqzxyyyyyyyyqzxyyqzxyyyyyyyy
xyyyyyyyyqzxyyyyqdxyyqdxyyyyyqdxyyyyyyyyqzxyyqdxyyyyyqzxyyqdxyyyyyyyyqzxyyyqzxyyyyy
xyyyyqzxyyyqdxyyqdxyyyyyqdxyyyyy
xyyqdxyyyqzxyyyyyyyyqdxyyyyyyyyyqdxyyyyqdxyyyyqdxyyqzxyyyyyyqdxyyyyyyqzxyyyyqzxyyy
xyyyyyyyqdxyyqdxyyqzxyyyyyyqdxyyyyyyyy
xyyyyqzxyqzxyyyyyyqdxyyyyyyqzxyqdxyyyyy
xyqzxyyqdxyqzxyyyyyyyqzxyyqdxyyyyyyyyyqzxyqdxyyyqzxyyqdxyy
xyyy // 0 whatzits
xyyyyqdxyqzxyyqdxyyyyyyyyyqzxyyyyyyqdxyqdxyyyyyqdxyyyyyyyy
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.