Method contains takes an array of integers and returns true


Question: Rewrite Exercise by using generics to allow the input array to be of an arbitrary type.

Exercise: Method contains takes an array of integers and returns true if there exists any item in the array that satisfies a specified condition. For instance, in the following code fragment:

int [] input = { 100, 37, 49 };

boolean result1 = contains( input, new Prime() );

boolean result2 = contains( input, new PerfectSquare() );

boolean result3 = contains( input, new Negative() );

The intended result is that result1 is true because 37 is a prime number, result2 is true because both 100 and 49 are perfect squares, and result3 is false because there are no negative numbers in the array. Implement the following components:

a. An interface that will be used to specify the second parameter to contains.

b. The contains method (which is a static method).

c. The classes Negative, Prime, and PerfectSquare.

Solution Preview :

Prepared by a verified Expert
Basic Computer Science: Method contains takes an array of integers and returns true
Reference No:- TGS02457414

Now Priced at $15 (50% Discount)

Recommended (92%)

Rated (4.4/5)