Java Reference
In-Depth Information
* @param filename The file of log data.
*/
public LogfileReader(String filename)
Exercise 4.75 Add a method busiestHour to LogAnalyzer that returns the busiest
hour. You can do this by looking through the hourCounts array to find the element with the
biggest count. Hint: Do you need to check every element to see if you have found the busiest
hour? If so, use a for loop or a for-each loop. Which one is better in this case?
Exercise 4.76 Add a method quietestHour to LogAnalyzer that returns the number
of the least busy hour. Note: This sounds almost identical to the previous exercise, but there is
a small trap for the unwary here. Be sure to check your method with some data in which every
hour has a non-zero count.
Exercise 4.77 Which hour is returned by your busiestHour method if more than one
hour has the biggest count?
Exercise 4.78 Add a method to LogAnalyzer that finds which two-hour period is the
busiest. Return the value of the first hour of this period.
Exercise 4.79 Challenge exercise Save the weblog-analyzer project under a different name
so that you can develop a new version that performs a more extensive analysis of the avail-
able data. For instance, it would be useful to know which days tend to be quieter than others,
Are there any seven-day cyclical patterns, for instance? In order to perform analysis of daily,
monthly, or yearly data, you will need to make some changes to the LogEntry class. This al-
ready stores all the values from a single log line, but only the hour and minute values are avail-
able via accessors. Add further methods that make the remaining fields available in a similar
way. Then add a range of additional analysis methods to the analyzer.
Exercise 4.80 Challenge exercise If you have completed the previous exercise, you could
extend the log-file format with additional numerical fields. For instance, servers commonly
store a numerical code that indicates whether an access was successful or not. The value 200
stands for a successful access, 403 means that access to the document was forbidden, and
404 means that the document could not be found. Have the analyzer provide information on
the number of successful and unsuccessful accesses. This exercise is likely to be very chal-
lenging, as it will require you to make changes to every class in the project.
4.17
Summary
In this chapter, we have discussed mechanisms to store collections of objects, rather than single
objects in separate fields. We have looked at two different collections in detail: the ArrayList
as an example of a collection with flexible size, and arrays as a fixed-size collection.
Using collections such as these will be very important in all projects from now on. You will
see that almost every application has a need somewhere for some form of collection. They are
fundamental to writing programs.
 
 
Search WWH ::




Custom Search