Solutions to other chapters:
Java Methods A & AB:
Object-Oriented Programming and Data Structures
Answers and Solutions to Exercises
Chapter 21
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 deck, int n)
{
Stack temp = new Stack();
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. |
|