Solutions to SELECTED textbook exercises: 3.6 If setValue() is called with an illegal value, the value field is unchanged but no error message is printed. 3.7 The change from >= to > prevents value from ever being set to 0 3.8 If || (or) replaces && (and), the resultant condition is ALWAYS TRUE. 3.20 replace value = (value+1) % limit with value = value + 1; if (value == limit) value = 0; Personally I tend to feel that the latter is clearer. 4.2 10 objects would be numbered 0..9 and size() would return 10 4.3 To retrieve the fifth item, use items.get(4). The first four are at 0,1,2,3. 4.4 14 4.5 I will assume that both vars "meeting" and "notes" are class fields. Then the following needs no parameters: public void my_add() { notes.add(meeting); } 4.6 The third object has index value 2: dates.remove(2) 4.7 Removing the object at index 0 (coll.remove(0)) renumbers everything else down by 1, so the object that had been at index 1 is now at index 0 and the object that had been at index 6 is now at index 5. Removing the object at index 9 has absolutely no effect on the position of the item at index 6 (or perhaps now 5); as 9 is bigger. 4.10 The problem is that we have no way of knowing how many lines of notes.get(0), notes.get(1), etc will be needed. Users add new notes at run time, so at compile time we cannot possibly know this. 4.11 Here is the book version (from the CD) of listNotes(): public void listNotes() { int index = 0; while(index < notes.size()) { System.out.println(notes.get(index)); index++; } } It was this method that demonstrated our first-ever while loop. 4.17 To get listNotes() to print the item number before each item, change the System.out.println in the code in 4.11 to System.out.println(index + ": " + notes.get(index)); 4.21 done in class in Club demo public void join(Membership member) { membership.add(member); // pld } 4.38 To deglare "people" to be an array, not yet allocated, of Person objects Person[] people; 4.39 boolean[] vacant; 4.41 []int counts; ===========> int[] counts; boolean[5000] occupied; =====> boolean[] occupied; For the second, the size of 5000 is set later at allocation: occupied = new boolean[5000]; 4.42 The question here is to provide the allocation statements (using "new"): readings = new double[60]; urls = new String[90]; machines = new TicketMachine[5]; 4.43 trick question! The declaration creates an array of 20 slots to put String objects into, but DOESN'T ACTUALLY CREATE ANY STRINGS. Those would be done later with simple assignments labels[6] = "hello"; or with "new" allocations: labels[8] = new String(); 4.44 The final () should be []" double [] prices = new double[50]; But it looks plausible because "new" statments for *nonarrays* do always use the parentheses. 4.45 for loop; < versus <= With the loop for (int hour = 0; hour <= 24; hour++) ... we execute the loop 25 times, from hour=0 to hour=24. Since hourCounts[24] doesn't actually exist, we encounter an array-bounds error. 4.46 for-to-while conversion Old: for(int hour = 0; hour < hourCounts.length; hour++) { System.out.println(hour + ": " + hourCounts[hour]); New: int hour = 0; while (hour < hourCounts.length) { System.out.println(...same...); hour++; } 4.48 while-to-for conversion: won't be on exam Old: int index = 0; while(index < notes.size()) { System.out.println(notes.get(index)); index++; } New: for (int index = 0; index < notes.size(); index++) { System.out.println(notes.get(index)); } You can also declare "index" outside the for loop (in fact that's more common): int index; for (index = 0; index < notes.size(); index++) { System.out.println(notes.get(index)); } ================================================================================== ================================================================================== Solutions to my example problems: > 1.Suppose we have an > ArrayList notes > All the actual notes are String objects. > Write a loop to concatenate all the strings into one big one. String result = ""; int i = 0; while ( i 2. Write a loop to add up the numbers from 1 to N. Here it is as a complete method: public int sumnums(int N) { int i = 0; int sum = 0; while (i<=N) { sum = sum + i; i=i+1; } return sum; } > 3. Finish a method printMultiple that takes two parameters, > an int n and a char ch, and prints n copies of that character: > printMultiple('a', 4) should yield aaaa. public void printMultiple(int n, char ch) { int i; for (i=0; i 4. *Using* the method printMultiple above, write a loop to draw > * > *** > ***** > ******* > (each row has two more *'s than the previous row.) int starcount = 1; // # of stars on row 1 int row = 1; while (row<= maxrow) { // suppose maxrow is given printMultiple(starcount, '*'); starcount += 2; // increase by 2 each time row++ } > 5. Write a method that reverses a string: > public String reverse (String s) { > } > Hint: let n = s.length(). The letters of s are s.charAt(n-1) down to > s.charAt(0). Append these (with +) in turn to an initially empty string. public String reverse (String s) { int n = s.length() -1; String rev = ""; while (n>=0) { rev = rev + s.charAt(n); n = n-1; } return rev; }