Nada

Lab assignment 5 - Tree of life

The book Object-oriented programming in Java by Martin Kalin was previously main course litterature of DD2385. This book includes a program that displays the tree structure of a file directory using an advanced swing component, JTree. The code is available here: DirTree.java. Compile and run it with:

java DirTree

and it will show the tree of the directory you are in. You could also, for example, try:

java DirTree ~henrik/labbar

which will show you the tree of the directory stated in the argument. Make sure that you can click and expand directories and that you can get details about a file if Show details is chosen.

A minimal Tree of life

As a first small task, to get started, modify the program so that it shows a completely different tree, namely a very small Tree of life. The root of the tree should be Life and the root should have three children, Plants, Animals and Fungus. In the full assignment these nodes will in turn contain classes, orders, families and species. Start with some minor modifications:
  • The root node represents "Life", not the directory name.
  • Erase all code in private void buildTree() and add code that does the following.
    • Create a node child in the same way as the root node but with the text Plants.
    • Add the node to the tree with root as its parent. You can see how this is done some rows below in the code.
  • Do the same with animals and fungus.
  • Compile, run and verify the tree.

A recursive Tree of life

Instead of creating the nodes by hand, read the Tree of life from the file Life.xml, also available as Life.txt. A small test version of the file is displayed below and can be found here: TinyLife.txt.
<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>
Apart from the name of the node there is also a level (i.e. Rike) and details (the information to the right of the start tag > but, unfortunately, there is nowhere to store this information in the class DefaultMutableTreeNode. Therefore, create a subclass MyNode which contains the String variables level and text. Read the file and create a tree recursively, e.g. using a method like
root = readNode(starttag); //Reads the XML-file and created the whole tree
Before the method is called the program should have read past the first angle bracket from the file and identified the first starttag. readNode() reads data for the node and creates a node object. Lifetree.gif When the end tag is reached,read it and compare with the start tag and if they don't match, throw a RuntimeException or print out an error message and exit from the program. Normally, the method ends with returning the newly created node. In one of the lectures (number 9 or 10) a solution for the file reading and tree buildning problem will be outlined.

Your are free to use other methods than the one suggested and/or use more than one method. It is also allowed to use some static variables, accessible to all the recursive method calls.

Just as the original program DirTree, your program should take a file name as argument:

java LifeTree life-file

and if no file name is given, let the program use the file Liv.xml (or Liv.txt).

Details about life

In the original program, information about a file is obtained by choosing "Show details". Your program should instead show the information about the the node, for example Art: Mås gillar Vaxholmsbåtar. Alter the code so that showDetails(TreePath p) prints this text in its JOptionPane.

Read from a textfile

The easiest way to do this is with the Scanner class:
    Scanner sc = new Scanner(new File("infil.txt"));
new File(...) may generate FileNotFoundException which must be checked (handled) but if the file was found, you can proceed and read line by line with sc.nextLine().

Requirements

  • You may not assume that the root node or any other node has a fixed number of children, any number of children must be possible. The program must also manage an arbitrary depth of the tree!
  • The program must check that start- and end-tags of a node are equal and throw an exception or print an error message and exit if they mismatch. It is not necessary to detect other irregularities of the file.
  • You must use recursion when building the tree.

File format etc.

  • You may assume that the file has the structure of the example files, namely one tag per line, exactly one space between the level and the word "namn", no spaces next to "=" etc.
  • It is fine to read the file line by line which means that the program has access to one complete line of the file at all times. Another alternative is to read one character at a time from the file which makes the task harder!
Of course, it is excellent if the program copes with extra spaces, extra empty lines, missing new-line-characters and other formatting "irregularities".

Extra assignment (small!):

Alter the code so that it prints out a whole chain on the form but all that is Mås is Fåglar is Djur is Liv, i.e. the names of all ancestor nodes in the tree are used. A useful method in TreePath is getLastPathComponent(). The method getParent() is available in the class DefaultMutableTreeNode, and therefore also in MyNode.



Published by: <ann@nada.kth.se>
Updated 2010-04-24
Technical support: <webmaster@nada.kth.se>