Java Reference
In-Depth Information
4. Write a method called runningTotal that returns a new ArrayIntList that contains a running total of the origi-
nal list. In other words, the i th value in the new list should store the sum of elements 0 through i of the original list.
For example, given a variable list that stores [2, 3, 5, 4, 7, 15, 20, 7] , consider what happens when the
following call is made:
ArrayIntList list2 = list.runningTotal();
The variable list2 should store [2, 5, 10, 14, 21, 36, 56, 63] . The original list should not be changed by
the method. If the original list is empty, the result should be an empty list. The new list should have the same capacity
as the original. Remember that there is a list constructor that accepts a capacity as a parameter.
5. Write a method called fill that accepts an integer value as a parameter and replaces every value in the list with that
value. For example, if a variable called list initially stores [42, -7, 3, 0, 15] and the call of list.fill(2) ;
is made, the list will be changed to store [2, 2, 2, 2, 2] .
6. Write a method called isPairwiseSorted that returns whether or not a list of integers is pairwise sorted. A list is
considered pairwise sorted if each successive pair of numbers is in nondecreasing order. For example, if a variable
called list stores [3, 8, 2, 5, 19, 24, -3, 0, 4, 4, 8, 205, 42] , then the call of
list.isPairwiseSorted() should return true because the successive pairs of this list are all sorted: (3, 8),
(2, 5), (19, 24), (-3, 0), (4, 4), (8, 205) . The extra value 42 at the end had no effect on the result
because it is not part of a pair. If the list had instead stored [7, 42, 308, 409, 19, 17, 2] , then the method
should return false because the pair (19, 17) is not in sorted order. If a list is so short that it has no pairs, then it
is considered to be pairwise sorted.
7. Write a method called maxCount that returns the number of occurrences of the most frequently occurring value in a
sorted list of integers. Because the list will be sorted, all duplicates will be grouped together, which will make it eas-
ier to count duplicates. For example, if a variable called list stores [1, 3, 4, 7, 7, 7, 7, 9, 9, 11, 13,
14, 14, 14, 16, 16, 18, 19, 19, 19] , the call of list.maxCount() should return 4 because the most fre-
quently occurring value ( 7 ) occurs four times. It is possible that there will be a tie for the most frequently occurring
value, but that doesn't affect the outcome because you are just returning the count, not the value. If there are no
duplicates in the list, then every value will occur exactly once and the max count is 1 . If the list is empty, the method
should return 0 .
8. Write a method called longestSortedSequence that returns the length of the longest sorted sequence within a list of
integers. For example, if a variable called list stores [1, 3, 5, 2, 9, 7, 3, 0, 42, 308, 17] , then the call
of list.longestSortedSequence() would return 4 because it is the length of the longest sorted sequence within
this list (the sequence -3, 0, 42, 308 ). If the list is empty, your method should return 0 . Notice that for a nonempty
list the method will always return a value of at least 1 because any individual element constitutes a sorted sequence.
9. Write a method called removeFront that takes an integer n as a parameter and that removes the first n values from a
list of integers. For example, if a variable called list stores [8, 17, 9, 24, 42, 3, 8] and a call of
list.removeFront(4); is made, the list's contents should become [42, 3, 8] . You may assume that the
parameter value passed is between 0 and the size of the list inclusive.
10. Write a method removeAll that accepts an integer value as a parameter and that removes all occurrences of the
given value from the list.
11. Write a method called printInversions that lists all inversions in a list of integers. An inversion is a pair of
numbers in which the first appears before the second in the list, but the first is greater than the second. Thus, for a sorted
list such as [1, 2, 3, 4] there are no inversions at all, and the method would produce no output. Suppose that a
Search WWH ::




Custom Search