bild
Skolan för
elektroteknik
och datavetenskap
 

Assignment 5 - Tree of Life

Goals

  • To be able to interpret example code and use it in a new application.
  • To be able to build a tree structure from a text file.
    You may use an XML-parser from the library but the instructions below assume that you do not.
  • To be able to use the advanced graphical library component JTree to visualize a tree.
  • To understand the classes that JTree rely on and to know how to define your own subclass for tree nodes.
The book Object-oriented programming in Java by Martin Kalin was course book of DD2385 some years ago. This book contains a nice example program where the advanced swing component JTree is used to visualize the structure of a file directory. In this assignment, you will graphically represent another tree structure with JTree but retain the frame layout and funcionality of the original program. Kalins code has been modified and divided into two classes. TreeFrame defines the tree and its environment. A TreeFrame is a JFrame and has a button, a small square to be clicked and also "owns" the tree itself. TreeFrame is a runnable program but the created tree has only a root object, no branches and leaves.

The second class DirTree2 inherits from TreeFrame och overrides the method in TreeFrame that creates the tree structure.

TreeFrame.java.     DirTree2.java

Download these classes. Familiarise yourself with the classes. Both have main-methods. Run them if you like.

java TreeFrame    shows a tree with just one node (root).

java DirTree2     shows the tree image of the current directory. Try another directory, e.g.

java DirTree2 ~     shows the tree of your home directory in Linux/Unix (may take a long time if it is big).

java DirTree2 ../../recepies     show the directory recepies two levels up from the current directory tree.

The tree may be expanded and contracted by clicking. If checkbox Show details is chosen, clicking a file or directory will display information about it.

A new subclass of TreeFrame

The structure of your program must resemble that of DirTree2, it inherits from TreeFrame and overrides a method that builds the tree. Of course you may define additional methods in your own program.

A minimal tree of life

This is an optional, preparatory task.
Create a simple tree with a root and three children which are leaves. Let the root of the tree represent "Life" and the children should be "Plants", "Animals" and "Fungi". In the final version of the program, the branches of the tree will contain part of the hierarchical classification of organism into e.g. kingdoms, families, classes and species. Now, define method initTree() to do the following:
  • Create a root node with contents "Life".
  • Make a TreeModel.
  • Make a node child just as the root node. Let the child represent "Plants".
  • Add the last node to the root node, making the root its parent:
    root.add(child);
  • Create children "Animals" and "Fungi" in the same way.
  • Create the JTree from the model (you can do this as soon as the model is created).
  • Compile, run and check that it looks ok..
You need only one node to create the model and the tree. The model may be extended subsequently.

A recursive tree of Life

Instead of creating nodes one by one in the program, read the tree from a file. Liv.xml (has the same contents as Liv.txt). Build the tree recursively from the file. A small test version of the file is shown below and is available here: LillaLiv.txt. The following files do not include any swedish letters (åäö): Life.txt, Life.xml och TinyLife.txt. The words in the files are in swedish though.
 
<Biosfär namn="Liv"> är allt som fortplantar sej 
<Rike namn="Växter"> kan inte förflytta sej 
</Rike> 
<Rike namn="Djur"> kan förflytta sej 
</Rike>
<Rike namn="Svampar"> är varken djur eller växter 
</Rike>
</Biosfär>
Each item of the file has a level value. This is the first word of the tag, e.g. "Biosfär" or "Rike". The item also has an explaining text which is the last part of each "starttag" line, after ">". The name of the item is in "...". (e.g. "Liv", "Djur"). The class DefaultMutableTreeNode can only store the name but we want to store level, name and explaining text. To achieve this, write a subclass whith String attributes for level and text.

A tree is a recursive structure which may be built with a recursive method. An alternative algorithm uses a stack and a simple loop. Both are excellent programming excercises.

Lifetree.gif

As in DirTree2, it must be possible to give a file name on the command line.

java LifeTree lifeFile

If no file name is provided, make sure the programs has a default name to use!
 

Details about a node

It the original program, if you mark "Show details", you will get information about a file if you click on the filename in the tree. In your program, the explaining text must be shown, e.g.
Art: Mås gillar Vaxholmsbåtar.

Implement showDetails(TreePath p) to display this text in its JOptionPane.
 

 
(The green color is obtained by setting the background of the tree to green, not the background of the JFrame.)
 

 

Read from a text file

The class Scanner provides an easy way to read from a text file.
  static Scanner sc = new Scanner(new File("infil.txt"));
new File(...) generates a FileNotFoundException if the file cannot be found. The Exception must be handled in a try ... catch section. After sucessfully opening the file, it may be read line by line with sc.nextLine(). It is convenient to make the Scanner-variable static and create the Scanner object already in the main method! You may have use also for sc.hasNextLine() to signal end of file. It is not necessary to put the reading statements in try ... catch, only the file opening.

Requirements

  • The program must manage an input file of any size. The program must not assume a constant number of children at any level or any maximum depth.
  • The program must check that the start tag is the same as the corresponding end tag. No other checks are required.

File format etc.

  • The program may assume that the file is formated as in the examples, i.e. with one "tag" on each line. It may also be assumed that there is exactly one blank space between the level and the word "namn".
    <Biosfär namn="Liv"> är allt som fortplantar sej
    and no spaces in other places except in the explaining text ending the lines.
  • It is OK to read the file line by line, i.e. let the program read a full line from the file, process the line, read a new line etc. An alternative to line reading is to read one character at a time but is is more difficult to interpret file this way and not required.
Of course, it is excellent if your program handles badly formatted data, e.g. many blank spaces in the tags, extra line feeds or no line feeds or syntax errors in the xml code but NOT required.

Demonstration

Run the program with the larger infile and demonstrate the functionalities. Don't forget to ask for the teachers signature when you have passed!

Extra assignment for a higher mark (this is a small one!)

Extend the method that gives information about the details so that beside the basic information
Art: Mås gillar Vaxholmsbåtar
you ge a chain of information with contributions from all nodes from the current node to the root.
The extended information should look something like this:
Art: Mås gillar Vaxholmsbåtar men allt som är Mås är Fåglar är Djur är Liv.
Class TreePath has a method getLastPathComponent() that can be useful. In DefaultMutableTreeNode and therefore also in MyNode is method getParent(). The task can be solved by other means.
Copyright © Sidansvarig: Ann Bengtsson <ann@nada.kth.se>
Uppdaterad 2014-05-04