Solutions to other chapters:
  1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26  
Java Methods Home Page Skylight Publishing



Java Methods A & AB:
Object-Oriented Programming and Data Structures

Answers and Solutions to Exercises

Chapter 21

1. (a) F
4.   A stack is not needed because we can process binNum's characters in reverse, starting at the end of the string: public class BinToDecimal { public static int binToInt(String binNum) { int result = 0, power2 = 1; for (int i = binNum.length() - 1; i >= 0; i--) { char ch = binNum.charAt(i); int dig = Character.digit(ch, 2); result += dig * power2; power2 *= 2; } return result; } }
5. (b) This implementation is quite inefficient because String stack is reallocated in each push and pop operation.
6.   public boolean moveToTop(Stack<Card> deck, int n) { Stack<Card> temp = new Stack<Card>(); while (n > 1 && !deck.isEmpty()) { temp.push(deck.pop()); n--; } Crad nth = null; if (!deck.isEmpty()) nth = deck.pop(); while (!temp.isEmpty()) { deck.push(temp.pop()); } if (nth != null) { deck.push(nth); return true; } else return false; }
9.   The integer values stored at 40:1A and 40:1C are the same, 0028 (in hex). These offsets represent the front and the rear of the ring buffer. The fact that they are the same indicates that the keyboard queue is currently empty. The last eight ASCII codes, stored in the buffer (going from location 0028 and around) are 64 20 34 30 3A 31 61 0D (in hex), which corresponds to the string d40:1a. Actually, this string is the "dump" command in the MS-DOS debug program that was used to produce the memory dump for this question.
10.   C

Copyright © 2006 by Skylight Publishing