I'm a pretty good programmer. I have read through half of the SCJP book and when I took the test at JavaOne (for free, thankfully) I only scored a 60% on it.
What I learned from taking this exam is that they are going to try to trick you as much as possible. The only way to combat this is to start trying to code silly things and see what happens.
class Foo {
public void doSomething() { System.out.println("Foo"); }
}
class MyFoo extends Foo {
public void doSomething() { System.out.println("MyFoo"); }
}
public class MyClass extends MyFoo {
Foo f;
static {
f = new MyFoo().doSomething();
}
public static void main(String args[]) {
new MyClass();
}
}
Create stupid examples like the one above and see what prints out. Then try playing around with Sets, TreeSets and all other kinds of Sets. Then do the same with Maps and all of the subsets of Maps. I came up with the following class that was taken from snipits from others playing with sets.
public class PlayingWithSets {
private Set<Integer> s1;
private Set<Integer> s2;
enum Test {
A,B,C
};
/*
* Cannot be a static block because the variable is not static. This block
* happens before the call to super() in the constructor.
*/
{
s1 = generateSet();
s2 = generateSet();
}
/**
* Helper method to set up a new HashSet
* @return
*/
private Set<Integer> generateSet() {
Set<Integer> s = new HashSet<Integer>();
Random r = new Random();
for (int i = 0; i < 20; ++i) {
s.add(r.nextInt(30));
}
return s;
}
/* ********************** Merges two sets *****************************/
private void mergeSets() {
System.out.println("Set s1 = " + s1);
System.out.println("Set s2 = " + s2);
/*
* Causes an error if you use the wildcard for the generic type. I.E.
* Set<?> s1; in the declaration.
*
* The cast is needed when using wild cards for declaration. The reason
* is that you cannot gurantee that the object is of the same type,
* which defeats the purpose of generics and type safety.
*/
s1.addAll(s2);
System.out.println(s1);
}
/* ************************ Sorting on a set ***************************/
private void sortSets() {
/*
* Collections.sort() is ONLY for lists.
*/
// Collections.sort(s1);
TreeSet<Integer> ts = new TreeSet<Integer>(s1);
System.out.println("Sorted set s1 = " + ts);
}
/* ******************** Tests the Uniqueness of sets (i.e. no duplicates) **************************/
static void fill(Set s) {
s.addAll(Arrays.asList("one two three four five six seven".split(" ")));
}
public static void testSetUniqueness(Set s) {
// Strip qualifiers from class name:
System.out.println(s.getClass().getName().replaceAll("\\w+\\.", ""));
fill(s);
fill(s);
fill(s);
System.out.println(s); // No duplicates!
// Add another set to this one:
s.addAll(s);
s.add("one");
s.add("one");
s.add("one");
System.out.println(s);
// Look something up:
System.out.println("s.contains(\"one\"): " + s.contains("one"));
}
/* ****************** Subset / Union / Intersection **********************/
public static <T> Set<T> union(Set<T> setA, Set<T> setB) {
Set<T> tmp = new TreeSet<T>(setA);
tmp.addAll(setB);
return tmp;
}
public static <T> Set<T> intersection(Set<T> setA, Set<T> setB) {
Set<T> tmp = new TreeSet<T>();
for (T x : setA)
if (setB.contains(x))
tmp.add(x);
return tmp;
}
public static <T> Set<T> difference(Set<T> setA, Set<T> setB) {
Set<T> tmp = new TreeSet<T>(setA);
tmp.removeAll(setB);
return tmp;
}
public static <T> Set<T> symDifference(Set<T> setA, Set<T> setB) {
Set<T> tmpA;
Set<T> tmpB;
tmpA = union(setA, setB);
tmpB = intersection(setA, setB);
return difference(tmpA, tmpB);
}
public static <T> boolean isSubset(Set<T> setA, Set<T> setB) {
return setB.containsAll(setA);
}
public static <T> boolean isSuperset(Set<T> setA, Set<T> setB) {
return setA.containsAll(setB);
}
private void subsetUnionIntersection() {
TreeSet<Character> set1 = new TreeSet<Character>();
TreeSet<Character> set2 = new TreeSet<Character>();
set1.add('A');
set1.add('B');
set1.add('C');
set1.add('D');
set2.add('C');
set2.add('D');
set2.add('E');
set2.add('F');
System.out.println("set1: " + set1);
System.out.println("set2: " + set2);
System.out.println("Union: " + union(set1, set2));
System.out.println("Intersection: " + intersection(set1, set2));
System.out.println("Difference (set1 - set2): "
+ difference(set1, set2));
System.out
.println("Symmetric Difference: " + symDifference(set1, set2));
TreeSet<Character> set3 = new TreeSet<Character>(set1);
set3.remove('D');
System.out.println("set3: " + set3);
System.out.println("Is set1 a subset of set2? " + isSubset(set1, set3));
System.out.println("Is set1 a superset of set2? "
+ isSuperset(set1, set3));
System.out.println("Is set3 a subset of set1? " + isSubset(set3, set1));
System.out.println("Is set3 a superset of set1? "
+ isSuperset(set3, set1));
}
/* ************************ ***************************/
/**
* @param args
*/
public static void main(String[] args) {
/*
* Testing different types of sets
*/
testSetUniqueness(new HashSet());
testSetUniqueness(new TreeSet());
testSetUniqueness(new LinkedHashSet());
Test test = new Test();
Test values[] = test.values();
System.out.println("\nValues: " + values);
for(Test t: values) {
System.out.println("T: " + t.toString());
}
PlayingWithSets p = new PlayingWithSets();
p.mergeSets();
p.sortSets();
p.subsetUnionIntersection();
}