Week 5 (HashSets)

Watch video (Intro to HashSets)

Yes, week1/test1.java does give duplicates. Point it at this website to see.

A difference between a list and a set is that lists contain duplicates but set don't. Study week2/test2.java. Notice the use of HashSet. Look up HashSet.

  1. Build up a class of useful non-destructive functions on ArrayLists, compile it and put it in your CLASSPATH. lists.java. Keep ading to this class as you think of more useful functions.

  2. Write a program for testing out your list methods. testList.java.

  3. Build up a class of useful non-destructive functions on HashSets, compile it and put it in your CLASSPATH. sets.java. Keep ading to this class as you think of more useful functions.

    Watch video (HashSets)

  4. Write a program for testing out your list methods. testSet.java.

  5. (Easy Assignment) Write a program which takes two command line arguments and prints out the sets consisting of the union, intersection and set difference of the set of characters in the two strings You must use the methods in you sets.java class. e.g.
    java setPlay asda asasdaqweqe
    
    will output:
    [w, d, e, s, q, a]
    [d,s,a]
    []
    
    (The order may be different)

    Hint:

    class setPlay
    {
    
    	static HashSet <Character> stringToSet(String s)
    	{
    		HashSet <Character> s1= new HashSet();
    		for (int i=0;i<s.length();i++)
    		{
    			//add the ith element of s to the set s1
    		
    		} 
    		return s1;
    	
    	}
    
    	public static void main(String [] args)
    	{
    		HashSet <Character> a1 = stringToSet(args[0]);
    		HashSet <Character> a2 = stringToSet(args[1]);
    		//set b to the union of a1 and a2.
    		//set c to the intersection of a1 and a2.
    		//set d to the difference of a1 and a2.
    		System.out.println(b);System.out.println(c);System.out.println(d);
    	}
    }
    
    Upload your assignment to directory IS52013B-assignments-2010-11 or to IS52014B-assignments-2010-11 depending on your course. Watch video (Help with Easy Assignment 2)

  6. Write a program that takes two URLs on the command line and prints out the links which occur on either of the pages (union). Hint: use addAll. Solution.

  7. Write a program that takes two URLs on the command line and prints out the links which occur on both pages (intersection). Solution. on the Department of Computing web site.

  8. Write a program that takes two URLs on the command line and prints out the links which occur in the first page but not in the second. Solution.

  9. Try java setDiff http://www.doc.gold.ac.uk http://www.doc.gold.ac.uk. Why isn't this empty?!

  10. Find all the links that are both on Goldsmiths site and the Department of Computing web site.

  11. Find all the links that are both on Goldsmiths site but not
  12. Write a program which takes a URL and finds all the links contained in that page and then for each of those URLs finds the links in in that page. It should print all the links out. Do it by writing a method which takes a String representing a URL as a parameter and returns a HashSet of all the links visited. (Here we are printing out all the links at Depth 1 and 2 from a given URL.) Solution. (Notice the use of Generics)

  13. Can week2/solutions/crawler2.javavisit the same link more than once? Explain your answer.

  14. (Hard assignment) Do the same (without using recursion), but this time print out all the URLs at depth 1, 2 and 3. Solution.

Week 2 Resources

s.danicic@gold.ac.uk
Sebastian Danicic BSc MSc PhD (Reader in Computer Science)
Dept of Computing, Goldsmiths, University of London, London SE14 6NW
Last updated 2015-09-04