Nada

Lab assignment 5 - Tree of life

The book Object-oriented programming in Java by Martin Kalin was previously main course litterature of DD2385. In this book is a program that displays the tree structure of a file directory with the help 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

Your task is to modify the program so that it shows a completely different tree, namely the Tree of life. The root of the tree should be Life and the root should have three children, Plants, Animals and Fungus, which 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 Liv.xml, also available as Liv.txt. A small test version the the file looks like this and can be found here: LillaLiv.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. Now, all that is needed is a recursive method which can be called with:
root = readNode(); //Reads the XML-file and created the whole tree
Before the method is called the program should have "eaten" the first angle bracket from the file so that readNode, after creating an empty node, can start reading the start tag and setting the level and attribute with node.setUserObject(...), "eat" the ending angle bracket, call readBetween(node) that reads the details and adds any possible child nodes. 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. Normally, the method ends with returning the newly created node. This recursive procedure is presented in lecture X (7?).

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 assume that the file has the structure of the example files, namely one tag per row, exactly one space between the level and the word "namn", no spaces next to "=" etc.

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 following requirement was originally forgotten in the english version of the assignment. Sorry about that!
The program must check that start- and end-tags of a node are equal and throw an exception if they mismatch. It is not necessary to detect other irregularities of the file.

Of course, it is fine if the program copes with exra spaces, extra empty lines, missing new-line-characters and other formatting "irregularities".

You must use recursion when building the tree.

Extra assignment: 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 in the class DefaultMutableTreeNode, and therefore also in MyNode.



Published by: <ann@nada.kth.se>
Updated 2009-05-12
Technical support: <webmaster@nada.kth.se>