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

Practical 8

Uploaded by

Chavini Hewage
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)
12 views1 page

Practical 8

Uploaded by

Chavini Hewage
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

BSc (Hons) in Information Technology

Year 2
Lab Exercise 8

IT2030 – Object Oriented Programming Semester 1, 2020

Objectives:

Write Programs that make uses Generics


Exercise 1

The bubble sort algorithm allows you to sort data. The following code shows how to sort an integer
array using bubble sort.

void bubbleSort(int arr[])


{
int n = arr.length;
for (int i = 0; i < n-1; i++)
for (int j = 0; j < n-i-1; j++)
if (arr[j] > arr[j+1])
{
// swap arr[j+1] and arr[i]
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}

(a) Write a main function to call the bubbleSort() to sort an array of 10 elements
(b) Rewrite the above class using generics so that the solution works for any kind of data.
(c) Rewrite the main function that uses the generic sort class to sort arrays containing
a. Float array
b. Double array
(d) Rewrite the bubbleSort() as a generic method and use it in the code as a generic method

Exercise 2

Implement a generic method called print which can print any value type. The message will be string.

Below is a version of the print() message that works only for integers.

void print(String message, int val)

e.g. of usage

print(“Age is “, age)  Age is : 24

You might also like