import java.util.*; public class C { public String makeString() { String initialString = "Hello"; String result = ""; for(int i=0; i < initialString.length(); i++) { result = result + initialString.charAt(i) + "-"; } return result; } public ArrayList getWords(String string) { ArrayList result = new ArrayList(); String currentWord = ""; for(int i=0; i < string.length(); i++) { char c = string.charAt(i); if (c == ' ') { result.add(currentWord); currentWord = ""; } else { currentWord += c; } } if (currentWord.length() > 0) { result.add(currentWord); } return result; } public ArrayList removeDuplicates(ArrayList list) { ArrayList result = new ArrayList(); for(String string: list) { if (!result.contains(string)) { result.add(string); } } return result; } public ArrayList setIntersection(ArrayList a, ArrayList b) { // a and b have duplicates alread removed ArrayList result = new ArrayList(); if (a == null || b == null || a.size() == 0 || b.size() == 0) { return result; } for(String string: a) { if (b.contains(string)) { result.add(string); } } return result; } public ArrayList setUnion(ArrayList a, ArrayList b) { ArrayList result = new ArrayList(); for(String word: a) { result.add(word); } for(String word: b) { result.add(word); } result = removeDuplicates(result); return result; } public double jaccard(String first, String second) throws Exception { // return the result of the intersection over the union if (first == null || second == null) { throw new Exception("Dont use nulls!"); } if (first.equals(second)) { return 1.0; } ArrayList fa = removeDuplicates(getWords(first.toLowerCase())); ArrayList sa = removeDuplicates(getWords(second.toLowerCase())); double numerator = (double)setIntersection(fa, sa).size(); if (numerator == 0.0) { return 0.0; } double denominator = (double)setUnion(fa, sa).size(); if (denominator == 0.0) { return 0.0; } return (numerator / denominator); } public void someMethod(int val) { if (val == 0) { return; } System.out.println("val is not zero"); } public static void main(String args[]) { C c = new C(); c.someMethod(10); //double jaccardValue = -1.0; //try { //jaccardValue = c.jaccard(null, null); //} catch (Exception ex) { //System.out.println(ex.getMessage()); //} //ArrayList val = c.getWords("hello world again something stuff more last "); //int expected = 7; //int result = val.size(); //if (expected != result) { //System.out.println("Expected is not equal to the result"); //System.out.println("Expected: " + expected + ", result: " + result); //} //System.out.println("The Jaccard index is: " + jaccardValue); } }