CSE-1020: Introduction to Computer Science I
York University
Lab Test #2: TabStop
7:00pm Thursday 12 November 2009
(Lab-02)
  Welcome to the Lab-test Environment

During the test, you will not be able to access your regular home directory, access, receive, or send e-mail, print, or access the Internet.

The lab tests are closed-book and no aids are allowed.

At the end of your test session, this machine will be converted back to a standard, unrestricted Prism Lab machine. At this time, any files written by you during the test, except those that have been submitted, will not be recoverable. Make sure you submit your code before the end of the test. (Instructions to submit your code are below). If you do not submit your work on time, you will receive a grade of zero. (There are no exceptions.)

Instructions

You have the lab period, 80 minutes, to complete this lab test. The computers will automatically shutdown at the end of the time (e.g., 8:20pm), warning you several minutes in advance. No additional time will be provided, so you must submit your work prior to this; otherwise, there will be no way to recover it (it will be lost), and you will receive a grade of zero (no exceptions).

You are encouraged to submit often during the test. Note that newer submissions overwrite older ones.

Your program will be marked for good style, as well as for running correctly (producing correct output in the correct format).

Submitting Your Work

When you submit a file, you should include at the top of the file your name (surname, given name) and your Prism lab login. These should be placed in a comment so that the file will compile. Note that files which do not compile will receive a large penalty when marked, no matter how small the error that prevented compiling.

Submit the class TabStop.java before the time deadline. Here is the command to submit your work:

% submit 1020 labtest2R7.00 TabStop.java

Further details regarding the submit command can also be obtained by typing man submit.

Once again, you are encouraged to submit regularly. Newer submissions simply overwrite older ones.

Unlike eCheck assignments, there is partial credit possible. If you are able to finish all aspects of the program (for example, say, input validation was requested but you did not do this), you should still submit it.

Useful APIs

Here are the common APIs that you may access:

 
  The Task

Write a Java application called TabStop.java. Your app should first ask the user to input the tab-stop line. This is a line that starts with an x, followed by a small number of x's spaced out, and ends with an x. The x's indicate the tab stops. Let T be the number of x's - 1 (our number of tab stops) that appear in the line. (The last x is not counted here. We call it the final stop.)

Your app should then repeatedly prompt a user for a line of words, until the user enters Ctrl-D to indicate the end of input. Each line will consist of T words. Each word will be composed of a-z, A-Z, and 0-9. There will be white-space between the words. There will be no punctuation.

After reading all the input, your app should first print the tab-stop line. It then should print each word line tab aligned; that is, each word is aligned to start at its tab stop. If a word is too long (that is, it would go past the next tab stop or the final stop), it should be truncated to fit.

You do not have to do input validation. Your output should look exactly as indicated in the sample runs below.

Hints

There is a utility method format in class java.lang.String. It works like printf, but returns the string instead of printing it. The formatting instructions "%x.ys" formats a string to be between x and y spaces. If the string is shorter than x, it right justifies the string, padding with spaces. If the string is longer than y, it truncates the string to its first y characters. The formatting string "%-x.ys" left justifies instead. For example, format("X%5.5sX", "one") would result in "X oneX".

As an intermediate step, print each word line out properly tab aligned immediately after each input word line from the user. This will be easier to debug. If you submit this version, you could achieve nearly full marks. For full marks, print all the output after processing all the input, as in the sample runs.

Sample Runs

Here are three sample runs of a correctly written program. Note that the text in red is text that the user types. The text in black is what the program types. (Italics is for clarifying comments, and do not appear in the input or output.) The '%' represents the prompt from the command-line window (shell).

% java TabStop
Enter tab mark line:
x        x       x          x
Enter row:
one two three
Enter row:
1 2 3
Enter row:
Burton Parke Natalie
Enter row:
Ctrl-D (Note that you will not actually see this appear on the screen when typed.)
x        x       x          x
one      two     three      
1        2       3          
Burton   Parke   Natalie    
% java TabStop
Enter tab mark line:
x    x   x     x
Enter row:
one two three
Enter row:
1 2 3
Enter row:
Burton Parke Natalie
Enter row:
Ctrl-D
x    x   x     x
one  two three 
1    2   3     
BurtoParkNatali
% java TabStop
Enter tab mark line:
x       x    x       x   x
Enter row:
four three two one
Enter row:
4 3 2 1
Enter row:
Parke Burton Natalie Cornel
Enter row:
best of worst of
Enter row:
Ctrl-D
x       x    x       x   x
four    threetwo     one 
4       3    2       1   
Parke   BurtoNatalie Corn
best    of   worst   of  
%
Coding

You should use good programming practises as described in the textbook, and your code should conform to the style guide in the textbook. You should comment at least minimally, as by the guidelines.

You should not use any features of Java not covered in the first six chapters of the text. There will be some deduction if you do.