0% found this document useful (0 votes)
4 views1 page

Lab 1

The document outlines three exercises focused on creating and demonstrating generic classes and methods in programming. Exercise 1 involves creating a Box class for storing any object type, Exercise 2 involves a Tuple class for holding multiple types of values, and Exercise 3 requires a generic method to swap elements in an array. Each exercise includes instructions for implementation and demonstration with various data types.

Uploaded by

eminyancar691
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views1 page

Lab 1

The document outlines three exercises focused on creating and demonstrating generic classes and methods in programming. Exercise 1 involves creating a Box class for storing any object type, Exercise 2 involves a Tuple class for holding multiple types of values, and Exercise 3 requires a generic method to swap elements in an array. Each exercise includes instructions for implementation and demonstration with various data types.

Uploaded by

eminyancar691
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

EXERCISE 1 (GENERIC CLASS):

Create a generic class named Box that can store any type of object. The Box
class should have methods to put an object into the box (put) and retrieve the
object from the box (get). Additionally, implement a method printContents that
prints the contents of the box. Then, create a simple program to demonstrate the
usage of this generic class with different types of objects.

EXERCISE 2 (GENERIC CLASS – MULTIPLE


TYPES):
Let's create a generic class named Tuple that represents a tuple of values of any
type with multiple type parameters. We can use it to represent a tuple containing
various pieces of information, such as a person's name, age, and occupation.

Implement methods to set and get the values of the tuple.

Implement a simple program to demonstrate the usage of this generic class with
different types of items.

- Create a Tuple for person's name (String), age (Integer), and occupation
(String)
- Create a Tuple for product ID (String), price (Double), and availability
(Boolean)

EXERCISE 3 (GENERIC METHOD):


Create a generic method named swap that takes an array and two indices as
input and swaps the elements at those indices in the array. Array can be of any
type T. Implement a simple program to demonstrate the usage of this generic
method with different types of arrays (Integer, String, and Double). Create
instances of arrays for each type, perform swaps using the swap method, and
then print out the arrays before and after the swaps to verify the results.

You might also like