ANU The Australian National University



____________________________________________________

[ANU] [DCS] [COMP2100/2500] [Description] [Schedule] [Lectures] [Labs] [Homework] [Assignments] [COMP2500] [Assessment] [PSP] [Java] [Reading] [Help]

____________________________________________________

COMP2100/2500
Homework 5

Continue filling in a new Time Recording Log and Weekly Time Use Summary each week.

Write the following program, following the initial PSP and filling in the Project Plan Summary and a Defect Recording Log.

Write a program called ‘LineCount’ that counts the number of logical lines of code in Java program files.

The program must be able to accept any number of command line arguments, each assumed to be the name of a Java source file. For each of those files it must open the file and count the number of lines of code. It must count all lines except those that contain only:

or any combination of those.

After scanning each file the program must write a line to the standard output containing the number of lines of code in that file, and the file's name. After it has done that for each file named on the command line, if there was more than one class, then it must leave a blank line and then print a summary line giving the number of files read and the total number of lines of code.

For example, suppose that I run the program in a directory containing the code for Expression Version 4:

comp2100@partch java LineCount Expression4.java
  16 Expression.java
comp2100@partch java LineCount *.java
   7 Addition4.java
   6 Constant4.java
  16 Expression4.java
  32 ExpressionEvaluator.java
  30 ExpressionFormatter.java
   5 ExpressionVisitor.java
   7 Multiplication4.java
   6 Negation4.java

8 classes, 109 lines of code

The program does not have to check that all files are really Java source files. But it does have to watch out for errors like files that don't exist. If there is a problem with a file and it can't count the lines, it should print an error message instead of the usual output line, and then continue with any remaining files in the argument list.

comp2100@partch java LineCount Foo.java Expression.java
ERROR: couldn't open Foo.java
  72 Expression.java

In the output for each file, the number of lines should be right-justified in a field of width 4. (I can imagine a Java class with more than 999 lines, but not more than 9999.) Leave one space after the number and before the file name.


Hints

The expansion of the wildcards on the command line is done by the shell before the arguments are passed to the Java program. For example, when I typed ‘java LineCount *.java’, the program actually saw 8 arguments on the command line, not two.

The non-existent file problem is easily solved. Just create an object of class File with your file name. Then ask it if it exists. (Check the API documentation.)

As part of your testing of this program, run it over all your homework programs written so far and check that the line counts were correct. Where you see differences, count the lines by hand to verify the results.

You may assume that there is no legitimate reason for one of those characters to be sitting on a line all by itself. In other words, you don't have to check that a slash really has a star or two after it. This leads to a simple algorithm: Take a line from the file, remove every instance of all those characters (spaces, tabs, braces, slashes, stars) and then just check if it's empty.


Rationale

I've changed the way you count lines slightly. Now comment lines count, as long as they have something really written on them. The reason for this is that writing comments is work, and it should be counted. Getting “credit” for putting proper documentation in code might encourage programmers to really do it, rather than always leaving it out when they're in a rush. As long as you are consistent about taking this into account, it makes no difference in the long term. All your size estimates will be larger of course, but who cares? You'll probably also look more productive, since writing comments really is a bit easier than writing code.

If you really object to this, rewrite your program so that it doesn't count comments at all. (This is quite a bit harder.)

(By the way, why did class Expression4 have 16 lines of code rather than the 2 plus comments you'd expect from looking at the lectures? The reason is that I put in a main() method with the sample code from “Using Expression Version 4” in it. Just in case you were wondering.)

____________________________________________________

[ANU] [DCS] [COMP2100/2500] [Description] [Schedule] [Lectures] [Labs] [Homework] [Assignments] [COMP2500] [Assessment] [PSP] [Java] [Reading] [Help]

____________________________________________________

Copyright © 2005, Ian Barnes, The Australian National University
Version 2005.1, Friday, 11 March 2005, 18:10:41 +1100
Feedback & Queries to comp2100@cs.anu.edu.au