Mad Lib

From CompSciWiki
Revision as of 12:17, 6 April 2010 by KarelK (Talk | contribs)

Jump to: navigation, search

Back to the Program-A-Day homepage

Problem

Lets create a program which takes some text input from a user and then makes a Mad Lib out of it. We're going to look at making this solution with the following:

The Mad Lib will follow this format:

"The ___________ in _______ like to ____ the ____."
     plural noun    country         verb     noun
 

Input & Output

I'm not ashamed to admit that while testing my version of the Mad Lib program, I put some of my favorite curse words I was willing to commit to text into my sample fields. Try it yourself!

Solution

The Basics

Start by creating the Class. We're going to call our class "MadLib".

public class MadLib
{
}

Next, we can make our main method. Put the main method between the curly-braces of the MadLib class. When we run our finished program, whatever code we write into the main method will run.

public static void main(String [] args)
{
}

For now it's not important to know just what each word means when writing the main method, but it must look like the code above in order to work properly.

Now that we have the bare basics for our program to compile and run. Keep in mind, running doesn't necissarily mean doing any work. This would be a good time to plan out what we intend to do with our program. We'll do this by adding some inline Comments to the code. This will help us remeber what we intend to do and when we're finished, will improve the readability of our code later.

Make a plan

Using comments lets outline the overall steps to our application:

  • Create our variables
  • Get the user's input
  • Show output (the completed Mad Lib)
public static void main(String []args)
{
	// Create variables

	// Get input
	
	// Show output
	
	// End of program
}

Declare the variables

We want to store our words for the Mad Lib in strings. To do that, let's declare some strings. We need to give the strings some logical names. Remeber we need one string for each of the following:

  • a plural noun
  • a country
  • a verb
  • a noun

Using our naming standards we'll name each variable something meaningful and easy to differentiate from one-another.

String pluralNoun;
String country;
String verb;
String noun;

Getting user input

Next, we need to get some input from the user. We're going to use the JOptionPane library so we can use Input Dialogs to get user input. In this example, we're going to use two methods from JOptionPane: showInputDialog and showMessageDialog.

Getting input from the showInputDialog is easy. It will automatically construct a popup dialog with a text field. Also, whatever text is put into the text field will be returned as a string. All you need to do is pass showInputDialog the text you intend to show the user. Lets use showInputDialog to get a plural noun from the user.

pluralNoun = JOptionPane.showInputDialog("Please enter a plural noun:");

What we're saying here is:

  • We're referencing the JOptionPane library and specifically the showInputDialog method.
  • The text from the text field will be stored in the variable pluralNoun which we created earler.
  • The text to appear in the dialog box will be "Please enter a plural noun:"

Lets do the same thing three more times for the other

Showing output

Similar to the way we set up the showInputDialog, we'll use JOptionPane's showMessageDialog method to show the user the Mad Lib we've created. Since showMessageDialog doesn't output anything, we can just call it and tell it what text to show.

JOptionPane.showMessageDialog("The " + pluralNoun + " in " + country + " like to " + verb + " the " + noun + ".");

Note how we can use a plus (+) to combine strings into one. Remember to account for spaces between each of the parts of the phrase and the string variables.

We're done!

Finally compile, run and test the Mad Lib program.

Code

Solution Code

Back to the Program-A-Day homepage


Code

Here be a place holder.

/*
This is a program that uses the JOptionPane input dialogs to create a Mad Lib from some user input.

The Mad Lib layout:
     "The ___________ in _______ like to ____ the ____."
          plural noun    country         verb     noun
*/

import javax.swing.JOptionPane;

public class MadLib 
{
	public static void main(String []args)
	{
		// Create variables
		String pluralNoun;
		String country;
		String verb;
		String noun;
		
		// Get input
		pluralNoun = JOptionPane.showInputDialog("Please enter a plural noun:");
		country = JOptionPane.showInputDialog("Please enter a name of a country:");
		verb = JOptionPane.showInputDialog("Please enter a verb:");
		noun = JOptionPane.showInputDialog("Please enter a noun:");
		
		// Show output
		JOptionPane.showMessageDialog(null, "The " + pluralNoun + " in " + country + " like to " + 
			verb + " the " + noun + ".");
		// End of program
	}
}