16 points

Have you ever done a MadLib? I'll briefly explain what a MadLib is. One person writes or finds a couple of paragraphs and crosses out various words from the text. She then asks the other person to supply the missing words: "Give me a noun," "Give me a present-tense verb," and so on. When all the missing words have been supplied, she reads back the paragraphs using the words that the other person has supplied. Sometimes it's funny.

To write the MadLib program, you can use JCreator. To get started, you can download the files below under 'A Starting Point'.


Here is what my MadLib looks like before the user supplies the missing words:

Here is what the MadLib looks llike after I enter the missing words:

Here is the window that pops up after I enter the missing words and hit the button at the bottom:

Note: You are asked to write your own MadLib, not to re-create mine.

Note #2: See the 'Demo' section below to get an idea of how the program runs.


The madLib class

The class 'madLib' can have this basic structure:

Private variables
You may need these private variables: an int 'arraySize' that stores how many words you ask the user for (at least 10), a Button, a TextArea and a String 'finalStr'. You also need three arrays: an array 'labels' of type Label to store your requests for words, a TextField array 'words' to store the user's responses to your requests, and a String array 'str' to store the parts of the paragraph that you don't tell the user about. Here's how I set up one of my arrays:
     private int arraySize = 16;
      private String[] str = new String[arraySize];

Constructor 'madLib()'
In 'madLib' set a title for the frame. In a for loop that runs from 1 to 'arraySize,' add each TextField to the frame. The command inside the loop might look like:
     words[i] = addTextField("",i,2,1,1);
Add each Label to the frame. Here is the line that defines the first Label in my version:
     labels[1] = addLabel ("Give me an adjective (ex: wonderful): ",1,1,1,1);
Add a Button to the frame, which when clicked causes the complete MadLib to be assembled and displayed. I wrote:
     showMadLibButton = addButton ("OK I'm done, now show me the complete MadLib.",16,1,2,2);
The first of the 4 numbers indicates in which row the Button should appear.

buttonClicked(Button buttonObj)
The method 'buttonClicked' should call the method 'setUpFullMadLib()' which assembles the complete MadLib. If you write the (optional) method 'addCarriageReturns(),' you can call it next. Finally, 'buttonClicked' needs 3 lines similar to the contents of 'main(),' the major differences being that 1) 'buttonClicked' creates a frame of type 'madLibResult', and 2) when you call the constructor 'madLibResult()' send to it the String 'finalStr' as a parameter. This new frame will display the final MadLib.

I used 'setUpFullMadLib' to store the parts of the MadLib that the user doesn't see. I assigned a series of strings to the array 'str'. Here is the first assignment:
     str[1] = "\nMUSIC\n\nThe music people like is so individual. One man's Meatloaf is another man's Poison. Music that to me is ";
The words that the user doesn't see are stored in 'str'. The words that the user supplies are stored in 'words'. It is necessary to knit 'str' and 'words' together to create sentences that make sense. You use the method 'getText()' to extract a String from a TextField. I stored the final result in the String 'finalStr'. Here is my first line in this effort:
     finalStr = str[1] + words[1].getText() + str[2] + words[2].getText() + " " + words[3].getText();
You can use the syntax 'finalStr += ...' on subsequent lines to append text to 'finalStr'.

addCarriageReturns() [optional, 3 extra-credit points]
If you have not yet added carriage returns ("\n") to the text you store in the array 'str,' you run the risk of having the entire MadLib display on one line. 'addCarriageReturns' adds "\n" characters to the String 'finalStr' so that the MadLib displays on several lines, and it assures that all the lines are about the same length. You may want to set up a 'while' loop that runs from 0 to 'finalStr.length()'. Inside the loop maintain an integer 'counter' that records how many characters are in the current line. If 'counter' exceeds some value (say, 60) and the current character in 'finalStr' is a space, then add a "\n" to 'finalStr'. You can use the 'substring' method to access the current character in 'finalStr':
     if (finalStr.substring(i,i + 1).equals(" ") && counter > length)
I can give you further hints on writing this method.
Inside the 'while' loop, you should also check if the current character in 'finalStr' is "\n" and if it is, set 'counter' to 0. This prevents you from printing lines that are much shorter than the others.

Note: If you don't write 'addCarriageReturns(),' you will need to add carriage returns ("\n") to the text you store in the array 'str' so that the MadLib is displayed on several lines.      

main(String[] args)
main is written for you. The first line causes an object of type 'madLib' (a frame) to be created, and the next lines determine its size and make it visible.


The madLibResult class

The (smaller) class 'madLibResult' can have this basic structure:

Private variables
You will need to define a String 'finalStr' and a TextArea 'output'.

Constructor 'madLibResult(String str)'
The constructor is written for you.



Here is Javadoc documentation that lists the methods you are responsible for.



You can download and run CompletedMadLib.jar to get an idea of how your program will run when it is complete.


A Starting Point

You can download and as a starting point in writing your program.