Created
November 11, 2014 01:33
-
-
Save OneRaynyDay/55b458bca5ce4b0bd128 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.ArrayList; | |
public class Console { | |
protected String[] ENTRY = {"Latitude", "Longitude", "City", "State"}; | |
public Console(){ /*empty on purpose*/ } | |
public void run(){ | |
DataHandler function = new DataHandler("Coordinate.xml"); | |
//loads up the nodelist | |
function.loadNodeList("Location"); | |
//finds the terms inside | |
ArrayList<Location> nodeList = function.populateADT(ENTRY); | |
LocationManager sorter = new LocationManager(nodeList); | |
sortAndDisplayByState(sorter, nodeList); | |
sortAndDisplayByCity(sorter, nodeList); | |
Location Seattle = sorter.searchLocation("Seattle"); | |
Location SanFran = sorter.searchLocation("San Francisco"); | |
System.out.printf("\nIt takes %-5.2f km to get to Seattle to San Francisco.", MathFunction.calculateDistance(Seattle, SanFran)); | |
} | |
public void sortAndDisplayByCity(LocationManager sorter, ArrayList<Location> nodeList){ | |
nodeList = sorter.sortLocationByCity(); | |
for(Location node : nodeList){ | |
System.out.println(node.getCity()); | |
} | |
} | |
public void sortAndDisplayByState(LocationManager sorter, ArrayList<Location> nodeList){ | |
nodeList = sorter.sortLocationByState(); | |
for(Location node : nodeList){ | |
System.out.println(node.getCity() + " : " + node.getState()); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Coordinate { | |
private double latitude; | |
private double longitude; | |
public Coordinate(double latitude, double longitude){ | |
this.latitude = latitude; | |
this.longitude = longitude; | |
} | |
public double getLatitude(){ return latitude; } | |
public double getLongitude(){ return longitude; } | |
public void setLatitude(double latitude){ this.latitude = latitude; } | |
public void setLongitude(double longitude){ this.longitude = longitude; } | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.IOException; | |
import java.util.ArrayList; | |
import org.w3c.dom.Document; | |
import org.w3c.dom.NodeList; | |
public class DataHandler { | |
private XMLReader scanner; | |
private Document doc; | |
private NodeList nodelist; | |
public DataHandler(String fileName){ | |
scanner = new XMLReader(); | |
try { | |
doc = scanner.ReadXML(fileName); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
System.out.println("Not successful in XML function"); | |
} | |
} | |
/* sends in an element to load up a list of nodes | |
* Postcondition: loads nodelist and returns true if there were any elements loaded | |
* if not, then return false. | |
*/ | |
public boolean loadNodeList(String element){ | |
nodelist = scanner.GetNodes(doc, element); | |
if(nodelist.getLength() != 0) | |
return true; | |
else | |
return false; | |
} | |
public ArrayList<String> stringifyNodes(String[] elements){ | |
return scanner.GetNodeStrings(nodelist, elements); | |
} | |
public ArrayList<Location> populateADT(String[] elements){ | |
//specifically for Coordinate.xml | |
ArrayList<Location> list = new ArrayList<Location>(); | |
//jump 4 spaces | |
ArrayList<String> stringList = stringifyNodes(elements); | |
for(int i = 0; i < stringList.size(); ){ | |
double lat = Double.parseDouble(stringList.get(i++)); | |
double lon = Double.parseDouble(stringList.get(i++)); | |
String city = stringList.get(i++); | |
String state = stringList.get(i++); | |
Location temp = new Location(lat, lon, city, state); | |
list.add(temp); | |
} | |
return list; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*A node class which allows users to store XML data in*/ | |
/*Not genericized, specific to Lab1*/ | |
public class Location { | |
private Coordinate coords; | |
private String mCity, mState; | |
//Node must be instantialized with these fields | |
public Location(double latitude, double longitude, String city, String state) | |
{ | |
coords = new Coordinate(latitude,longitude); | |
mCity = city; | |
mState = state; | |
} | |
public double getLat(){ return coords.getLatitude(); } | |
public double getLong(){ return coords.getLongitude(); } | |
public String getCity(){ return mCity; } | |
public String getState(){ return mState; } | |
public void setLat(double latitude){ coords.setLatitude(latitude); } | |
public void setLong(double longitude){ coords.setLongitude(longitude); } | |
public void setCity(String city){ mCity = city; } | |
public void setState(String state){ mState = state; } | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.ArrayList; | |
import java.util.Collections; | |
import java.util.Comparator; | |
//[email protected] | |
public class LocationManager { | |
private ArrayList<Location> nodeList; | |
public LocationManager(){ | |
nodeList = null; | |
} | |
public LocationManager( ArrayList<Location> nodeList){ | |
this.nodeList = nodeList; | |
} | |
public Location searchLocation(String name){ | |
int start = 0, end = nodeList.size()-1, middle = (start+end)/2; | |
while(middle <= end && middle >= start){ | |
if(nodeList.get(middle).getCity().compareTo(name) == 0) | |
return nodeList.get(middle); | |
else if(nodeList.get(middle).getCity().compareTo(name) < 0) | |
start = middle+1; | |
else | |
end = middle-1; | |
middle = (start + end)/2; | |
} | |
return null; | |
} | |
public ArrayList<Location> sortLocationByCity(){ | |
Collections.sort( nodeList , new Comparator<Location>(){ | |
public int compare(Location nodeA, Location nodeB){ | |
//Assumes that Location is being passed in | |
return (((Location)nodeA).getCity()).compareTo(((Location)nodeB).getCity()); | |
} | |
}); | |
return nodeList; | |
} | |
public ArrayList<Location> sortLocationByState(){ | |
Collections.sort( nodeList , new Comparator<Location>(){ | |
public int compare(Location nodeA, Location nodeB){ | |
//Assumes that Location is being passed in | |
return (((Location)nodeA).getState()).compareTo(((Location)nodeB).getState()); | |
} | |
}); | |
return nodeList; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.ArrayList; | |
import java.util.Collections; | |
import java.util.Comparator; | |
import java.util.List; | |
public class MainFunction { | |
public static void main(String[] args){ | |
Console console = new Console(); | |
console.run(); | |
} | |
} | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.ArrayList; | |
import java.util.Collections; | |
import java.util.Comparator; | |
public class MathFunction { | |
public final static int RADIUS = 6371; | |
public static double calculateDistance(Location nodeA, Location nodeB){ | |
double deltaLat = Math.toRadians(nodeB.getLat() - nodeA.getLat()); | |
double deltaLong = Math.toRadians(nodeB.getLong() - nodeA.getLong()); | |
double lat1 = Math.toRadians(nodeA.getLat()); | |
double lat2 = Math.toRadians(nodeB.getLat()); | |
double a = Math.sin(deltaLat / 2) * Math.sin(deltaLat / 2) + Math.sin(deltaLong / 2) * Math.sin(deltaLong / 2) * Math.cos(lat1) * Math.cos(lat2); | |
double c = 2 * Math.asin(Math.sqrt(a)); | |
return RADIUS * c; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.File; | |
import java.io.IOException; | |
import java.util.ArrayList; | |
import java.util.Iterator; | |
import javax.xml.parsers.DocumentBuilder; | |
import javax.xml.parsers.DocumentBuilderFactory; | |
import javax.xml.parsers.ParserConfigurationException; | |
import org.w3c.dom.Document; | |
import org.w3c.dom.Element; | |
import org.w3c.dom.Node; | |
import org.w3c.dom.NodeList; | |
import org.xml.sax.SAXException; | |
public class XMLReader | |
{ | |
public Document ReadXML(String sfile) throws ParserConfigurationException, SAXException, IOException | |
{ | |
File file = new File(sfile); | |
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); | |
DocumentBuilder db = dbf.newDocumentBuilder(); | |
Document doc = db.parse(file); | |
return doc; | |
} | |
public NodeList GetNodes(Document opendoc, String selement) | |
{ | |
opendoc.getDocumentElement().normalize(); | |
NodeList nodelist = opendoc.getElementsByTagName(selement); | |
return nodelist; | |
} | |
public Node GetNode(NodeList nodelist, int index) | |
{ | |
Node node = null; | |
int length = nodelist.getLength(); | |
if (index > -1 && index < length) | |
node = nodelist.item(index); | |
return node; | |
} | |
public ArrayList<String> GetNodeStrings(NodeList nodelist, String[] selements) | |
{ | |
ArrayList<String> slist = new ArrayList<String>(); | |
for(int s=0; s<nodelist.getLength() ; s++) | |
{ | |
Node firstNode = GetNode(nodelist,s); | |
if(firstNode.getNodeType() == Node.ELEMENT_NODE) | |
{ | |
Element element = (Element)firstNode; | |
for (int x=0; x<selements.length; x++) | |
{ | |
NodeList nlist = element.getElementsByTagName(selements[x]); | |
Element item = (Element)nlist.item(0); | |
NodeList tlist = item.getChildNodes(); | |
slist.add(((Node)tlist.item(0)).getNodeValue().trim()); | |
} | |
} | |
} | |
return slist; | |
} | |
public boolean TestXML(String sfile,String selement,String[] sfields) throws ParserConfigurationException, SAXException, IOException | |
{ | |
boolean breturn = false; | |
try | |
{ | |
Document doc = ReadXML(sfile); | |
NodeList nodelist = GetNodes(doc,selement); | |
ArrayList<String> slist = GetNodeStrings(nodelist,sfields); | |
for (String s : slist) | |
System.out.println(s); | |
} | |
finally | |
{ | |
System.out.println("Error in xml file: " + sfile); | |
} | |
breturn = true; | |
return breturn; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment