Complete the implementation of BlurbGenerator.java, which is called from Blurbs.java. The incomplete methods are makeWhoozit() makeYString() makeWhatzit() Submit BlurbGenerator.java. Blurbs.java /** * Solution to Programming Project 17.5 * * @author Java Foundations */ import java.util.Scanner; public class Blurbs { /** * Generates a series of Blurbs (a word in an alien language). */ public static void main(String args[]) { BlurbGenerator blurbMaker = new BlurbGenerator(); Scanner scan = new Scanner(System.in); System.out.println("How many blurbs would you like? "); int numBlurbs = scan.nextInt(); for (int i = 1; i <= numBlurbs; i++) { System.out.println("Blurb #" + i + ": " + blurbMaker.makeBlurb()); } } } BlurbGenerator.java import java.util.Random; /** * In the language of an alien race, all words take the form of Blurbs. * A Blurb is a Whoozit followed by one or more Whatzits. * A Whoozit is the character 'x' followed by zero or more 'y's. * A Whatzit is a 'q' followed by either a 'z' or a 'd', followed * by a Whoozit. * * Solution to Programming Project 17.5 * * @author Java Foundations */ public class BlurbGenerator { private Random gen; /** * Instantiates a random number generator needed for blurb creation. */ public BlurbGenerator() { gen = new Random(); } /** * Generates and returns a random Blurb. A Blurb is a Whoozit * followed by one or more Whatzits. */ public String makeBlurb() { return makeWhoozit() + makeMultiWhatzits(); } /** * Generates a random Whoozit. A Whoozit is the character 'x' * followed by zero or more 'y's. */ private String makeWhoozit() { System.out.println("makeWhoozit() unimplemented."); String whoozit = ""; return whoozit; } /** * Recursively generates a string of zero or more 'y's. */ private String makeYString() { System.out.println("makeYString() unimplemented."); String yString = ""; return yString; } /** * Recursively generates a string of one or more Whatzits. */ private String makeMultiWhatzits() { String whatzits = makeWhatzit(); if (gen.nextBoolean()) { whatzits += makeMultiWhatzits(); } return whatzits; } /** * Generates a random Whatzit. A Whatzit is a 'q' followed by * either a 'z' or a 'd', followed by a Whoozit. */ private String makeWhatzit() { System.out.println("makeWhatzit() unimplemented."); String whatzit = ""; return whatzit; } } // class BlurbGenerator
Complete the implementation of BlurbGenerator.java, which is called from Blurbs.java. The incomplete methods are
-
- makeWhoozit()
- makeYString()
- makeWhatzit()
Submit BlurbGenerator.java.
Blurbs.java
import java.util.Random;
/**
* In the language of an alien race, all words take the form of Blurbs.
* A Blurb is a Whoozit followed by one or more Whatzits.
* A Whoozit is the character 'x' followed by zero or more 'y's.
* A Whatzit is a 'q' followed by either a 'z' or a 'd', followed
* by a Whoozit.
*
* Solution to Programming Project 17.5
*
* @author Java Foundations
*/
public class BlurbGenerator {
private Random gen;
/**
* Instantiates a random number generator needed for blurb creation.
*/
public BlurbGenerator() {
gen = new Random();
}
/**
* Generates and returns a random Blurb. A Blurb is a Whoozit
* followed by one or more Whatzits.
*/
public String makeBlurb() {
return makeWhoozit() + makeMultiWhatzits();
}
/**
* Generates a random Whoozit. A Whoozit is the character 'x'
* followed by zero or more 'y's.
*/
private String makeWhoozit() {
System.out.println("makeWhoozit() unimplemented.");
String whoozit = "";
return whoozit;
}
/**
* Recursively generates a string of zero or more 'y's.
*/
private String makeYString()
{
System.out.println("makeYString() unimplemented.");
String yString = "";
return yString;
}
/**
* Recursively generates a string of one or more Whatzits.
*/
private String makeMultiWhatzits() {
String whatzits = makeWhatzit();
if (gen.nextBoolean()) {
whatzits += makeMultiWhatzits();
}
return whatzits;
}
/**
* Generates a random Whatzit. A Whatzit is a 'q' followed by
* either a 'z' or a 'd', followed by a Whoozit.
*/
private String makeWhatzit() {
System.out.println("makeWhatzit() unimplemented.");
String whatzit = "";
return whatzit;
}
} // class BlurbGenerator
Algorithm:
- The
BlurbGenerator
class initializes aRandom
generator. - The
makeBlurb()
method returns a randomWhoozit
followed by one or moreWhatzit
generated by themakeMultiWhatzits()
method. - The
makeWhoozit()
method returns the string "x" followed by zero or more "y" generated by themakeYString()
method. - The
makeYString()
method recursively generates a string of zero or more "y". If a random boolean value is true, it adds a "y" to the string and calls itself again. - The
makeWhatzit()
method generates aWhatzit
which starts with the string "q" and appends either "z" or "d" depending on a random boolean value. It then callsmakeWhoozit()
to generate the rest of theWhatzit
. - The
makeMultiWhatzits()
method generates a string of one or moreWhatzit
usingmakeWhatzit()
. If a random boolean value is true, it appends the result of a recursive call to itself. It returns the concatenated string of all theWhatzit
generated.
Trending now
This is a popular solution!
Step by step
Solved in 4 steps with 3 images