Question

In: Computer Science

Write a generic method in java code public static double jaccard(HashSet A, HashSet B) that on...

Write a generic method in java code

public static double jaccard(HashSet A, HashSet B)

that on input two sets represented as hash sets, returns their Jaccard similarity.

The following are a few sample runs:

Input :    A=1, 2, 3, 4,   B=2, 4, 6, 8

Return:   0.3333333333333333

Input :    A=Larry, Michael, Shaq, Kobe, LeBron  

                 B=Steve, Kobe, Shaq, LeBron, Steph, Jeremy, Michael

Return:   0.5

Your method must have time complexity On and space complexity O1, where n is the size of the smaller input set.

Solutions

Expert Solution

Program Code Screensshot :

Sample Output :'

'

Program Code to Copy

import java.util.Arrays;
import java.util.HashSet;

class Main{

    public static double jaccard(HashSet A, HashSet B){
        //Find union of both A and B
        HashSet union = new HashSet(A);
        union.addAll(B);
        //common elements/total elements
        return (A.size()+B.size()-union.size())/(union.size()*1d);
    }
    public static void main(String[] args) {
        HashSet A = new HashSet(Arrays.asList(1,2,3,4));
        HashSet B = new HashSet(Arrays.asList(2,4,6,8));
        System.out.println(jaccard(A,B));
        HashSet A1 = new HashSet(Arrays.asList("Larry","Micheal","Shaq","Kobe","LeBron"));
        HashSet B1 = new HashSet(Arrays.asList("Steve","Kobe","Shaq","LeBron","Steph","Jeremy","Micheal"));

        System.out.println(jaccard(A1,B1));
    }
}

Related Solutions

FOR JAVA: Need to write a code for the implementation of this public static method: findLongestPalindrome...
FOR JAVA: Need to write a code for the implementation of this public static method: findLongestPalindrome takes a Scanner scn as its parameter and returns a String. It returns the longest token from scn that is a palindrome (if one exists) or the empty string (otherwise). (Implementation note: You'll find your isPalindrome method helpful here. This method calls for an optimization loop.)
JAVA please write this method public static void recursiveSelectionSort(int[] arr) { }
JAVA please write this method public static void recursiveSelectionSort(int[] arr) { }
JAVA please write this method public static void recursiveMergeSort(int[] arr) { }
JAVA please write this method public static void recursiveMergeSort(int[] arr) { }
Java please. Write a static method sqrt()that takes a double argument and returns the square root...
Java please. Write a static method sqrt()that takes a double argument and returns the square root of that number using newton's method to compute result.
Write in Java: Write a method called: public static String[] noIdenticalCombine(String[] array1, String[] array2) { //...
Write in Java: Write a method called: public static String[] noIdenticalCombine(String[] array1, String[] array2) { // instructions: returns an array that contains all the Strings in array1 and array2 but without repetition. order does not matter, but it will return array1's elements and then array2's element that are not in array1. Assume there are no duplicates are in array1 and array2. Could use count which is how many str there are in array2, where !contains(array1, str). May an array of...
JAVA Given the header of a method public static void m1 (int[ ] max) Write down...
JAVA Given the header of a method public static void m1 (int[ ] max) Write down Java codes to invoke m1 method, declare variables as needed, (Do NOT implement the method)
write a java code Write a generic program to compute the sum of 30 terms of...
write a java code Write a generic program to compute the sum of 30 terms of the following series. (181 - 5)/31 + (186 + 9)/34 - (191 - 13)/37 + (196 + 17)/40 + . . . . . 1 5 11 Note: the 3rd, 6th, 9th term etc, has a negative sign in front of parenthesis. User Input: None Expected output: Term Ratio Sum 1 5.677 5.677 2 5.735 11.413 3 -4.811 6.602
How do you write a Java method that is called : public static String[] noIdenticalCombine(String[] array1,...
How do you write a Java method that is called : public static String[] noIdenticalCombine(String[] array1, String[] array2) { // instructions: returns an array that contains all the Strings in array1 and array2 but without repetition. order does not matter, but it will return array1's elements and then array2's element that are not in array1. Assume there are no duplicates are in array1 and array2. Could use count which is how many str there are in array2, where !contains(array1, str)....
(java)Write a recursive method public static int sumForEachOther(Int n) that takes a positive Int as an...
(java)Write a recursive method public static int sumForEachOther(Int n) that takes a positive Int as an argument and returns the sum for every other Int from n down to 1. For example, sumForEachOther(8) should return 20, since 8+6+4+ 2=20.And the call sumForEachOther(9) should return 25 since 9+7+5 + 3+1-=25. Your method must use recursion.
COMPLETE JAVA CODE public class Point2 { private double x; private double y;    /** *...
COMPLETE JAVA CODE public class Point2 { private double x; private double y;    /** * Create a point with coordinates <code>(0, 0)</code>. */ public Point2() { complete JAVA code this.set(0.0, 0.0); COMPLETE CODE }    /** * Create a point with coordinates <code>(newX, newY)</code>. * * @param newX the x-coordinate of the point * @param newY the y-coordinate of the point */ public Point2(double newX, double newY) { complete Java code this.set(newX, newY); }    /** * Create a...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT