Java Reference
In-Depth Information
Exercises
1. Write a method called lastIndexOf that accepts an array of integers and an integer value as its parameters and returns
the last index at which the value occurs in the array. The method should return -1 if the value is not found. For exam-
ple, in the array {74, 85, 102, 99, 101, 85, 56}, the last index of the value 85 is 5.
2. Write a method called range that returns the range of values in an array of integers. The range is defined as 1 more
than the difference between the maximum and minimum values in the array. For example, if an array called list
contains the values {36, 12, 25, 19, 46, 31, 22}, the call of range(list) should return 35 (46
12
1). You may
assume that the array has at least one element.
3. Write a method called countInRange that accepts an array of integers, a minimum value, and a maximum value as
parameters and returns the count of how many elements from the array fall between the minimum and maximum
(inclusive). For example, in the array {14, 1, 22, 17, 36, 7,
43, 5}, for minimum value 4 and maximum value 17,
there are four elements whose values fall between 4 and 17 .
4. Write a method called isSorted that accepts an array of real numbers as a parameter and returns true if the list is in
sorted (nondecreasing) order and false otherwise. For example, if arrays named list1 and list2 store {16.1, 12.3,
22.2, 14.4} and {1.5, 4.3, 7.0, 19.5, 25.1, 46.2} respectively, the calls isSorted(list1) and isSorted(list2)
should return false and true respectively. Assume the array has at least one element. A one-element array is consid-
ered to be sorted.
5. Write a method called mode that returns the most frequently occurring element of an array of integers. Assume that
the array has at least one element and that every element in the array has a value between 0 and 100 inclusive. Break
ties by choosing the lower value. For example, if the array passed contains the values {27, 15, 15, 11, 27}, your
method should return 15 . ( Hint : You may wish to look at the Tally program from this chapter to get an idea how to
solve this problem.) Can you write a version of this method that does not rely on the values being between 0 and 100 ?
6. Write a method called stdev that returns the standard deviation of an array of integers. Standard deviation is com-
puted by taking the square root of the sum of the squares of the differences between each element and the mean,
divided by one less than the number of elements. (It's just that simple!) More concisely and mathematically, the
standard deviation of an array a is written as follows:
a . length
-
1
average ( a ) 2 )
3
4 -
( a
i
a
i
=
0
stdev ( a )
= a
a . length
-
1
For example, if the array passed contains the values {1, -2, 4,
4, 9,
6, 16,
8, 25,
10}, your method should
return approximately 11.237 .
7. Write a method called kthLargest that accepts an integer
and an array
as its parameters and returns the ele-
k
a
ment such that
k = 1 , return the
second-largest element, and so on. For example, if the array passed contains the values {74, 85, 102, 99, 101, 56,
84} and the integer
elements have greater or equal value. If
k = 0 , return the largest element; if
k
passed is 2, your method should return 99 because there are two values at least as large as 99
(101 and 102). Assume that 0 k < a .length. (Hint: Consider sorting the array or a copy of the array first.)
k
8. Write a method called median that accepts an array of integers as its parameter and returns the median of the num-
bers in the array. The median is the number that appears in the middle of the list if you arrange the elements in order.
Assume that the array is of odd size (so that one sole element constitutes the median) and that the numbers in the
 
Search WWH ::




Custom Search