Question: Exercise 1 : Java Generics Task 1 : Define a generic class named PairBox that can store a pair of objects of different types. The

Exercise 1: Java Generics
Task 1: Define a generic class named PairBox that can store a pair of objects of different types. The class should have two type parameters: T for the first type and U for the second type. Implement methods to set and get these objects.
Hints:
Use angle brackets to specify the type parameters in your class definition, e.g., class PairBox.
For setting objects, define two methods: one for each type. For getting objects, also define two methods.
Ensure your class has private variables to store the objects and use the constructor to initialize them.
Task 2: Implement a Generic Method
Create a utility class named BoxSwapper. Inside BoxSwapper, implement a static generic method named swap that swaps a second pair of similar elements inside a given PairBox.Due to type safety constraints, the swap method will only work with similar types stored in the PairBox.
Hints:
Remember that generics provide compile-time type safety. When defining generic classes or methods, use type parameter(s) like , where T stands for "Type".
Your method should be defined with type parameters that match those of PairBox.
Use the get and set methods of PairBox to perform the swap
Task 3: Implement a Generic Interface
Define a generic interface named Transformer with one type parameter.
The interface should declare a method named transform that accepts and returns an object of the generic type. Implement this interface in a class, using a specific type (e.g., Integer or String), and provide the logic to transform the input in some way.
Hints
Think about simple transformations, like negating a number or converting a string to uppercase.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!