-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBlurbGenerator.java
88 lines (75 loc) · 2.03 KB
/
BlurbGenerator.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/*
* **********************************************
* San Francisco State University
* CSC 220 - Data Structures
* File Name: BlurbGenerator.java
* Author: Java Foundation
* Author: Duc Ta
* Author: Bryan Khor
* **********************************************
*/
import java.util.Random;
public class BlurbGenerator {
private Random random;
/**
* Instantiates a random number generator needed for blurb creation.
*/
public BlurbGenerator() {
this.random = 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(random.nextInt(4) + 1);
}
/**
* Generates a random Whoozit. A Whoozit is the character 'x' followed by
* zero or more 'y's.
*/
private String makeWhoozit() {
return "x" + makeYString(random.nextInt(4));
}
/**
* Recursively generates a string of zero or more 'y's.
*/
private String makeYString(int count) {
if (count != 0) {
return makeYString(count - 1) + "y";
}
return "";
}
/**
* Recursively generates a string of one or more Whatzits.
*/
private String makeMultiWhatzits(int count) {
String multi = "";
if (count == 1) {
return makeWhatzit();
} else {
multi += makeMultiWhatzits(count - 1) + makeWhatzit();
}
return multi;
}
/**
* Generates a random Whatzit. A Whatzit is a 'q' followed by either a 'z'
* or a 'd', followed by a Whoozit.
*/
private String makeWhatzit() {
String ZorD = "";
int random = (int) (Math.random() * 2);
if (random == 0) {
ZorD = "z";
} else {
ZorD = "d";
}
return "q" + ZorD + makeWhoozit();
}
public Random getRandom() {
return random;
}
public void setRandom(Random random) {
this.random = random;
}
}