CS 5741 - Programming Assignment 3

Due Tue Oct 6, 10 points

Introduction

In this programming assignment you will write a class FileLister that produces formatted text listings for directories in various orders. You will test the FileLister class with a standalone application class that displays the text listings. You will be using classes in the CS5741_Toolkit project that you set up earlier.

The FileLister Class

When constructed with its default constructor, a FileLister produces a name ordered text listing of the user's home directory. The directory and the ordering can be changed with setter methods. The class uses a comparator as a strategy for sorting the files in its directory. Suggestions for sorting files and producing text listing are described in later sections of this web page.

The FileLister class should designed to be a subject in the Observer design pattern. It can do this by implementing the mvc.Model<String> interface defined in the CS5741_Toolkit project. It can implement registration services by extending the mvc.CallbackExecutor class. Thus the code for the class begins as follows.

      public class FileLister
              extends CallbackExecutor
              implements Model<String> {
    

To complete the implementation of the mvc.Model<String> interface, the FileLister needs to implement a getValue() method that returns the text listing of the files in its current directory.

You will also need to implement setter methods for the directory and the comparator. These should update the text listing and notify observers.

Files

Files should be represented by instances of the java.io.File class. You can construct File objects for two directories using the Java system properties:

Comparators

You should write three implementations of the java.util.Comparator<File> interface with the following orderings.

The Tester Class

The tester class extends JFrame to make a standalone application. It should have a FileLister instance variable and a JTextArea for displaying the file listing. You will need to create a mvc.Callback that sends text from the FileLister to the JTextArea. It should be registered with the FileLister so that it is an observer in the Observer design pattern.

Sorting Files

If your FileLister has an instance variable declared as a java.util.List<File> then it is easy to sort files using one of the sort() methods of the java.util.Collections class. For a File object that represents a directory, you can get an array of its files and subdirectories and convert it to a List<File> using the asList() method of the java.util.Arrays class.

Producing a File List

There is a combination of classes in the Java standard libraries that is very useful for producing formatted listings. The two classes are java.lang.StringBuilder and java.util.Formatter. When a Formatter is constructed with a StringBuilder as its constructor parameter then you can append formatted text to the StringBuilder just by invoking the format() method of the Formatter. This method is similar to the printf() family of functions in the C standard library. When you are done appending text, you can recover the complete text with the Formatter toString() method.

The listing text should have one line for each file or subdirectory in the current directory of the FileLister. Each line should contain the name, length, and modification time for its file or subdirectory. The information should be lined up in columns so that it is easy to read.