CSci 151: Foundations of computer science II
Home Syllabus Assignments Tests

Assignment 11: Logins

Due: 5:00pm, Friday, November 21. Value: 30 pts. Submit to Sauron.

The file logins.txt is a log containing information about users who have logged into the 14 laboratory Linux computers between October 1 and November 14, 2008. The file starts as follows.

1222850580	1222855380
1222853220	1222855080
1222854780	1222858560

Each line corresponds to a single user login to a laboratory computer. The first number indicates when the user logged in, measured in seconds since the beginning of 1970. (This is a common way for computers to represent times.) The second number indicates when the user logged out.

Your assignment is to write a Java program that analyzes this log and displays a table listing the amount of time that no computers were in use, that 1 computer was in use, that 2 computers were in use, and so on up to 14 computers. (We would want this information to justify the number of computers in our laboratory.) The following is the output from my solution:

 0: 283:36
 1: 159:37
 : (lines omitted)
13:   0:18
14:   0:00

You should do this by first reading the file into a priority queue of events, each event representing either a login at a particular time or a logout at a particular time. Your class to represent an event needs to implement the Comparable interface, so that when you later remove events from the queue, they are removed in chronological order.

To create a neatly formatted table for your output, you'll want to use the printf method of System.out.

System.out.printf("%2d: %03d\n", 1, 2);

The first string in the parentheses is the format string. The printf method displays each character found in the format string until it finds a percent sign. When it sees a percent sign, it uses the characters after the percent to determine how to display the next parameter that hasn't been displayed yet. Here is how it processes the above example.

  1. The format string starts with %2d, indicating to display an integer in decimal (base 10) over two columns; thus, if the integer is below 10, it includes a space before the integer. Since the first parameter after the format string is 1, the string  1 is displayed on the screen.
  2. Then the format string contains two non-percent characters , which are displayed on the screen as they are.
  3. Then the format string contains another formatting indicator %03d, which says to display a decimal integer in three columns, padded to the left with zeroes. Since the next parameter after the format string is 2, the string 003 is displayed on the screen.
  4. Finally, the format string contains a newline character, which is displayed on the screen so that following print or println invocations appear on the next line.

The end result is that the line  1: 002 appears on the screen.