Exercises


[Page 465]

Note

For programming exercises, first draw a UML class diagram describing all classes and their inheritance relationships and/or associations.


Exercise 9.1

Explain the difference between the following pairs of terms:

  1. An element and an element type.

  2. A subscript and an array element.

  3. A one-dimensional array and two-dimensional array.

  4. An array and a vector.

  5. A insertion sort and a selection sort.

  6. A binary search and a sequential search.

Exercise 9.2

Fill in the blanks.

  1. The process of arranging an array's elements into a particular order is known as ____.

  2. One of the preconditions of the binary search method is that the array has to be ____.

  3. An ____ is an object that can store a collection of elements of the same type.

  4. An ____ is like an array except that it can grow.

  5. For an array, its ____ is represented by an instance variable.

  6. An expression that can be used during array instantiation to assign values to the array is known as an ____.

  7. A ____ is an array of arrays.

  8. A sort method that can be used to sort different types of data is known as a ____ method.

  9. To instantiate an array you have to use the ____ operator.

  10. An array of objects stores ____ to the objects.

Exercise 9.3

Make each of the following array declarations:

  1. A 4 x 4 array of doubles.

  2. A 20 x 5 array of Strings.

  3. A 3 x 4 array of char initialized to '*';

  4. A 2 x 3 x 2 array of boolean initialized to true.

  5. A 3 x 3 array of Students.

  6. A 2 x 3 array of Strings initialized to "one", "two", and so on.

Exercise 9.4

Identify and correct the syntax error in each of the following expressions:

  1. int arr = new int[15];

  2. int arr[] = new int(15);

  3. float arr[] = new [3];

  4. float arr[] = new float {1.0,2.0,3.0};

  5. int arr[] = {1.1,2.2,3.3};

  6. int arr[][] = new double[5][4];

  7. int arr[][] = { {1.1,2.2}, {3.3, 1} };


[Page 466]
Exercise 9.5

Evaluate each of the following expressions, some of which may be erroneous:

int arr[] = { 2,4,6,8,10 }; 


  1. arr[4]

  2. arr[ arr.length ]

  3. arr[ arr[0] ]

  4. arr[ arr.length / 2 ]

  5. arr[ arr[1] ]

  6. arr[ 5 % 2 ]

  7. arr[ arr[ arr[0] ] ]

  8. arr[ 5 / 2.0 ]

  9. arr[ 1 + (int) Math.random() ]

  10. arr[ arr[3] / 2 ]

Exercise 9.6

What would be printed by the following code segment?

int arr[] = { 24, 0, 19, 21, 6, -5, 10, 16}; for (int k = 0; k < arr.length; k += 2)   System.out.println( arr[k] ); 


Exercise 9.7

What would be printed by the following code segment?

int arr[][] = { {24, 0, 19}, {21, 6, -5}, {10, 16, 3}, {1, -1, 0} }; for (int j = 0; j < arr.length; j++)   for (int k = 0; k < arr[j].length; k++)     System.out.println( arr[j][k] ); 


Exercise 9.8

What would be printed by the following code segment?

int arr[][] = { {24, 0, 19}, {21, 6, -5}, {10, 16, 3}, {1, -1, 0} }; for (int j = 0; j < arr[0].length; j++)     for (int k = 0; k < arr.length; k++)         System.out.println(arr[k][j]); 


Exercise 9.9

What's wrong with the following code segment, which is supposed to swap the values of the int variables, n1 and n2?

int temp = n1; n2 = n1; n1 = temp; 



[Page 467]
Exercise 9.10

Explain why the following method does not successfully swap the values of its two parameters? Hint: Think about the difference between value and reference parameters.

public void swapEm(int n1, int n2) {     int temp = n1;     n1 = n2;     n2 = temp; } 


Exercise 9.11

Declare and initialize an array to store the following two-dimensional table of values:

1   2   3   4 5   6   7   8 9  10  11  12 


Exercise 9.12

For the two-dimensional array you created in the preceding exercise, write a nested for loop to print the values in the following order: 1 5 9 2 6 10 3 7 11 4 8 12. That is, print the values going down the columns instead of going across the rows.

Exercise 9.13

Define an array that would be suitable for storing the following values:

  1. The GPAs of 2,000 students.

  2. The lengths and widths of 100 rectangles.

  3. A week's worth of hourly temperature measurements, stored so that it is easy to calculate the average daily temperature.

  4. A board for a tic-tac-toe game.

  5. The names and capitals of the 50 states.

Exercise 9.14

Write a code segment that will compute the sum of all the elements of an array of int.

Exercise 9.15

Write a code segment that will compute the sum of the elements in a two-dimensional array of int.

Exercise 9.16

Write a method that will compute the average of all the elements of a two-dimensional array of float.

Exercise 9.17

Write a method that takes two parameters, an int array and an integer, and returns the location of the last element of the array that is greater than or equal to the second parameter.

Exercise 9.18

Write a program that tests whether a 3 x 3 array, input by the user, is a magic square. A magic square is an N x N matrix of numbers in which every number from 1 to N2 must appear just once, and every row, column, and diagonal must add up to the same total. For example,

6 7 2 1 5 9 8 3 4 



[Page 468]
Exercise 9.19

Revise the program in the preceding exercise so that it allows the user to input the dimensions of the array up to 4 x 4.

Exercise 9.20

Modify the AnalyzeFreq program so that it can display the relative frequencies of the 10 most frequent and 10 least frequent letters.

Exercise 9.21

The merge sort algorithm takes two collections of data that have been sorted and merges them together. Write a program that takes two 25-element int arrays, sorts them, and then merges them, in order, into one 50-element array.

Exercise 9.22

Challenge: Design and implement a BigInteger class that can add and subtract integers with up to 25 digits. Your class should also include methods for input and output of the numbers. If you're really ambitious, include methods for multiplication and division.

Exercise 9.23

Challenge: Design a data structure for this problem: As manager of Computer Warehouse, you want to track the dollar amount of purchases made by clients who have regular accounts. The accounts are numbered from 0, 1, . . . , N. The problem is that you don't know in advance how many purchases each account will have. Some may have one or two purchases. Others may have 50 purchases.

Exercise 9.24

An anagram is a word made by rearranging the letters of another word. For example, act is an anagram of cat, and aegllry is an anagram of allergy. Write a Java program that accepts two words as input and determines whether they are anagrams.

Exercise 9.25

Challenge: An anagram dictionary is a dictionary that organizes words together with their anagrams. Write a program that lets the user enter up to 100words (in a TextField, say). After each word is entered, the program should display (in a TextArea perhaps) the complete anagram dictionary for the words entered. Use the following sample format for the dictionary. Here the words entered by the user were: felt, left, cat, act, opt, pot, top.

act:   act cat eflt:  felt left opt:   opt pot top 


Exercise 9.26

Acme Trucking Company has hired you to write software to help dispatch its trucks. One important element of this software is knowing the distance between any two cities that it services. Design and implement a Distance class that stores the distances between cities in a two-dimensional array. This class will need some way to map a city name, Boise, into an integer that can be used as an array subscript. The class should also contain methods that would make it useful for looking up the distance between two cities. Another useful method would tell the user the closest city to a given city.

Exercise 9.27

Rewrite the main() method for the WordGuess example so that it allows the user to input the number of players and whether each player is a computer or a human. Use a KeyboardReader.


[Page 469]
Exercise 9.28

Write a smarter version of the WordGuesser class that "knows" which letters of the English language are most frequent. Hint: Rather than use random guesses, store the player's guesses in a string in order of decreasing frequency: "ETAONRISHLGCMFBDGPUKJVWQXYZ".

Exercise 9.29

Write a CLUI version of the SlidingTilePuzzle. You will need to make modifications to the SlidingTilePuzzle class.




Java, Java, Java(c) Object-Orienting Problem Solving
Java, Java, Java, Object-Oriented Problem Solving (3rd Edition)
ISBN: 0131474340
EAN: 2147483647
Year: 2005
Pages: 275

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