Java Reference
In-Depth Information
΢ Exercise R7.2. Write a program that contains a bounds error. Run the
program. What happens on your computer? How does the error message
help you locate the error?
΢΢Exercise R7.3. Write Java code for a loop that simultaneously computes
the maximum and minimum values of an array list. Use an array list of
accounts as an example.
324
325
΢ Exercise R7.4. Write a loop that reads 10 strings and inserts them into an
array list. Write a second loop that prints out the strings in the opposite
order from which they were entered.
΢΢Exercise R7.5. Consider the algorithm that we used for determining the
maximum value in an array list. We set largestYet to the starting
element, which meant that we were no longer able to use the Ȓfor eachȓ
loop. An alternate approach is to initialize largestYet with null , then
loop through all elements. Of course, inside the loop you need to test
whether largestYet is still null . Modify the loop that finds the bank
account with the largest balance, using this technique. Is this approach
more or less efficient than the one used in the text?
΢΢΢Exercise R7.6. Consider another variation of the algorithm for
determining the maximum value. Here, we compute the maximum value
of an array of numbers.
double max = 0; // Contains an error!
for (x : values)
{
if (x > max) max = x;
}
However, this approach contains a subtle error. What is the error, and
how can you fix it?
΢ Exercise R7.7. For each of the following sets of values, write code that
fills an array a with the values.
a. 1 2 3 4 5 6 7 8 9 10
b. 0 2 4 6 8 10 12 14 16 18 20
Search WWH ::




Custom Search