Chapter 6


Exercise 1 The following two declarations are equivalent as far as the compiler is concerned, but one is considered more readable than the other. Which is more readable, and why?

  1. double dubs[];

  2. double[] dubs;

Solution 1 Format B (double[] dubs) is more readable than Format A (double dubs[]) because in B, as with all other declarations, the data type (double[]) comes first, followed by the variable name (dubs). Format A begins with some, but not all, of the data type (double). Then comes the variable name, followed by the remainder of the data type ([]). So Format A is less readable for two reasons: It does not follow the convention of data type followed by variable name, and it splits the data type into two parts.

Exercise 2 Write a line of code that declares an array of 5 ints and initializes the array to contain the first 5 prime numbers. The code should be a single statement.

Solution 2 int[] first5Primes = {2, 3, 5, 7, 11};

Exercise 3 Write a method whose single argument is an array of double. The method should return the average (mean) of the array's components. Write an application that tests the method by passing it an array containing any values you like.

Solution 3 The following code is one possible solution:

public class MeanOfArray {     public static void main(String[] args)     {         double[] theArray = {1.2, 1.3, 1.4, 1.5, 1.6};         double average = computeAverage(theArray);         System.out.println("mean = " + average);     }     static double computeAverage(double[] doubles)     {         double sum = 0;         for (int i=0; i<doubles.length; i++)             sum += doubles[i];         return sum/doubles.length;     } } 

Exercise 4 Write a program that uses the array-averaging method of Question 3. The program should compute and print out the average of an array (you can choose the component values). Then the program should add 100 to each component, and again compute and print out the average.

Solution 4 The following code is one possible solution. The main method is long enough that comments are in order:

public class Question4 {     public static void main(String[] args)     {         // Create the array.         double[] theArray = {1.2, 1.3, 1.4, 1.5, 1.6};         // Compute and print out average.         double average = computeAverage(theArray);         System.out.println("mean = " + average);         // Add 100 to each component.         for (int i=0; i<theArray.length; i++)             theArray[i] += 100;         // Compute and print out new average.         average = computeAverage(theArray);         System.out.println("mean = " + average);     }     static double computeAverage(double[] doubles)     {         double sum = 0;         for (int i=0; i<doubles.length; i++)             sum += doubles[i];         return sum/doubles.length;     } }

Exercise 5 Write a program that contains a method that creates and returns an array of int containing the first n square numbers, where n is the method's argument. Test your method by calling it with n=10. Your program should print out the index and value of each component, in descending order.

Solution 5 The following code is one possible answer. Note that the loop in main decrements its loop counter down to and including 0, because of the requirement that the squares should be printed out in descending order:

public class DescendingSquares {     public static void main(String[] args)     {         int[] squares = createArrayOfSquares(10);         for (int i=squares.length-1; i>=0; i--)             System.out.println(squares[i]);     }     static int[] createArrayOfSquares(int nSquares)     {         int[] squares = new int[nSquares];         for (int i=0; i<nSquares; i++)             squares[i] = i*i;         return squares;     } }

Exercise 6 Write a method that creates a multiplication table. The method should return a two-dimensional array of N by N ints, where N is specified by the method's argument. In the array, the component at [row][col] should have a value of row*col.

Solution 6 The following method creates a multiplication table:

static int[][] makeTable(int n) {   int[][] table = new int[n][n];   for (int i=0; i<n; i++)     for (int j=0; j<n; j++)       table[i][j] = n;   return table; }




Ground-Up Java
Ground-Up Java
ISBN: 0782141900
EAN: 2147483647
Year: 2005
Pages: 157
Authors: Philip Heller

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net