Java

TOPIC 18 – GENERICS

 

LESSON WORK

 

 

QUESTION 1


Create a class called Trio that has three private datafields.  Their type is undetermined and is set via a generic parameter.  Include a constructor and set and get methods.

Create a Tester class that will create a few Trio objects with different types such as String, Point, Integer, Double, …

 

Note: You can easily base yourself on the example in the lesson.

 

QUESTION 2 (IF TIME PERMITS)

 

Below are some built-in generic classes that create data structures.  The value of T represents the type of data that will be stored in the structure.

 

LinkedList<T>
ArrayList<T>

HashMap<T>

TreeMap<T>

 

Try creating one of the above structures with Integer objects.  Can figure out how to add some data into them?