Java Reference
In-Depth Information
A
SortedArrayList
stores a collection. It is similar to
ArrayList
, except
that
add
will place the item in the correct sorted order instead of at the
end (however, at this point it will be difficult for you to use inheri-
tance). Implement a separate
SortedArrayList
class that supports
add
,
get
,
remove
, and
size
.
4.38
Provide a function object that can be passed to
findMax
and which will
order
Strings
using
compareToIgnoreCase
instead of
compareTo
.
4.39
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:
4.40
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
.
Method
transform
takes two identically sized-arrays as parameters:
input
and
output
, and a third parameter representing a function to be
applied to the input array.
For instance, in the following code fragment:
4.41
double [ ] input = { 1.0, -3.0, 5.0 };
double [ ] output1 = new double [ 3 ];
double [ ] output2 = new double [ 3 ];
double [ ] output3 = new double [ 4 ];
transform( input, output1, new ComputeSquare( ) );
transform( input, output2, new ComputeAbsoluteValue( ) );
transform( input, output3, new ComputeSquare( ) );
The intended result is that
output1
contains 1.0, 9.0, 25.0,
output2
contains
1.0, 3.0, 5.0, and the third call to
transform
throws an
IllegalArgumentEx-
ception
because the arrays have different sizes. Implement the following
components:
Search WWH ::
Custom Search