Computer Science 202
Introduction to Programming

Fall 2012, The College of Saint Rose

Lab 3: Mad Libs
Due: 11:59 PM, Monday, September 24, 2012

In this lab, you will write a program to play something like the popular children's word game called "Mad Libs". A few minutes with the results from your favorite search engine will explain the concept for those unfamiliar.

We will start by going step-by-step to develop first a Visual Logic flowchart then a Java program to play a very simple and boring Mad Lib, then you will be on your own to create a more fun and interesting Mad Lib in a second Java program.

You may work alone or with a partner on this lab.

Getting Set Up

A Simple Mad Lib in Visual Logic

We will begin by creating a Visual Logic flowchart to play the following simple Mad Lib:

I never thought I'd see <positive whole number> <plural form of a kind of animal> in <a place name>!

The idea is that you ask for the type of information in each item inside the angle brackets without giving the context, and then print the (ideally hilarious) sentence that results from substituting in the responses.

So in this case, if someone enters 12 when asked for a <positive whole number>, "elephants" when asked for a <plural form of a kind of animal>, and "Albertus Hall" when asked for <a place name>, the hilarious sentence would be:

I never thought I'd see 12 elephants in Albertus Hall!

So your flowchart should create 3 input units that issue appropriate prompts and save the responses in appropriate variables. Then it should have a single output unit that prints the resulting sentence, including the values in the variables that were entered at the input units.

Try your flowchart with several different inputs. Once it works, save your flowchart in a file Simple.vls in your Lab3 folder.

Converting the Simple Mad Lib to Java

Next, we will convert our flowchart into an equivalent Java program. In your Lab3Progs project, create a new class called Simple.

Most of what you need to do will follow class examples. You'll need a Scanner to read input from the keyboard, you'll need to prompt for and read information from the Scanner and save the responses in variables, and then you'll need to print out the final sentence using the saved responses in their appropriate positions.

There is one thing you will probably want to use that we haven't seen yet in class examples. In some cases (like when I used "Albertus Hall" above), you want to read in an entire line of input as a String rather than a single word.

We previously used the Scanner's next method to read in a single word. For example, in the HelloYou example, we have the line:

    String name =;

If someone types in multiple words, the variable name will get only the first word typed in.

There is another Scanner method that will help us here. If instead we say

    String name = input.nextLine();

the variable name will get the entire line typed in at the keyboard (our Scanner in this case is called input), even if it includes multiple words (you might try this out by modifying the HelloYou example).

Unfortunately, there is one complication if we want to make use of the nextLine method in a program that also uses some of the other Scanner methods, in particular nextInt and nextDouble. If we try to use a nextLine right after, say, a nextInt, we will find that the nextLine call will return to us anything on the previous input line after the number returned by nextInt up to the new line, even if there is nothing there! While this is a perfectly reasonable behavior for the Scanner (it has no reason to believe that anything else you may have happened to type on the previous input should be ignored), it is pretty annoying for us in this case.

Fortunately, there is an easy solution. We can add an extra line to our program after each line that uses nextInt or nextDouble that will cause the Scanner to "consume" any remaining input on the same line as our numeric input. We can just add another nextLine call!

So anywhere in your program you have nextInt or nextDouble, you can add


(assuming the Scanner is named input) on the line immediately following any Scanner method call that does not consume the entire line. This will read in the rest of the line, but not store it in any variable. While this may seem strange, it is exactly what we want to do. Then when the next Scanner method is called, it will use the brand new line you type in in response to the next prompt.

A More Interesting and Fun Mad Lib

Now that you have the idea how to write a program like this, develop your own Mad Lib with more words or phrases to be filled in and write a second Java program to play the game. To get started with it, create a new class in your Lab3Progs project called Madlibs.

You can make your Mad Lib as long and complicated as you wish, but it must meet these minimum complexity requirements:

Bonus points will be available for especially creative or potentially funny Mad Libs.

Style and Documentation Reminders

Before you submit your programs, make sure they conform to our guidelines for style and documentation.

In particular, you should have a comment at the top of each class that describes your program and has your name (and that of your partner if you are working with someone), the course number and section (02 for 11:15, E1 for 4:10). You should have comments throughout your programs describing your variables and any non-obvious Java statements or groups of statements.

All identifiers (class names and variable names) should be meaningful and conform to Java's naming conventions.

Your code should be nicely formatted, with new lines after any { or }, and indented as done in class examples.

Submitting Your Work

Before 11:59 PM, Monday, September 24, 2012, submit your Visual Logic flowchart and your two Java programs to Blackboard for grading. Please upload your Visual Logic flowchart (Simple.vls) and your two Java source files ( and - upload the ones with the .java file extension, not the .class, .ctxt, package.bluej or README files).


This assignment is worth 35 points, which are distributed as follows:

> FeatureValueScore
Visual Logic flowchart correctness 6 correctness 5 correctness 5 at least 8 items 4 at least 5 different types 3 at least 2 numeric values 2
Using correct filenames 1
Comments 4
Naming conventions 3
Formatting 2
Creativity bonus up to 2
Total 35