Fund. CS II (CS152 2006S)

Homework 4: Mad-Libs, Revisited

Assigned: Wednesday, February 1, 2006
Due: 8:00 a.m., Monday, February 6, 2006

Summary: In this assignment, you will extend your simple version of the game Mad Libs.

Purpose: To give you an opportunity to explore issues of input, output, and strings in Java.



In a previous assignment you built a simple game of Mad Libs. However, that game required that you hard-code the Mad Lib template. It would be nicer (and more general) to have your program read the template from a file. That's what you'll do in this assignment.

The Assignment

Write a program the serves the role of the questioner in Mad Libs. Your program should

You can assume that the template file contains exactly four lines, that each line contains exactly one thing to fill in, and that the thing to fill in is surrounded by underscores.

I've written a template that matches these specifications for the sample from the previous assignment and another template.

A Sample Session

Please enter the name of the template file: johnjava.txt
Please enter the name of the result file: jj1.txt
Please enter a(n) verb: walk
Please enter a(n) adjective: superfluous
Please enter a(n) adjective: incredible
Please enter a(n) exclamation: wicked

Here is your story ...

  John likes to walk with Java 
  because he thinks that Java is superfluous.
  When things go well, John is incredible.
  When things go badly, John yells "wicked!"


In the lab on input and output, you practiced reading from and writing to files. You should therefore be able to prompt for names of files and read and write from them. Note that you will need two BufferedReader objects (one for reading the user responses and one for reading from the file) and two PrintWriter objects (one for printing prompts to the user and one for printing to the result file).

You are likely to need at least four string variables, one for each line of input.

In the lab on strings, you wrote a program that segmented an input line by identifying the location of a separator (in that case, a colon). You can use a similar technique here.

Submitting Your Work

When you are satisfied with your work, you should submit it to me. Email me the program.


When you ask questions, I'll try to put the answers here.



Wednesday, 1 February 2006 [Samuel A. Rebelsky]


Disclaimer: I usually create these pages on the fly, which means that I rarely proofread them and they may contain bad grammar and incorrect details. It also means that I tend to update them regularly (see the history for more details). Feel free to contact me with any suggestions for changes.

This document was generated by Siteweaver on Tue May 9 08:31:07 2006.
The source to the document was last modified on Fri Feb 3 09:48:39 2006.
This document may be found at

You may wish to validate this document's HTML ; Valid CSS! ; Check with Bobby

Samuel A. Rebelsky,