} public void add(E element) { elements[n] = elementToAdd; n++; } // . . . public E get(int index) { return (E)elements[index]; } } import static org.junit.Assert.*; import org.junit.Test; public class ArrayBagTest { String Integer // Test highlighting passing type arguments and @Test public void testGenericity() { ArrayBag<String> names = new ArrayBag<String>(); names.add("Taylor"); names.add("Kim"); ArrayBag<Integer> testScores = new ArrayBag<Integer>(); testScores.add(99); testScores.add(77); testScores.add(88); } } In the method, is replaced with so only elements of the type argumentString, or add Object E abovecan be added to that collection. Integer The compiler catches accidental messages using the wrong type of argument. For add example, the following three attempts to add the wrong type generate errors (not compiletime runtime errors later. This is a good thing. It is better than waiting until runtime when the program would otherwise terminate. names.add(999); The method add(String) in the type ArrayBag<String> is not applicable for the arguments (int) testScores.add("a String"); The method add(Integer) in the type ArrayBag <Integer> is not applicable for the arguments (String) You also have to manually box primitives during messages. don’t add testScores.add(new Integer(89)); Nor do you have to cast the return values names.add("First"); names.add("Second"); String noCastToStringNeeded = strings.get(0); noCastToStringNeeded = strings.get(1); because the cast is now done in the method. get
} public void add(E element) { elements[n] = elementToAdd; n++; } // . . . public E get(int index) { return (E)elements[index]; } } import static org.junit.Assert.*; import org.junit.Test; public class ArrayBagTest { // Test highlighting passing type arguments String and Integer @Test public void testGenericity() { ArrayBag<String> names = new ArrayBag<String>(); names.add("Taylor"); names.add("Kim"); ArrayBag<Integer> testScores = new ArrayBag<Integer>(); testScores.add(99); testScores.add(77); testScores.add(88); } } In the add method, Object is replaced with E so only elements of the type argumentString, or Integer abovecan be added to that collection. The compiler catches accidental add messages using the wrong type of argument. For example, the following three attempts to add the wrong type generate compiletime errors (not runtime errors later. This is a good thing. It is better than waiting until runtime when the program would otherwise terminate. names.add(999); The method add(String) in the type ArrayBag<String> is not applicable for the arguments (int) testScores.add("a String"); The method add(Integer) in the type ArrayBag <Integer> is not applicable for the arguments (String) You also don’t have to manually box primitives during add messages. testScores.add(new Integer(89)); Nor do you have to cast the return values names.add("First"); names.add("Second"); String noCastToStringNeeded = strings.get(0); noCastToStringNeeded = strings.get(1); because the cast is now done in the get method.
227Sum15.docx