Homework 1 - Basic Non-OOP Java Syntax


[Homepage] | [General Lab Info] | [TA's & Tutors] | [FAQ's] | [Homework] | [Excellent Programs] | [Exams]

Reading: Chapters 2-4, and the instructions for editing, compiling and running a program.

Due: At 8:00am, January 12, 2001, submit is turned off automatically. Work turned in after that time will not be accepted for grading.

Program Description - Logical Line Counting

(This program is slightly modified from a program used in a regional ACM Programming Contest competition.)

A software development company develops programs using a little known programming language with the following features:

A rough estimate for the size of a program can be made by counting logical lines, which means counting the semi-colons that do not appear inside a comment or string.

Write a program that reads in a program, counts the number of logical lines and the number of physical lines and issues warning messages for unterminated strings and unterminated block comments.

For each line containing an unterminated string, output a line of the form:

Unterminated string in line #.
where # is the physical line number within the program. Subsequent text must be handled as though the string had been terminated at the end of the line. If the program contains an unterminated block comment, output a line of the form:
Unterminated block comment at end of program.
After error messages, if any, output a line of the form:
The program contains # logical lines and # physical lines.
Output will be as specified above. It must have exactly the form shown, with no extra spaces around numeric output, and with a full stop at the end of each line and with no blank lines.

Reading the input

Console.in.readLine() will return the next line of input as a String. If there is no more input (end-of-file has been reached), then the expression will return null. Given a String say str the expression str.toCharArray() will convert the String to an array of characters. For example:
String str = Console.in.readLine();
// better check for a null here to indicate end-of-file
char[] buffer = str.toCharArray();
// now examine characters in the array of chars named buffer.

Sample Input1

(( Block comment; ))
"string"; ('another string;') @ line comment;

Sample Output

The program contains 1 logical lines and 2 physical lines.

Sample Input2

(((;(( A program typed by many monkeys; ));)))
yo mama! ;;@ ; '
' @ ;" ; ;'; "
)) ; (( ; )) '@"'; (";");

Sample Output

Unterminated string in line 3.
The program contains 7 logical lines and 4 physical lines.

Formatting

It is essential that your program be well formatted and easy to read (see 5.2-5.5 in the grading guidelines. It is your responsibility to make sure your program displays properly in a standard, 80 character wide, display window. If you are working at home, I recommend that after transferring your program to a CATS unix machine (e.g. teach.ic.ucsc.edu), that you view the file with a standard editor such as pico, emacs or vi. If any changes are required to fix indenting etc., you should also recompile your program to make sure you have not introduced any syntax errors (e.g. forgot to end a /* */ type comment).
[Homepage] | [General Lab Info] | [TA's & Tutors] | [FAQ's] | [Homework] | [Excellent Programs] | [Exams]

This page maintained by Charlie McDowell. Email regarding this site.