Skip to main content
Logo image

Java, Java, Java: Object-Oriented Problem Solving, 2022E

Section 9.14 Exercises

Note: For programming exercises, first draw a UML class diagram describing all classes and their inheritance relationships and/or associations.
  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.
  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.
  3. Make each of the following array declarations:
    1. A \(4 \times 4\) array of double s.
    2. A \(20 \times 5\) array of String s.
    3. A \(3 \times 4\) array of char initialized to ‘*’;
    4. A \(2 \times 3 \times 2\) array of boolean initialized to true.
    5. A \(3 \times 3\) array of Student s.
    6. A \(2 \times 3\) array of String s initialized to “one,” “two,” and so on.
  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} };
  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 ]
  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] );
  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] );
  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++)
  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;
  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;}
  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
  12. For the two-dimensional array you created in the previous 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.
  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.
  14. Write a code segment that will compute the sum of all the elements of an array of int.
  15. Write a code segment that will compute the sum of the elements in a two-dimensional array of int.
  16. Write a method that will compute the average of all the elements of a two-dimensional array of float.
  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.
  18. Write a program that tests whether a \(3\; \times \;3\) array, input by the user, is a magic square. A magic square is an \(N\; \times\; N\) matrix of numbers in which every number from 1 to \(N^2\) 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
  19. Revise the program in the previous exercise so that it allows the user to input the dimensions of the array, up to \(4\; \times\; 4\text{.}\)
  20. Modify the AnalyzeFreq program so that it can display the relative frequencies of the 10 most frequent and 10 least frequent letters.
  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.
  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.
  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 those clients that 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.
  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 if they are anagrams.
  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 100 words (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
  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.
  27. Rewrite the main() method for the WordGuess example so that it allows the user to input the number of players and whether each players is a computer or a human. Use a KeyboardReader.
  28. Write a smarter version of the WordGuesser class that “knows” which letters of the English language are most frequent. HINT: Rather than using random guesses, store the player’s guesses in a string in order of decreasing frequency: "ETAONRISHLGCMFBDGPUKJVWQXYZ".
  29. Write a CLUI version of the SlidingTilePuzzle. You will need to make modifications to the SlidingTilePuzzle class.
You have attempted of activities on this page.