Exercises


Note

The solutions to these exercises are in Appendix B.

  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;

  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.

  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.

  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.

  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.

  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.




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