[Page 460 (continued)] Solution 9.1 | Space (in bytes) allocated for each of the following? int a[] = new int[5]; // 5 * 4 = 20 bytes double b[] = new double[10]; // 10 * 8 = 80 bytes char c[] = new char[30]; // 30 * 2 = 60 bytes String s[] = new String[10]; // 10 * 4 (reference) = 40 bytes Student s[] = new Student[5]; // 5 * 4 (reference) = 20 bytes | Solution 9.2 | An array containing 10 floats, 1.0 to 10.0. float farr[] = {1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0,9.0,10.0}; | | [Page 461] | Solution 9.3 | Prints the first element of farr. System.out.println(farr[0]); | Solution 9.4 | Assigns 100.0 to the last element in farr. farr[farr.length-1] = 100.0; | Solution 9.5 | A loop to print all of the elements of farr. for (int j = 0; j < farr.length; j++) System.out.println(farr[j]); | Solution 9.6 | An array containing the first 100 square roots. double doubarr[] = new double[100]; for (int k = 0; k < doubarr.length; k++) doubarr[k] = Math.sqrt(k+1); | Solution 9.7 | Analyzing the letter frequencies in a file. import java.io.*; import java.util.Scanner; public static void main(String[] argv) { Scanner fileScan; // To read lines of text from the file String str; // To store the line of text AnalyzeFreq af = new AnalyzeFreq(); try { // Create a Scanner File theFile = new File("freqtest.txt"); fileScan = Scanner.create(theFile); fileScan = fileScan.useDelimiter("\r\n"); // For Windows while (fileScan.hasNext()) { // Read and count str = fileScan.next(); af.countLetters(str); } // while af.printArray(); // Print frequencies } catch (Exception e) { e.printStackTrace(); } // catch() } // main() | | [Page 462] | Solution 9.8 | Sort 24 18 90 1 0 85 34 18 with insertion sort. 24 18 90 1 0 85 34 18 // Initial 18 24 90 1 0 85 34 18 // Pass 1 18 24 90 1 0 85 34 18 // Pass 2 1 18 24 90 0 85 34 18 // Pass 3 0 1 18 24 90 85 34 18 // Pass 4 0 1 18 24 85 90 34 18 // Pass 5 0 1 18 24 34 85 90 18 // Pass 6 0 1 18 18 24 34 85 90 // Pass 7 | Solution 9.9 | Sort 24 18 90 1 0 85 34 18 with selection sort. 24 18 90 1 0 85 34 18 // Initial 0 18 90 1 24 85 34 18 // Pass 1 0 1 90 18 24 85 34 18 // Pass 2 0 1 18 90 24 85 34 18 // Pass 3 0 1 18 18 24 85 34 90 // Pass 4 0 1 18 18 24 85 34 90 // Pass 5 0 1 18 18 24 34 85 90 // Pass 6 0 1 18 18 24 34 85 90 // Pass 7 | Solution 9.10 | Code to swap two Students. Student tempStud = student1; student1 = student2; student2 = tempStud; | Solution 9.11 | Implementation of the selectionSort(). public void selectionSort(int arr[]) { int smallest; // Location of smallest element for (int k = 0; k < arr.length-1; k++) { smallest = k; for (int j = k+1; j < arr.length; j++) if (arr[j] < arr[smallest]) smallest = j; if (smallest != k) { // Swap smallest and kth int temp = arr[smallest]; arr[smallest] = arr[k]; arr[k] = temp; } // if } // outer for } // selectionSort() | | [Page 463] | Solution 9.12 | After mystery(myArr,k), myArr will store 1,2,3,5,5 and k will store 3. | Solution 9.13 | Binary search trace for 21 in 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28: key iteration low high mid -------------------------------------- 21 0 0 13 6 21 1 7 13 10 21 2 7 9 8 21 3 9 9 9 21 4 10 9 failure | Solution 9.14 | A two-dimensional array with five rows of 10 integers. int int2d[][] = new int[5][10]; | Solution 9.15 | Prints the last integer in the third row int2d and assigns 100 to its last element. System.out.println(int2d[2][9]); int2d[4][9] = 100; | Solution 9.16 | Prints all of the elements of int2d. for (int k = 0; k < int2d.length; k++) { for (int j = 0; j < int2d[k].length; j++) System.out.print( int2d[k][j] + " "); System.out.println(); // new line } | Solution 9.17 | A 52 x 7 two-dimensional array of int. int sales[][] = new int[52][7]; for (int k = 0; k < sales.length; k++) for (int j= 0; j < sales[k].length; j++) sales[k][j] = 0; | | [Page 464] | Solution 9.18 | A method to calculate average number of newspapers per week. double avgWeeklySales(int arr[][]) { double total = 0; for (int k = 0; k < arr.length; k++) for (int j = 0; j < arr[k].length; j++) total += arr[k][j]; return total/52; } | Solution 9.19 | A method to calculate average number of Sunday newspapers. double avgSundaySales(int arr[][]) { double total = 0; for (int k = 0; k < arr.length; k++) total += arr[k][6]; return total/52; } | Solution 9.20 | A compareTo() for LetterFreq. public int compareTo(Object lf) { LetterFreq letFreq = (LetterFreq)lf; if (freq < letFreq.getFreq()) return -1; else if (freq > letFreq.getFreq()) return +1; else return 0; // The frequencies must be equal. } // compareTo() | Solution 9.21 | A sort() for AnalyzeFreq. public void sort() { java.util.Arrays.sort(freqArr); } // sort() | Solution 9.22 | A new AnalyzeFreq.main() that uses sort(). public static void main(String[] argv) { AnalyzeFreq af = new AnalyzeFreq(); af.countLetters("Now is the time for all good students" + " to study computer-related topics."); af.sort(); af.printArray(); } // main() | |