Lab assignment 5 - Tree of lifeThe 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 lifeYour 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 beLife 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:
A recursive Tree of lifeInstead 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 treeBefore 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.
![]()
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() .
RequirementsYou 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! 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
Published by: <ann@nada.kth.se> Updated 2009-05-12 Technical support: <webmaster@nada.kth.se> |