Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Generics is such a powerful tool that it enables us to use types (classes and interfaces) as parameters to our methods. Type parameters provide a

Generics is such a powerful tool that it enables us to use types (classes and interfaces) as parameters to our methods. Type parameters provide a way for you to re-use the same code with different inputs. This means that we can transform our previously written algorithms so that they now perform on several types of inputs like Integers, Doubles, Strings etc. rather than just ints or floats. In this exercise, you shall have your first taste of writing a generic method in java which lets you swap the positions of two different elements in an array. Notice how the type of the array has been abstracted away, this is because the type is generic. Create a class `Exchange` that implements the method `public static  T[] swap(T[] a, int i, int j)`. The method takes in a generic array along with two integers `i` and `j` representing the positions of the elements to be swapped. It then returns the updated array as the output. Note: In case the positions of the elements to be swapped are invalid, the input array should remain unchanged. Sample Input 1 Sample Output 1 {1, 3, 4, 5} {4, 3, 1, 5} 0 2 Sample Input 2 Sample Output 2 {1, 3, 4, 5} {1, 3, 4, 5} 5 2 Sample Input 3 Sample Output 3 {8, 4, 1, 9} {8, 4, 1, 9} -1 3

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Excel As Your Database

Authors: Paul Cornell

1st Edition

1590597516, 978-1590597514

More Books

Students also viewed these Databases questions