CIT 591 Assignment 9: General-Purpose Text Translator
Fall 2008, David Matuszek

Purposes of this assignment:

General idea of the assignment:

I often have need to perform some simple translations on text--for example, copying text from a PowerPoint slide and getting rid of all the vertical tab characters. If I need to modify text in this way just once in a while, I can do it manually in a text editor; but if I have to do it frequently, I want a tool that does it for me. This assignment is to create just such a tool.


screenshot of GUI

Create a GUI that looks like this picture. (I have used a small window; your text areas should be larger.)

The menu items under the File menu are:

Uses a JFileChooser to request a text file from the user, and displays the contents of this file in the upper text area.
Save As...
Uses a JFileChooser to ask the user where to save the file, then saves the contents of the lower text area in this location.
Terminates the program. (Closing the window will also terminate the program.)

The menu items under the Translate menu are:

Makes no changes to the input. This is useful for debugging the GUI, but also provides a model that you can look at the next time you want to add a translator.
Wrap Lines
Wraps lines so that each line is no longer than 72 characters. Long lines should be broken at a space character, with the remainder put on a new line with the same indentation. Very long lines will result in several shorter lines. If a line contains more than 72 consecutive non-space characters (as often happens with a URL), the long "word" is not broken, but is put on a line by itself.
Fix Indentation
Adjusts the indentation according to Java rules. The first line has zero indentation (leading spaces are removed, if necessary). Indentation for subsequent lines is increased by four spaces for each open brace, '{', encountered, and decreased by four spaces for each close brace, '}'. Braces are not moved from one line to another; only indentation is adjusted.

It is possible for the indentation count to become negative. Lines with "negative indentation" should not, of course, be indented, but the count should be maintained so that subsequent lines can be indented correctly.
Zap Gremlins
Deletes "bad" characters. "Good" characters are the printable ASCII characters, the space character, the newline character '\n', the carriage return character '\r', and the tab character '\t'.

Both the upper and the lower text areas are fully editable. When the user clicks the Translate button or chooses an translation from the Translate menu, the specified translation is applied to the text in the upper area, and the result replaces any previous text in the lower area.

Programming details:

Name your project Translator, your package translator, and your "main" class Translator.

Create an interface TranslatorInterface that declares the following methods:

String getName();
Returns the string to be used as a menu item.
String getDescription();
Returns a description of what this translator does.
String translate(String text);
Translates the input to the output. Note that the input is a single string, probably containing newlines, that represents the entire input, and the output is the same. Often this is more convenient. If it is easier for some methods to work with an array of lines, this can be done with
     String[] inputLines = text.split("\n");

Each translator class you write should implement TranslatorInterface. All translator classes you write (yes, even including the identity translator!) should be fully JUnit tested. Include an test suite.

Your main class should have a method addTranslateItem(TranslatorInterface translator) that does all the work of adding a new translator to the GUI. That is, it creates a menu item with the correct name, adds it to the Translate menu, and adds a listener for this menu item that calls the new translator. In addition, the name of the translator is displayed in the GUI title bar, and the description of the translator is displayed just below the title bar (see the above picture for an example). In other words, you should be able to install a new translator to your program by adding a single line to your Translator class, for instance,

     addTranslateItem(new ZapGremlinsTranslator());

This is how all of the above translators should be installed in your Translator class; they should not be "hard-wired" in.

Remember that the purpose of doing assignments is to learn, not to produce the required program. If you did not do the GUI for the previous program, you should do it for this program. If you did not to JUnit testing for the previous programs, you should do some of the methods that require it for this program.


All the usual style rules apply. Good Javadoc comments, JUnit tests for all computations, proper formatting and indentation, methods that fit on a single screen, good method and variable names, etc., etc. Use the names and method signatures specified, etc.

Due date:

Before midnight, Thursday, November 13. Submit to Blackboard one copy of your project directory, properly zipped. Both your names should be in Javadoc comments in the classes. In addition, whichever of you turns the program in, should put your partner's name in the comments field in Blackboard.