Lab assignment 5 - Tree of lifeThe 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 lifeAs 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 beLife 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:
A recursive Tree of lifeInstead 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 treeBefore 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.
![]() 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: Details about lifeIn 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 exampleArt: Mås gillar Vaxholmsbåtar . Alter the code so that showDetails(TreePath p) prints this text in its JOptionPane.
Read from a textfileThe easiest way to do this is with theScanner 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
File format etc.
Extra assignment (small!):Alter the code so that it prints out a whole chain on the formbut 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> |