Created
February 12, 2012 21:57
-
-
Save vj--/1811047 to your computer and use it in GitHub Desktop.
two
This file contains 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
package com.vijayraj.learn; | |
import javax.xml.parsers.SAXParser; | |
import javax.xml.parsers.SAXParserFactory; | |
public class Problem2 { | |
public static void main(String args[]) { | |
try { | |
SAXParserFactory factory = SAXParserFactory.newInstance(); | |
SAXParser parser = factory.newSAXParser(); | |
SAXHandler handler = new SAXHandler(); | |
parser.parse("test.xml", handler); | |
System.out.println("Standard Deviation: " + handler.getStandardDeviation()); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
System.out.println(e); | |
} | |
} | |
} |
This file contains 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
package com.vijayraj.learn; | |
import java.util.ArrayList; | |
import org.xml.sax.Attributes; | |
import org.xml.sax.SAXException; | |
import org.xml.sax.helpers.DefaultHandler; | |
public class SAXHandler extends DefaultHandler { | |
int depthFromArticle = 0; | |
ArrayList<Integer> ListOfP = new ArrayList<Integer>(); | |
public void startElement(String uri, String localName, String qName, | |
Attributes attributes) throws SAXException { | |
System.out.println(qName + " - " + depthFromArticle); | |
if (qName.equalsIgnoreCase("p")) { | |
ListOfP.add(depthFromArticle); | |
} | |
depthFromArticle++; | |
} | |
public void endElement(String uri, String localName, String qName) | |
throws SAXException { | |
// System.out.println("End Element: " + qName); | |
depthFromArticle--; | |
} | |
public void characters(char ch[], int start, int length) | |
throws SAXException { | |
} | |
public double getStandardDeviation(){ | |
double standardDeviation = 0; | |
int length = ListOfP.size(); | |
int sum =0; int average = 0; | |
for(int i=0;i<length;i++) | |
{ | |
sum+=(ListOfP.get(i)); | |
} | |
average=sum/length; | |
ArrayList<Integer> newList = diffData(average); | |
sum=0; | |
for(int i=0; i<newList.size(); i++) | |
{ | |
sum+=(newList.get(i)); | |
} | |
standardDeviation = Math.sqrt(sum/average); | |
return standardDeviation; | |
} | |
private ArrayList<Integer> diffData(int average){ | |
ArrayList<Integer> newList = new ArrayList<Integer>(); | |
for(int i=0; i<ListOfP.size(); i++) | |
{ | |
int oe = (ListOfP.get(i)-average); | |
newList.add(oe*oe); | |
} | |
return newList; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment