Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
1. Consider the following classes:public class AnimalHouse<E> { private E animal; public void setAnimal(E x) { animal = x; } public E getAnimal() { return animal; } } public class Animal{ } public class Cat extends Animal { } public class Dog extends Animal { }For the following code snippets, identify whether the code:
- fails to compile
- compiles with a warning
- generates an error at runtime
- none of the above (compiles and runs without problem)
a. AnimalHouse<Animal> house = new AnimalHouse<Cat>();
b. AnimalHouse<Dog> house = new AnimalHouse<Animal>();
c. AnimalHouse<?> house = new AnimalHouse<Cat>(); house.setAnimal(new Cat());
d. AnimalHouse house = new AnimalHouse(); house.setAnimal(new Dog());
1. Write an example that sorts a set of predefined strings based on string length. Use the version of theCollections.sort
method that takes aComparator
parameter.
Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
Copyright 1995-2005 Sun Microsystems, Inc. All rights reserved.