King Fahd University of Petroleum and Minerals

Department of Information and Computer Science

ICS 313: Fundamentals of Programming Languages

Fall 1999-2000

Date: 30th October 1999   Programming Assignment # 1: Object Oreinted Programming in Java            Due date: 20th November 1999

Deliverables and Submission guidelines

 

Download this assignment from the course page and take it on a floppy. If you open it in a browser, you will have the advantage of easy navigation to some web documents through hot links. Deliverables and Submission guidelines are provided on-line.

Before You Start

After downloading this softcopy and taking a print out, you may consider following the strategy given below.

1.       Set aside about 20-40 hours (depending upon your familiarity with Java) of your schedule during the three-week interval exclusively catered for this assignment. For best results, allocate it in sufficiently large chunks; say three or more hours each.

2.       Read through this whole document slowly and carefully[1], noting down what you consider to be important details. If any statements are not clear, get them clarified as soon as possible.

3.       Dwell upon the scope and plan of the project. Prepare an initial sketch on paper with a pencil on what you need to do for each of the problems. You may sketch high-level steps.

4.       Prepare a table of required functions based on your initial sketch.

5.       Go to the JDK documentation as well as the pointed sections in the tutorial and read through the indicated sections taking a note of important capabilities that can be of use to solve this assignment.

6.       Revise the initial sketch you prepared in step 3, in view of the available facilities you encountered in step 5.

7.       Prepare a soft copy of those requirements (outcome of step 6) at a detailed level (down to the level of which methods you may want to use), so that coding becomes straightforward. Prepare an organized directory structure for each of the problems, so that you can attempt to bootstrap the solutions as well as documentation. Also prepare data files and test data to be used on developed programs in the various problems.

8.       If you wish to, you may seek feedback from your instructor, at this juncture on your report and overall approach.

9.       Solve each of the required problems sequentially – with the bare minimum bells and whistles addressing the essential functionality. Keep taking notes on mistakes you’re making and those aspects that are taking time. At the end of the project you will notice how much time you could have saved with a bit more investment in steps 2 – 5.

10.    Test the functionality thoroughly for each of those minimal implementations.

11.    Clean up your files and take a backup copy as a major checkpoint.

12.    Make another pass through this document, relevant JDK classes and packages, Java FAQs book, and mentioned sections in the Java tutorial. Revise your detailed design to reflect the problems and workarounds that you had to devise during this early implementation pass.

13.    Start afresh with a new organization and redoing your work, taking code from the checkpointed version of step 11 and solve all the required problems sequentially. This time give adequate attention to the potential user errors and catching and handling different kinds of exceptions. As you test the programs, keep preparing detailed and annotated sessions as you saw in the sample program sessions you were given. When you are preparing annotated sessions show as many illustrative example executions as possible to illustrate the capabilities. Cut down repetition.

14.    Prepare a project report according the spelled out requirements for project reports.

15.    Take a checkpoint of all your work.

16.    Take a look into the optional extra credit questions, which are logical generalizations of the basic problems you have already completed. Attempt to tackle as many as you can in view of your time budget. You are expected to be creative in devising solutions and suggesting and doing alternatives for these extra credit questions. There are fewer restrictions on adherence to guidelines. However they have to work well within the framework you’re developing.

17.    After one or two days of break, reflect on the full activity of the project, your state at the beginning, major transitions, costly mistakes etc. Prepare a new design document with detailed design (this is similar to the one that resulted from step 6) that you may follow to tackle this project if you have had the kind of experience you now have.

18.    Prepare a final project report that includes all these components. One cannot over emphasize the need for the section on reflections (you will be doing this for yourself and not to the grader or your instructor), which will help you better-organized next time.

 


On-line Exploration and Reading Part

 

Exercise 1 [Online Reference Section]:

Continue your exploration of the Java tutorial that you started as part of the previous assignment.

Go through the Trails Covering the Basics in the tutorial. In particular the trials Essential Java Classes and Writing Applets are recommended for the time being. In particular, study the reference material for the classes: java.lang.System, java.io.File, java.util.StringTokenizer. The categorized list of frequently asked questions and answers will be a valuable reference after you are done with the detailed design of your project. At a bit more advanced stage, after you are finding it at ease in dealing with JDK API, you can get more benefit by a careful study of example clippings.

 

You are also recommended to examine the material that was prepared for and discussed in various Java lectures together with the example programs they contain.

 

Programming Part

 

A number of graded exercises are given below, gradually building up functionality. You are expected to address each one as a separate program with its own documentation. Each problem is expected to result in a little utility. You may wish to imitate the style of keeping a program in source and in binary as well as an annotated session illustrating the source, compilation, and execution of that utility, all in a folder. Each utility is made out of one or more classes that can make use of other classes/utilities. For each utility there should be a main() routine in some class that can illustrate a sample usage of the utility.

 

1.     A Copy Utility for Text Files [CopyFile.java]:

Study the sample program NumberLines.java from the softcopy handout of Java Lecture 1. Design and implement a simple copy utility that takes two parameters, a source text file and a target text file. It reads one line at a time and copies it to the destination. Handle as many exceptions as you can. A sample invocation can be:

>> java CopyFile CopyFile.java CopyFile.java.bak

 

2.     An Option Processor [OptionHandler.java]:

After reading the documentation on the java.util.Properties class, design a utility that reads a configuration file and processes its contents line by line, remembering the attribute names and their values. It should support a number of methods so that new entries can be added, existing entries can be modified, deleted, or printed. In addition, it can supply the values of an attribute for a given attribute name. Have the main() process a file and list the options out. Assuming that there is a file Project.cfg with contents:

Project.cfg

HIDDEN_FILE_EXTENSION=”*.bak;*.old;*.dll”

INDEX_FILE_NAME=”jindex.html”

LINE_WIDTH=80

SHOW_SERIAL_NUMBERS=false

 

>> java OptionHandler Project.cfg

may produce:

 

HIDDEN_FILE_EXTENSION      ”*.bak;*.old;*.dll”

INDEX_FILE_NAME          ”jindex.html”

LINE_WIDTH              80

SHOW_SERIAL_NUMBERS       false

 

2e. (Optional Extra Credit Question) An Option Processor [OptionHandler.java]:

Enhance the Option Handler so that one could insert comments into configuration files that begin with two minus symbols. They should be dropped from consideration. Such a facility allows programmers to document the configuration files.

Project.cfg

-- What are the extensions that need to be hidden?

-- The same extensions are assumed for directories too.

HIDDEN_FILE_EXTENSION=”*.bak;*.old;*.dll”

-- Name of the index file to be generated

INDEX_FILE_NAME=”jindex.html”

-- Line width for printing on A4 paper

LINE_WIDTH=80

-- Skip the display of serial numbers

SHOW_SERIAL_NUMBERS=false

 

>> java OptionHandler Project.cfg

may produce:

 

HIDDEN_FILE_EXTENSION       ”*.bak;*.old;*.dll”

INDEX_FILE_NAME           “jindex.html”

LINE_WIDTH               80

SHOW_SERIAL_NUMBERS        false

 

3.     A Tokenizer [Tokenizer.java]:

Study the classes java.io.StreamTokenizer and java.util.StringTokenizer. Design and implement a simple tokenizer utility that reads a line at a time from keyboard or a file or a URL and tokenizes it according to the token delimeters given in its configuration file Tokenizer.cfg. Assuming that there is a file Tokenizer.cfg contains:

Tokenizer.cfg

TOKEN_SEPARATOR=”;”

SHOW_SEPARATORS=false

A sample invocation can be:

>> java Tokenizer

  This is a test;Let us see how many lines will come out of it;That is it for now;Bye

  This is a test

Let us see how many lines will come out of it

That is it for now

Bye

^Z

 

3e. (Optional Extra Credit Question) A Tokenizer [Tokenizer.java]:

Generalize the tokenizer so that it understands a few more configurable parameters. It recursively attempts to tokenize by the first separator. When there is only one separator, it reduced to the previous problem. When there are several separators, it first tokenizes by using the first character of the TOKEN_SEPARATORS, and prints each token, before recursively invoking tokenization on it with the remaining separators.

 

Tokenizer.cfg

TOKEN_SEPARATORS=”;:,”

SHOW_SEPARATORS=false

 

A sample invocation can be:

>> java Tokenizer

O n e,T w o:Three,Four:Five,six,seven;eight nine ten;

O n e,T w o:Three,Four:Five,six,seven

O n e,T w o

O n e

T w o

Three,Four

Three

Four

Five,six,seven

Five

six

seven

eight nine ten

^Z

 

4.     An ASCII to HTML Table Converter [HTMLTableGenerator.java]:

Adapting the utilities CopyFile and Tokenizer, design and implement a simple HTML table generator which converts an ASCII file with tabbed text into a HTML file which when interpreted by a browser will display a table. Assume that there is a file HTMLTableGenerator.cfg with the following contents.

 

 

 

HTMLTableGenerator.cfg

PAGE_TITLE=”Java Assignment for ics 313”

TABLE_HEADING=”ICS313: Programming Languages (Fall 1999)”

BGCOLOR=”#B8E0FE”

PERCENTAGE_COLUMN_WIDTHS=”40;40;20”

DATA_FORMAT=”Hyperlink;Text;Text”

 

The invocation is of the form:

>> java HTMLTableGenerator <data file> <index file to be generated>

A sample invocation can be:

>> java HTMLTableGenerator index.txt jindex.html

 

Assuming the contents of index.txt to be:

index.txt

Quiz3-Comments.html  Quiz3-Comments.html  Comments on Quiz 3   5893

Downloadables/indexJ.html   Downloadables Additional Downloadables   19

the HTML table generator produces a file jindex.html with contents as shown below. Pay particular attention to the bold-faced items that were extracted from the configuration file and the bold-italicized ones that came from the data file. If there are 10 lines in the data file, i.e., index.txt, then there will be ten blocks delimited by <tr> * * * </tr>. Also, notice that a Hyperlink specification takes two pieces of information, one for HREF and the other for display. To get an idea about what it will look like, copy the table below into a notepad and save it as test.html and view it in a browser.

jindex.html

<head>

<title>Java Assignment for ics 313  (Fall 1999) - HTMLTableGenerator</title>

</head>

 

<body>

<h1 align="center"><font color="#8B0537">ICS313: Programming Languages (Fall 1999)</font></h1>

 

<div align="center">

<center>

   <table border="2" cellpadding="2" width="80%" style="border: medium none rgb(0,0,128)"

      bordercolor="#000080" bordercolorlight="#FFFF00" bordercolordark="#FF0000" bgcolor="#B8E0FE">

     <tr>

          <td width="40%"><a href="Quiz3-Comments.html">Quiz3-Comments.html</a></td>

          <td width="40%">Comments on Quiz 3</td>

          <td width="20%" align="right">5893</td>

     </tr>

     <tr>

          <td width="40%"><a href="Downloadables/indexJ.html">Downloadables</a></td>

          <td width="40%">Additional Downloadables</td>

          <td width="20%" align="right">19</td>

     </tr>

   </table>

</center>

</div>

 

</body>

 

</html>

 

4e. (Optional Extra Credit Question) An ASCII to HTML Table Converter [HTMLTableGenerator.java]:

If you notice, there are many hard coded constants in the above file as well as in your program. For maximum flexibility, each of them could have been replaced by a parameter with a default value, allowing the user to change them by having them in the configuration file without having to change the program. Generalize your utility so that it takes an extra parameter that corresponds to a template file, and transcribes the template file into the output file, by substituting all variables in the template file by their corresponding values as given in the configuration file. In the process of generating the output, it copies the portion between <$ *** $> as many times as needed to the output file, once for each record from the data file. Notice that the configuration variables are represented by %VAR_NAME% and the variables that need to be taken from the data file by $DataVariable$. Blocks of text that require repetition, once for each data line are represented by <$ *** $>. Such a utility allows the user to separate the issue of designing a structure for an HTML file from that of utility development.

A sample invocation can be. Notice how backward compatibility with the previous tool was preserved:

>> java HTMLTableGenerator dataFile outputFile [ templateFile ]

The following call amounts to the same as the one in the previous exercise.

>> java HTMLTableGenerator index.txt jindex.html

The new call with a new template file allows a designer to experiment with template files.

>> java HTMLTableGenerator index.txt jindex.html index.template

index.txt

ADDRESS_OF_LINK     LINK  DESCRIPTION  SIZE

 

Quiz3-Comments.html  Quiz3-Comments.html  Comments on Quiz 3   5893

Downloadables/indexJ.html   Downloadables Additional Downloadables   19

 

index.template

<head>

<title>%PAGE_TITLE% - HTMLTableGenerator</title>

</head>

 

<body>

<h1 align="center"><font color="#%HEADING_COLOR%">%TABLE_HEADING%</font></h1>

 

<div align="center">

<center>

   <table border="2" cellpadding="2" width="80%" style="border: medium none rgb(0,0,128)"

      bordercolor="#000080" bordercolorlight="#FFFF00" bordercolordark="#FF0000" bgcolor="#B8E0FE">

<$

     <tr>

          <td width="%WIDTH_OF_COLUMN1%"><a href="$ADDRESS_OF_LINK$">$LINK$</a></td>

          <td width="%WIDTH_OF_COLUMN2%">$DESCRIPTION$</td>

          <td width="%WIDTH_OF_COLUMN3%" align="right">$SIZE$</td>

     </tr>

$>

   </table>

</center>

</div>

 

</body>

 

</html>

 

 

jindex.html

<head>

<title>Java Assignment for ics 313  (Fall 1999) - HTMLTableGenerator</title>

</head>

 

<body>

<h1 align="center"><font color="#8B0537">ICS313: Programming Languages (Fall 1999)</font></h1>

 

<div align="center">

<center>

   <table border="2" cellpadding="2" width="80%" style="border: medium none rgb(0,0,128)"

      bordercolor="#000080" bordercolorlight="#FFFF00" bordercolordark="#FF0000" bgcolor="#B8E0FE">

     <tr>

          <td width="40%"><a href="Quiz3-Comments.html">Quiz3-Comments.html</a></td>

          <td width="40%">Comments on Quiz 3</td>

          <td width="20%" align="right">5893</td>

     </tr>

     <tr>

          <td width="40%"><a href="Downloadables/indexJ.html">Downloadables</a></td>

          <td width="40%">Additional Downloadables</td>

          <td width="20%" align="right">19</td>

     </tr>

   </table>

</center>

</div>

 

</body>

 

</html>

 

 

5.     A Simple Tree Traverser [BasicTreeTraverser.java]:

Study the classes java.io.File. Design and implement a simple tree traversal utility that recursively traverses a hierarchy of directories and files, performing the specified actions on the directories and files as it traverses. Let the default action be printing the path name and size information

>> java BasicTreeTraverser ics313Submissions

listing of path names of all files and folders under ics313Submissions folder. This will be similar to invoking a DOS command like dir ics313Submissions /s or the Unix utility ls –l.

 

6.     A Tree Traverser [Traverser.java]:

Generalize the above so that it applies a pass/fail test for each folder or file before performing the specified action. These tests as well as actions can be supplied either as class-file names in the configuration file Traverser.cfg or can be defined in the overriding methods of a sub-class of Traverser.java.

Assuming that there is a file Traverser.cfg contains:

Traverser.cfg

HIDDEN_FILE_EXTENSION=”*.bak;*.old;*.dll”

HIDDEN_DIRECTORY_EXTENSION=”Images;*.old;*.dll”

DIRECTORY_ACTION=HTMLTableGenerator

FILE_ACTION=Print

>> java Traverser ics313Submissions

listing of path names of all files and directories that do not have an extension of “bak” or “old” or “dll” or those that are in the Images directories.

 

6e. (Optional Extra Credit Question) A Tree Traverser [Traverser.java]:

Read more about interfaces from the language specification, the tutorial, FAQs book, and the java language reference books. Rewrite the previous version using an interface Tester with a method boolean passes(File) and another interface Action which implements a method void act(File). The class HiddenFileOrDirectory implements the interface Tester. The class HTMLTableGenerator implements the interface Action. Inside the class Traverse, there should not be any reference to either HiddenFileOrDirectory or HTMLTableGenerator. This greatly facilitates the task of developing other applications. For instance, say that we want to print the contents of all the non-hidden files in a tree that were modified during the last one-week. It will be sufficient to write a simple class, Print that implements Action provided a given file was modified during the past one week. Changing the configuration file to make it the value of variable, FILE_ACTION is what all we need to do. You may find the method Class.forName() useful in this exercise.

Assuming that there is a file Traverser.cfg contains:

 

Traverser.cfg

HIDDEN_FILE_EXTENSION=”*.bak;*.old;*.dll”

HIDDEN_DIRECTORY_EXTENSION=”Images;*.old;*.dll”

PATH_TEST=HiddenFileOrDirectory

FILE_ACTION=Print

>> java Traverser ics313Submissions

prints all non-hidden files under ics313Submissions.

 

7.     Redesign:

By the time the tools mentioned so far are implemented one gets to explore a reasonable portion of the important Java libraries in some depth. It is time to reconsider the tools in light of that enhanced understanding. Perform a thorough redesign of your tools on paper, if you have to redo them from the beginning. Compare this version from the previous draft you had when you started the implementation. Comment on the significant changes between the two versions.

 

7e. (Optional Extra Credit Question) A Graphical Traverser [GTraverser.java]:

Read more about developing graphical user interfaces from the tutorial, FAQs book, and the java language reference books. Add a graphical user interface similar to that of File Explorer under Windows 95 that shows the files and directories in two adjacent frames enabling navigation. If somebody clicks on a directory, its contents will be shown in the right-side panel. The major difference between windows version and yours is that your version will be a richer version with the ability to hide things the way you want them to be based on the user configuration. In addition it is portable as well as readily extendible.

 

8e. (Optional Extra Credit Question) A Graphical Explorer [Explorer.java]:

In this exercise you’ll be extending (through inheritance) the previous solution. Add the customization where the user can specify an external application with a given file extension as part of the configuration file. When the user clicks on a file, based on its type, the appropriate external application will be launched on that file. You will need to know a bit more about launching external applications in this context. So get started with the class java.lang.System as well as several clippings from the examples.


 

Organizational Part

 

Exercise 1 [Filing and Organization]:

Keep all your work related to this assignment in the folder:

ics313991-ProgrammingLanguages\Submissions\Project1-ObjectOriented

in the directory structure you prepared in the first assignment. Keep all the programs related to this assignment in the folder:

ics313991-ProgrammingLanguages\Programming\Java

Keep all the class files related to this assignment in the folder:

ics313991-ProgrammingLanguages\Library\Java

Modify your batch file so that it sets the CLASSPATH to include the previous path enabling you to use these tools either interactively or in your other programs.

 

As in the previous assignment keep two versions of your files, one in rtf format for good layout as the primary/reference copy and the other in html format facilitating easy browsing. Make a copy of the template for sampleIndex.html and sampleIndex.txt that you prepared in Assignment # 1 as:

ics313991-ProgrammingLanguages\Submissions\Project1-ObjectOriented\index.html

overwriting the existing place holder page. Update both index.html and index.txt so that they adequately represent the contents of all the folders whose contents you altered.

Needless to say that if you have done an adequate job in this project, you would eliminate this tedious job of generating index.html files manually.

 



[1] In the first pass, you may wish to give less importance to the extra credit questions that have a label ending with ‘e’.