Survey
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
C ORNELL U NIVERSITY CS 211, S PRING 1998 Assignment 1: Justify My Text Due Date: 10:10 am, Thursday, 12 February, 1998 in lecture Note: For this assignment, you may work with one partner. Please hand in only one copy of the assignment. It should contain: Both of your names and CUIDs. The name of your recitation instructor, the section number, and the meeting time. The assignment title, and the date. Introduction The purpose of this assignment is to give you some experience in dealing with Java and object-oriented programming. The full program implements a simple GUI (graphical user interface) that sits on top of a text justification tool. We have already written the GUI and laid out the structure of the justification tool. Your job will be to complete the justification engine. Justification and text formatting To make text look more pleasing to the eye, different formatting conventions may be used. The term “justification” means making the characters line up underneath each other: left justification means the lines all start at the same column, and right justification means that they end in the same column. This document is, for the most part, both right and left justified. In this assignment, we will assume the use of a monospaced font, such as a member of the Courier font family. In a monospaced font, each character takes up the same width (i.e., one column), therefore “mmmm” takes up the same space as “llll”. By contrast, with a proportional font, characters take up only as much space as they aesthetically need, so “mmmm” takes up more space than “llll”. Proportional fonts, such as those of the TimesRoman or Helvetica font families, are considered to be easier to read than monospaced fonts in most uses.1 Monospaced fonts are, however, commonly used to display program source code; things seem to “line up” better. Text formatting and justification with a proportional font is fairly tricky; it involves taking real measurements of space as well as use of aesthetics and experience. Luckily for us, text formatting with a monospaced font (what we will be doing) is simpler, as it involves only the judicious insertion of extra spaces (i.e., the single space character “ ”) here and there. 1 This document is typeset (mostly) in the ComputerModern font family, which is similar to TimesRoman. A SSGT 1: J USTIFY M Y T EXT Page 1 of 6 C ORNELL U NIVERSITY CS 211, S PRING 1998 Formatting parameters The GUI of our formatting tool is shown below2: The user is asked to specify four items: 1. The maximum length of each line, call it MAX. 2. The formatting style (more on this below). 3. The file to take input from. 4. The file to write the output to (cannot be the same as the input file for basic sanity). We will give you the source code for the GUI shown above. It will handle all of the annoying details such as figuring out when the input has been entered and what the name of the input and output files should be. Your job will be to finish up the formatting engine. We will consider the input file to be just a sequence of character strings (the fancy computer science term is “token”). We don’t care how much “white space” occurs between tokens as the white space will just be ignored. Part of your job in this assignment is to implement the doFormatting method of the class Formatter. Basically, what you are to do is this: Build up the next line of output by reading in one token at a time until the next token would put you over the maximum line length (don’t throw out that token as you will need it to start the next line). As you read in each token, don’t forget to add a single space between words. The current line is now at most MAX chars long. Apply the appropriate formatting to the current line (inserting spaces as needed) and then print it to the output file. 2 This is how it looks under Unix; it will look a bit different under MS-Windows and MacOS. In particular, the Unix version incorrectly set the title of the window to “untitled”. This is a bug in Sun’s Java compiler for Unix, it seems to work correctly on other platforms. A SSGT 1: J USTIFY M Y T EXT Page 2 of 6 C ORNELL U NIVERSITY CS 211, S PRING 1998 Build up the next line, then format it and print it to the output file. Continue until there is no more input. Our formatting styles Our formatting engine is to support four styles. We have done the first already; you will have to create classes that implement the last three: 1. Ragged Right — Each line should be left justified, but not right justified. Do not insert any extra spaces in between the words; let the line end wherever it ends (up to a maximum line length, after which you should start a new line). This is a trivial style to implement, as no work needs to be done. You just build up the next line (up to the specified maximum length) and print it! Look at the class RaggedRightFormatter; it is already completed. 2. Centred — Neither left nor right justified. Do not insert any spaces between words, but add enough leading spaces to the current line so that it appears to be centred.3 3. Smooth — (The term is our invention.) Both right and left justified. Figure out how many extra spaces must be inserted and insert them all before the last word in the line (if there is only one word in the line, don’t do anything). This is a rather unsophisticated way of doing right and left justification. 4. Very Smooth — (The term is also our invention.) Both right and left justified. Figure out how many extra spaces must be inserted and make the distribution as even as possible within the line. For example, if there are six words in the line (and therefore five inter-word gaps) and if there are 12 spaces to insert, then insert three extra spaces in two of gaps and two extra spaces in three of the gaps. It doesn’t matter which gaps get two extra spaces and which get three as long as it all works out in the end. This is the trickiest part of the assignment; you can decide how to implement it, but take the time to get it correct. An example Let’s do an example do show the differences in the various styles. Suppose we use a max line length of 25 and use the following input file: 1234567890123456789012345 If we shadows have offended, Think but this, and all is mended That you have but slumb’red here, While these visions did appear. -- Wm. Shakespeare Recall that we will be reading in the input one “token” at a time, effectively throwing away all of the initial spacing. As long as the same tokens are in the same order and there is at least one space or line separator 3 No, that’s not a typo. Being Canadian, I use Canadian spelling. I’m too set in my ways to change. James Gosling (the guy most responsible for Java) is Canadian too, so I’m sure he’d approve :-) A SSGT 1: J USTIFY M Y T EXT Page 3 of 6 C ORNELL U NIVERSITY CS 211, S PRING 1998 between tokens, it wouldn’t have mattered if we had put each token on a line by itself or put the whole file on one long line, the output would be the same. Note that the "123..." is a real token in the text file. We include it in the input file to make the output style differences more obvious to you. Here is the output of the various formatting styles: Ragged right: 1234567890123456789012345 If we shadows have offended, Think but this, and all is mended That you have but slumb’red here, While these visions did appear. -- Wm. Shakespeare Smooth: 1234567890123456789012345 If we shadows have offended, Think but this, and all is mended That you have but slumb’red here, While these visions did appear. -Wm. Shakespeare Centred: 1234567890123456789012345 If we shadows have offended, Think but this, and all is mended That you have but slumb’red here, While these visions did appear. -- Wm. Shakespeare Very Smooth: 1234567890123456789012345 If we shadows have offended, Think but this, and all is mended That you have but slumb’red here, While these visions did appear. -Wm. Shakespeare What you are to do Once you have grabbed the source files and created a CodeWarrior project with them (see below), this is what you have to do: 1. First, implement the doFormatting method of the class Formatter.java. You can tell if your solution works by trying out the GUI using the Ragged Right formatting option (we have already implemented the trivial RaggedRightFormatter.java for you). To get your program to compile, you will have to create at least a trivial implementation for each of the three classes mentioned in the next step (i.e., just clone the RaggedRightFormatter version of formatLine for the moment; implement them properly when you have doFormatting working together with the RaggedRight style). 2. Look at RaggedRightFormatter.java. Create three new files CentredFormatter.java, SmoothFormatter.java, and VerySmoothFormatter.java to contain the Java classes CentredFormatter, SmoothFormatter, and VerySmoothFormatter respectively. Each of these new classes should: A SSGT 1: J USTIFY M Y T EXT Page 4 of 6 C ORNELL U NIVERSITY CS 211, S PRING 1998 extend Formatter.java, implement a constructor similar to what RaggedRightFormatter does, and implement the method formatLine appropriately (this is where most of the work is). 3. Test your program for us thoroughly. Come up with several different kinds of test cases. You should include the sample tests given here as part of your testing. Include a test of all four styles on at least one input file of your own creation for a few different line widths. Print out the input files and output files, label them appropriately, and hand them in with your code. You don’t have to test any of the aspects of the program that we created for you (e.g., valid input from the GUI, an input file containing a token longer than the stated maximum line length). 4. If you encounter a situation where an input file contains a single word longer than the maximum possible line, the code we have given you should catch it as an error. You shouldn’t have to worry about it yourself, just don’t hand in any test runs where this happens. (However, it might be good to test an example where the longest token is exactly the same as the maximum line length.) What you need from us The Java source code classes you need can be found on the course web page. Please download them as soon as possible and try them out. CodeWarrior details First, reread over the handouts on using CodeWarrior prepared by Hal Perkins. Now start up CodeWarrior. Create a new project that is a “Java Application”. Click on the Sources folder in the project window; you should see an entry for the TrivialApplication. Try compiling and running it, just for fun. Now remove the TrivialApplication from the Sources folder of the project and add the Java source files we wrote for you. Now click on the Targets tab above. Select Java Application then Java Target. The field Main Class currently reads TrivialApplication. Change this to JustifyWidget. Be very careful that you spell it correctly. If you misspell the main class name, you might find that when you try to compile that an error window pops up and disappears before you have a chance to read it. If this happens to you, it is likely that you have set the Main Class field to a class that is no longer in the project. Now click on the File tab to get the project window to show you the file view again. You are now ready to start. First, open up Formatter.java by double clicking in the file window. Complete the definition of the doFormatting() method as described above. Then create new java source files for each of the indicated classes: File!New, then edit the file, save it as a .java file and add it to the project. A SSGT 1: J USTIFY M Y T EXT Page 5 of 6 C ORNELL U NIVERSITY CS 211, S PRING 1998 Advice Remember all of the advice we have given you on commenting your code, choosing good variable names, etc. Make your program easy to read by using a consistent layout for indentation. You can do this assignment using Strings. To make life easy, don’t use StringBuffers, even if you know what they are already. You should read up on the various routines that java.lang.String defines, such as indexOf, charAt, equals, length, and substring (see Core Java, page 66 on), and be sure to go to recitation. By default, the program will look for input files in a directory called data, so you might want to create one of that name as a subfolder of your project folder. Do not use your program files as data guinea pigs as you will likely overwrite them without meaning to do so. Apart from adding the parts we asked for, please don’t change any of the existing code. Start early. It’s later than you think. This will take you a while to implement and debug. What to hand in Hand in printouts of: the follow Java classes which you wrote all or part of: Formatter.java, CentredFormatter.java, SmoothFormatter.java, and VerySmoothFormatter.java. The results of your testing. Clearly label (pen is OK) which are input files, and which are output files according to which style. Also, answer the following question, if you can: What single adjective best describes the tone of the quote we have used as an example? Why? (This last question isn’t worth any marks ... it’s intended for fun only.) A SSGT 1: J USTIFY M Y T EXT Page 6 of 6