Difference between revisions of "Mad Lib"

From CompSciWiki
Jump to: navigation, search
(not so good at java)
(Added quotations to student quotes)
 
(16 intermediate revisions by 6 users not shown)
Line 2: Line 2:
  
 
|ProblemName=Mad Lib
 
|ProblemName=Mad Lib
|Problem=Lets create a program which takes some text input from a user and then makes a Mad Lib out of it.
+
|Problem=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:
+
Consider making this solution with the following guidelines:
 
* [[JOptionPane Methods|JOptionPane]] with input and output.
 
* [[JOptionPane Methods|JOptionPane]] with input and output.
 
* Input with [[Strings]].
 
* Input with [[Strings]].
Line 9: Line 9:
  
 
The Mad Lib will follow this format:
 
The Mad Lib will follow this format:
<pre>"The ___________ in _______ like to ____ the ____."
+
{{OutputBlock
     plural noun    country        verb    noun</pre>
+
|Code=
|SolutionCode=<pre>
+
"The ___________ in _______ like to ____ the ____."
</pre>
+
     plural noun    country        verb    noun
 +
}}
  
 +
|SolutionCode=/*
 +
This is a program that uses the JOptionPane input dialogs to create a Mad Lib from some user input.
  
|SideSectionTitle=...by students
+
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
 +
}
 +
}
 +
 
 +
 
 +
|SideSectionTitle=...By Students
  
 
|SideSection=
 
|SideSection=
  
I'm not ashamed to admit that while testing my version of the Mad Lib program, I put the most vile, hateful and obscene curse words I was willing to commit to text into my sample fields. Try it yourself!
+
"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!"
 +
 
 +
"Testing is of course important. It's the only way to figure out if your program works. But, you'll soon find that testing your programs can be a bore. With a little bit of creativity it doesn't have to be so boring. Try to entertain yourself. Think of new and exciting ways to break your program with some input. Believe it or not, every time you manage to break your program gives you the opportunity to make it better."
  
 
|Solution=
 
|Solution=
==The Basics==
+
===The Basics===
Start by creating the Class. We're going to call our class "MadLib".
+
Start by creating the Class. Name the class "MadLib".
<pre>public class MadLib
+
{{CodeBlock
 +
|Code=
 +
public class MadLib
 
{
 
{
}</pre>
+
}
 +
}}
  
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.
+
Next, make the main method. Put the main method between the curly-braces of the MadLib class.
<pre>public static void main(String [] args)
+
{{CodeBlock
 +
|Code=
 +
public static void main(String [] args)
 
{
 
{
}</pre>
+
}
 +
}}
 +
 
 
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.
 
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.
+
With these bare basics, the program will compile and run. Keep in mind, running doesn't necessarily mean doing any work.  
 +
 
 +
===Make a plan===
 +
This would be a good time to plan out how the program will run. Do this by adding some inline [[Comments]] to the code. This will help remember what we intend to do and when we're finished, will improve the readability of our code later.
  
==Make a plan==
+
Using comments outline the overall steps to the solution:
Using comments lets outline the overall steps to our application:
+
* Create the variables
* Create our variables
+
 
* Get the user's input
 
* Get the user's input
 
* Show output (the completed Mad Lib)
 
* Show output (the completed Mad Lib)
<pre>public static void main(String []args)
+
{{CodeBlock
 +
|Code=
 +
public static void main(String []args)
 
{
 
{
 
// Create variables
 
// Create variables
Line 50: Line 97:
 
 
 
// End of program
 
// End of program
}</pre>
+
}
 +
}}
  
==Declare the variables==
+
===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:
+
Store the words for the Mad Lib in strings. To do that, declare some strings and give the strings some logical names.
* a plural noun
+
Using the [[Variables_and_Literals#Naming_Guidelines|naming standards]], name each variable something meaningful and distict.
* a country
+
{{CodeBlock
* a verb
+
|Code=
* a noun
+
String pluralNoun;
Using our [[Variables_and_Literals#Naming_Guidelines|naming standards]] we'll name each variable something meaningful and easy to differentiate from one-another.
+
<pre>String pluralNoun;
+
 
String country;
 
String country;
 
String verb;
 
String verb;
String noun;</pre>
+
String noun;
 +
}}
  
==Getting user input==
+
===Getting user input===
Next, we need to get some input from the user. We're going to use the [[JOptionPane Methods|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.
+
Next, get some input from the user. Use the [[JOptionPane Methods|JOptionPane]] library so the application can use Input Dialogs. In this example, 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.
+
At the top of the MadLib file, outside of the MadLib class, import the JOptionPane library
<pre>pluralNoun = JOptionPane.showInputDialog("Please enter a plural noun:");</pre>
+
{{CodeBlock
What we're saying here is:
+
|Code=
* We're referencing the JOptionPane library and specifically the showInputDialog method.
+
import javax.swing.JOptionPane;
 +
}}
 +
 
 +
Getting input from the showInputDialog is easy. It will automatically construct a pop-up 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.
 +
{{CodeBlock
 +
|Code=
 +
pluralNoun = JOptionPane.showInputDialog(null, "Please enter a plural noun:");
 +
}}
 +
What the code is saying here:
 +
* Reference the JOptionPane library and within it the showInputDialog method.
 
* The text from the text field will be stored in the variable pluralNoun which we created earler.
 
* 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:"
 
* 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  
+
Do the same thing three more times for the other strings.
 
+
==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.
+
<pre>JOptionPane.showMessageDialog("The " + pluralNoun + " in " + country + " like to " + verb + " the " + noun + ".");</pre>
+
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!==
+
===Showing output===
Finally lets compile, run and test the Mad Lib program.
+
Similar to the way the showInputDialog is setup, use JOptionPane's showMessageDialog method to show the user the Mad Lib.
 +
{{CodeBlock
 +
|Code=
 +
JOptionPane.showMessageDialog(null, "The " + pluralNoun + " in " + country + " like to " + verb + " the " + noun + ".");
 
}}
 
}}
 +
Note the usage of a plus (+) to combine several strings into one. Remember to account for spaces between each of the parts of the phrase and the string variables.
  
==Code==
+
===We're done!===
Here be a place holder.
+
Finally compile, run and test the Mad Lib program.
<pre>/*
+
}}
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
+
}
+
}</pre>
+

Latest revision as of 15:14, 8 December 2011

Back to the Program-A-Day homepage

Problem

Create a program which takes some text input from a user and then makes a Mad Lib out of it. Consider making this solution with the following guidelines:

The Mad Lib will follow this format:

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

...By Students

"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!"

"Testing is of course important. It's the only way to figure out if your program works. But, you'll soon find that testing your programs can be a bore. With a little bit of creativity it doesn't have to be so boring. Try to entertain yourself. Think of new and exciting ways to break your program with some input. Believe it or not, every time you manage to break your program gives you the opportunity to make it better."

Solution

The Basics

Start by creating the Class. Name the class "MadLib".

 public class MadLib
{
} 

Next, make the main method. Put the main method between the curly-braces of the MadLib class.

 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.

With these bare basics, the program will compile and run. Keep in mind, running doesn't necessarily mean doing any work.

Make a plan

This would be a good time to plan out how the program will run. Do this by adding some inline Comments to the code. This will help remember what we intend to do and when we're finished, will improve the readability of our code later.

Using comments outline the overall steps to the solution:

  • Create the 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

Store the words for the Mad Lib in strings. To do that, declare some strings and give the strings some logical names. Using the naming standards, name each variable something meaningful and distict.

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

Getting user input

Next, get some input from the user. Use the JOptionPane library so the application can use Input Dialogs. In this example, use two methods from JOptionPane: showInputDialog and showMessageDialog.

At the top of the MadLib file, outside of the MadLib class, import the JOptionPane library

 import javax.swing.JOptionPane; 

Getting input from the showInputDialog is easy. It will automatically construct a pop-up 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(null, "Please enter a plural noun:"); 

What the code is saying here:

  • Reference the JOptionPane library and within it 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:"

Do the same thing three more times for the other strings.

Showing output

Similar to the way the showInputDialog is setup, use JOptionPane's showMessageDialog method to show the user the Mad Lib.

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

Note the usage of a plus (+) to combine several 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