" on a line by itself. For example, consider the following input file: This is an example of an input file with four different paragraphs.
The second paragraph is the longest with three lines, so your method should return 3 when processing this file.
The third paragraph was empty. This one is just short.
The method should count the number of lines in each paragraph and report that information to System.out. For example, if the input above is stored in a Scanner called input and we make the following call: int max = analyzeParagraphs(input); It should produce the following output: 2-line paragraph 3-line paragraph 0-line paragraph 1-line paragraph It would assign max the value 3 because the method returns the maximum number of lines in any given paragraph. You must exactly reproduce the format of this output. You may assume that the input file has no blank lines, that it contains at least one paragraph, and that each paragraph is terminated by a line containing just "
".
6. Arrays, 10 points. Write a static method called numUnique that takes a
sorted array of integers as a parameter and that returns the number of
unique values in the array. The array is guaranteed to be in sorted order,
which means that duplicates will be grouped together. For example, if a
variable called "list" stores the following values:
[5, 7, 7, 7, 8, 22, 22, 23, 31, 35, 35, 40, 40, 40, 41]
then the following call:
numUnique(list)
should return 9 because this list has 9 unique values (5, 7, 8, 22, 23, 31,
35, 40 and 41). It is possible that the list might not have any
duplicates. For example if list instead stored this sequence of values:
[1, 2, 11, 17, 19, 20, 23, 24, 25, 26, 31, 34, 37, 40, 41]
then a call on the method would return 15 because this list contains 15
different values.
If passed an empty list, your method should return 0. Remember that you can
assume that the values in the array appear in sorted (nondecreasing) order.
You are not allowed to use an auxiliary data structure such as a temporary
array or ArrayList to solve this problem and you are not allowed to call any
methods of the Arrays class or the Collections class.
7. ArrayList, 10 points. Write a static method called removeAdjacentMatches
that removes extra copies of a value that are adjacent to it in an ArrayList
of integers. For example, if a variable called list contains the following:
[1, 3, 3, 15, 2, 2, 2, 2, 1, 19, 3, 42, 42, 42, 7, 42, 1]
and we make the following call:
removeAdjacentMatches(list);
Then list should store the following values after the call:
[1, 3, 15, 2, 1, 19, 3, 42, 7, 42, 1]
Notice that the two occurrences of 3 that appear next to each other have
been replaced with a single occurrence of 3, the four occurrences of 2 that
appear next to each other have been replaced with a single 2, and the three
occurrences of 42 that appear next to each other have been replaced with a
single 42. Notice, however, that the list still contains three occurrences
of 1, two occurrences of 3, and two occurrences of 42. That is because
those duplicate values do not appear right next to each other in the list.
You may not construct any extra data structures to solve this problem. You
must solve it by manipulating the ArrayList you are passed as a parameter.
8. Critters, 15 points. Write a class called Eagle that extends the Critter
class. The instances of the Eagle class switch back and forth between red
and blue in an increasing pattern. They should be red for their first move
and then blue for one move. Then they should be red for two moves and then
blue for two moves. Then they should be red for three moves and then blue
for three moves. Then they should be red for four moves and then blue for
four moves. And so on, each time increasing the number of moves at a
particular color by one. They should be displayed using a less-than
character followed by a greater-than character (<>). They should always hop
whenever possible. If they can't hop, they should turn right if there is a
wall in front of them and otherwise they should infect.
9. Arrays, 15 points. Write a static method called reverseSublist that takes
as parameters an array of integers and a "from index" (inclusive) and "to
index" (exclusive) and that returns a new array containing the specified
sublist in reverse order. For example, suppose that an array called list
stores the following values:
[0, 1, 2, 3, 4, 5, 6, 7]
and we make the following call:
int[] result = reverseSublist(list, 2, 5);
The sublist specified goes from index 2 (inclusive) to index 5 (exclusive).
In other words, it includes the values [2, 3, 4] from the list. The method
should construct and return a new array that contains this sequence of
values in reverse order:
[4, 3, 2]
This first example used an array of sequential integers, but the array might
contain any values. If the list instead stores these values:
[34, 9, -8, 17, 4, 32, 9]
and we make the following call:
int[] result = reverseSublist(list, 1, 5);
the method should instead return an array containing this sequence:
[4, 17, -8, 9]
You may assume that the two index values are legal:
0 <= (from index) <= (to index) <= (array length)
If the from-index and to-index are equal, your method should return an array
of length 0.
Your method should not change the array passed as a parameter. You are not
allowed to solve this problem using an ArrayList and you are not allowed to
call any methods of the Arrays class or the Collections class.
10. Programming, 10 points. Write a static method called removeZeros that
takes an array of integers as a parameter and that moves any zeros in the
array to the end of the array, otherwise preserving the order of the list.
For example, if a variable called "list" stores the following values:
[7, 2, 3, 0, 4, 6, 0, 0, 13, 0, 78, 0, 0, 19, 14]
then the call:
removeZeros(list);
should rearrange the values in the array so that it stores the following:
[7, 2, 3, 4, 6, 13, 78, 19, 14, 0, 0, 0, 0, 0, 0]
Notice that the six zeros have been moved to the end of the array and the
other values are in the same order as in the original list.
You are not allowed to use an auxiliary data structure such as a temporary
array or ArrayList to solve this problem and you are not allowed to call any
methods of the Arrays class or the Collections class.
11. Art, 1 point (bonus). You've programmed lions, bears, giants, and huskies,
but what kind of critter would your TA be? Give an artistic rendering of
how you see your TA fitting into the unforgiving world of the critter
simulation. You may either write a poem or draw a picture. If your answer
below indicates at least 1 minute of effort, you will receive full credit,
although your TA would probably appreciate it if you put in a little more
effort.
CSE142 Final Cheat Sheet
Math Method Description ArrayList Types
-------------------------------------- -----------------
Math.abs(n) absolute value of n ArrayList