Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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 a loop but I need the methods to be recursive, not loops.

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

Step: 3

blur-text-image

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

Database Design Using Entity Relationship Diagrams

Authors: Sikha Saha Bagui, Richard Walsh Earp

3rd Edition

103201718X, 978-1032017181

More Books

Students also viewed these Databases questions