Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

- Give an example of a program that uses the nongeneric version of a class from

ID: 3671869 • Letter: #

Question

- Give an example of a program that uses the nongeneric version of a class from the STL and the equivalent program that uses the generic version. How do the two implementations differ? Why is having the syntax of generics better?

- Implement a generic method that returns the minimum element in an array.

       public static<EextendsComparable<E>> E min(E[ ] list)

- Write a generic method that returns the maximum element in a two-dimensional array.

public static <E extends Comparable<E>> E max(E[ ][ ] list)

- Write a generic method that returns the largest element in an ArrayList:

public static <E extends Comparable<E>> E max(ArrayList<E> list)

- Write a generic method that returns a new ArrayList. The new list contains the non-duplicate elements from the original list.

public static <E> ArrayList<E> removeDuplicates(ArrayList<E> list)

Please provide a demo programs that used these methods ( You can have a demo for each method)

Example:

(Maximum element in an array)
Implement a generic method that returns the maximum element in an array.
public static <E extends Comparable<E>> E max(E[ ] list)


Implementation:

/*

Maximum element in an array.
Implement the following method that returns the maximum element in an array.
public static <E extends Comparable<E>> E max(E[ ] list)
*/
public class Demo_max {
public static void main(String[] args) {
Integer[] numbers = { 1, 2, -3, 7, 5, 4 , 11,12, 87, 23, -1, 0, 3};
for (int i=0;i<numbers.length; i++) {
System.out.print(numbers [i] +" ");
}
System.out.println();
System.out.println("Max= "+max(numbers));
}

public static <E extends Comparable<E>> E max(E[] list) {
E max = list[0];
for (int i = 1; i < list.length; i++) {
if (max.compareTo(list[i]) < 0) {
max = list[i];
}
}
return max;
}
}

Explanation / Answer

Hi below i written a sample code that explains about the Generic and Non generic style and the difference on them,

For generic Style:

final class HtmlProcessor {
        public static Collection<String> process( Collection<URI> files) {
            Collection<String> imageFileNames = new TreeSet<String>();
            for (URI uri : files) {
                Collection<HtmlToken> tokens = HtmlTokenizer.tokenize(new File(uri));
                imageFileNames.addAll(ImageCollector.collect(tokens));
            }
            return imageFileNames;
        }
    }
    final class ImageCollector {  
        public static Collection<String> collect( Collection<HtmlToken> tokens) {
            Set<String> images = new TreeSet<String>();
            for (HtmlToken tok : tokens) {
                if (tok.getTag().equals("img") && tok.hasAttribute("src")) {
                    Attribute attr = tok.getAttribute("src");
                    images.add(attr.getValue());
                }  
            }
            return images;
        }
    }

For generic style we can easily tell what type of elements are stored in the various collections. This is one of the benefits of generic Java: the source code is substantially more expressive and captures more of the programmer's intent. In addition it enables the compiler to perform lots of type checks at compile time that would otherwise be performed at runtime.

For non generic style:

final class HtmlProcessor {
        public static Collection process( Collection files) {
            Collection imageFileNames = new TreeSet();
            for (Iterator i = files.iterator(); i.hasNext(); ) {
                URI uri = (URI) i.next();
                Collection tokens = HtmlTokenizer.tokenize(new File(uri));
                imageFileNames.addAll(ImageCollector.collect(tokens));  // unchecked warning
            }
            return imageFileNames;
        }
    }
    final class ImageCollector {
        public static Collection collect( Collection tokens) {
            Set images = new TreeSet();
            for (Iterator i = tokens.iterator(); i.hasNext(); ) {
                HtmlToken tok = (HtmlToken) i.next();
                if (tok.getTag().3("img") && tok.hasAttribute("src")) {
                    Attribute attr = tok.getAttribute("src");
                    images.add(attr.getValue());       // unchecked warning
                }  
            }
            return images;
        }
    }

For non generic code ,it is relatively difficult to tell what the various collections contain. This is typical for non-generic code. The raw type collections do not carry information regarding their elements. This lack of type information also requires that we cast to the alledged element type each time an element is retrieved from any of the collections. Each of these casts can potentially fail at runtime with a ClassCastException .ClassCastException s are a phenomenon typical to non-generic code.

To Implement a generic method that returns the minimum and maximum element consider the following example,