HW 4 - Note there are 2 parts (3 methods)
NOTE: This HW has 200 Bonus points available (an additional method)
SETUP
Note that in this assignment, you will have to use the eclipse IDE. To start:
- Download cse214_hw4_fall2009.zip and save it to a directory of your choice. Don't unzip the file! You'll need to remember where you put it.
- Open eclipse now, and choose whatever workspace directory you like.
- Click on:
- File
- Import
- General
- Existing Projects into Workspace
- Next
- Select Archive File
- Browse
- Now find and select the cse214_hw4_fall2009.zip file you just downloaded and click OK
- Finish
- You should now have a project with all the files necessary to do this homework, including source and data files.
- Verify that the project was properly imported into eclipse. Are you able to open and edit the source code in eclipse?
- NOTE: There should be no compiler errors after you import the zip file
Part 1 - using Trees to parse HTML
HTML is a very simple language. It is not a programming language (it has no logic or instructions). It is a markup language, using tags (like <p>) to define the structure and content of a Web page. The structure of an HTML document (as well as XML), is hierarchical. Tags are placed inside of other tags, to denote relationships between different page components. Becauase of this, Web Tools (like Browsers) store Web page documents like HTML & XML files in trees. In this part, we are going to build Web pages (HTML) by manipulating trees. For this part, I have provided the HTMLFrame class.
The HTMLFrame class has a main method that opens up a GUI and uses a JTree to display the contents of a tree that stores an HTML document we may want to build. Note that we will never load or save our HTML document, just view it using the preview button. The GUI has buttons to allow us to add node, remove nodes, and change the contents of nodes.
The Java API has a number of classes for storing and manipulating trees, but 3 interest us for this part of the assignment: DefaultTreeModel, which is a tree-managing class, DefaultMutableTreeNode, which is a tree node class, and TreePath, which stores an array of objects representing a path to a node.
YOUR TWO METHODS, ALL FOR THE HTMLFrame CLASS
- Define the
initTreeDatamethod so that it adds nodes to thetreeManagersuch that it forms a starting point for making Web pages. Such a starting point would have the necessary tags to make any Web page: html, head, title, and body, as well as a couple of example content tags. In order to do this, you are going to have to construct nodes and then insert them into the tree model properly (useinsertNodeInto). Note that there are methods for adding nodes to other nodes in the node class, but you should use the one in the tree-managing class because it does some other stuff like updates important instance variables that maintain information about the tree. - Define the
generateHTMLTextmethod such that when called, constructs and returns aStringobject representing a Web page by combining the contents of all the tree's nodes. For this you'll have to carefully walk the tree in the proper order and build your text.
NOTE: Note that when a tree has been updated, to ensure the changes are immediately displayed we have to call the JTree's scrollPathToVisible method, which takes a tree path argument. A tree path can be constructed using the path of nodes provided by the node you want to make visible. For example, to make the titleTextNode visible, we might write:
tree.scrollPathToVisible(new TreePath(titleTextNode.getPath()));
NOTE: Your nodes should be storing text. Either tag text (like <body>), or content text, which humans will acutally read in a Web browser. Your nodes do not have to store HTML closing tags, but your generateHTMLText method needs to include them when generating the Web page text.
Should you implement your tree initialization properly, when the program starts it should look like:
Should you click on the "Preview" button, it should open a dialog that looks like:
Part 2 - FlightPlanner
The FlightPlanner program might be used by an airline that flies to a number of American cities. It uses AirportGraph, a graph-managing class, to create paths between various cities it flies to. Left-click on a city to select the start city for a trip. Right-click on a city to select the destination of the trip. Examine the following 3 files:
- Airport.java
- AirportGraph.java
- FlightPlanner.java
YOUR METHOD, IN THE AirportGraph CLASS:
- public LinkedList
generateGreedyDepthFirstPath(String sourceID, String destinationID) - Define thegenerateGreedyDepthFirstPathmethod such that when called, it uses a greedy algorithm to find a path from the sourceID airport to the destinationID airport. It should return a LinkedList of String objects, representing the airport IDs in the path, including the start and end airport.
Note that I have provided the calculateDistance method inside the Airport class. This method will return the number of miles between the two Airport arguments, a1 & a2.
A Greedy Path Algorithm: When trying to find a path from point A to poitn B, at each step along the way, a Greedy algorithm would make decisions with the most immediate benefit. So, for example, if I am looking for a path starting at node A, I will start by getting all neighbors of A, and if none of them are B, I will try to make a path to B going through the neighbor closest to A. You should then continually do this until you have a complete path connecting the two airports.
200 Bonus Points
You may earn additional points in this assignment by finding the shortest path from the source to the destination, not just any old path. In order to do this, you'll have keep track of the minimal path found to each node you encounter, and update this path as you find faster routes. This is not so easy, so make sure you design your solution out first before starting your coding. For this part, you must define the following method:
- public LinkedList
generateShortestPath(String sourceID, String destinationID) - this method should find the shortest path from the sourceID to the destinationID and returning it as a LinkedList of the airport codes in that path.
Handin Instructions
- Go to the hand-in instructions page for the details on the required handin procedure. You must hand in by the designated due date and time in the course schedule for credit. Late assignments will not be graded.
Grading
All assignment will be graded purely based on program performance. Submissions that do not compile will receive 0 points. Submissions that never produce correct results will receive 0 points. The TAs will test your submissions using various input, and grades will be awarded depending on how often your code produces the correct results.
Web page created and maintained
by Richard McKenna

