Chapter 6

  1. Initialize a 10-element integer array to the values 1 to 10. Print the array using toString, a for loop, a for-each loop, and the toString() method in the java.util.Arrays class.

  2. Initialize a 10-element integer ArrayList to the values 1 to 10. Print the array using toString, a for loop, and a for-each loop and an Iterator.

  3. Initialize a 10-element integer LinkedList to the values 1 to 10. Print the list using toString, a for loop, and a for-each loop and a ListIterator.

  4. Initialize a 5x5 integer array-matrix to the values 1 to 25. Print the matrix using for loops, for-each loops, and the deepToString() method in the java.util.Arrays class.

  5. Initialize a 5x5 integer ArrayList-matrix to the values 1 to 25. Print the array using toString, for loops and for-each loops.

  6. Implement and test a function int[] maxMin(int[] ar) that simultaneously computes both the maximum and minimum of an integer array. The return value is a two-element array [max, min]. The program should then print the array, max and min.

  7. Implement and test a function double[] solve(double a, double b, double c) that calculates and returns the roots of a quadratic equation. The function returns [root1, root2] for two real roots and [-b/2a, root1, root2] for two imaginary roots.

  8. A run is a sequence of adjacent repeated values. Write a method int index(int[] ar) for finding the index of the first longest run. For example, the longest run index in the array with elements
    1 2 5 5 3 1 2 4 3 2 2 2 2 3 6 5 5 6 3 1 first longest run has index 9.

  9. Implement and test method int[][] magic(int n) for the following algorithm to construct magic n × n squares; it works only if n is odd. A magic square is constructed such that each vertical, horizontal, and diagonal row add up to the same value.

Set row = n - 1, column = n / 2.

For k = 1 ... n * n

Place k at [row][column]. Increment row and column.

If the row or column is n, replace it with 0.

If the element at [row][column] has already been filled

Set row and column to their previous values.

Decrement row.

Here is the 5 × 5 square that you get if you follow this method:

11 18 25 2 9

10 12 19 21 3

4 6 13 20 22

23 5 7 14 16

17 24 1 8 15

  1. Search the Internet for a recursive merge sort algorithm. Implement and test it for a set of random values.

  2. The sieve of Eratosthenes is an ancient algorithm to find all primes in the range 2 through n. A prime number is divisible only by itself and one. Initialize a LinkedList of integers to the values 2 to n. For an integer n, no factor can be larger than sqrt(n). Start with i=0 take the value of that element, scan the list and remove all elements divisible by that number. Then increment i by one, take that value and repeat the scan removal. Continue until the ith element is > sqrt(n).

Example n = 17 sqrt(17) = 4

i = 0--2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 divide by 2

i = 1--2 3 5 7 9 11 13 15 17 divide by 3

i = 2--2 3 5 7 11 13 17 divide by 5

i = 3-- 7 > 4 stop