Chap03 EX Solutions
Chap03 EX Solutions
EX 3.1 Object-oriented programming allows you to define and compile a general form
of a class. Later you can define a specialized version of this class, starting with
the original class. Which particular OOPs technique does this depict?
Inheritance. It is the process in which a new class is derived from an existing one. The new class
automatically contains some or all of the variables and methods in the original class.
EX 3.2 Why is an object used as the perfect mechanism for creating a collection?
A collection is an abstraction wherein the details of the implementation are hidden. An object is the
perfect mechanism for creating such an abstraction because the internal workings of
an object are encapsulated from the rest of the system. In most cases, the instance
variables defined in a class should be declared with private visibility. Therefore, only
the methods of that class can access and modify them. The only interaction a user
has with an object should be through its public methods, which represent the
services that the object provides.
EX 3.3 What type of collection is a stack? Give one natural example of the usage of
stacks in computer science.
A stack is a linear collection. The most natural example of the usage of a stack in computer science is
the processing of subroutine calls and their returns.
EX 3.4 Hand trace an initially empty stack X through the following operations:
X.push(new Integer(20));
X.push(new Integer(15));
Integer Y = X.pop();
X.push(new Integer(35));
X.push(new Integer(10));
X.push(new Integer(25));
X.push(new Integer(45));
Integer Y = X.pop();
X.push(new Integer(15));
X.push(new Integer(45));
©2014 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
Java Software Structures, 4th Edition Exercise Solutions, Ch. 3
EX 3.5 Given the resulting stack X from the previous exercise, what would be the
result of each of the following?
Y = X.peek();
Y = X.pop();
Z = X.peek();
The value of Y would be 45, the value of Z would be 15, and the stack would
contain:
15 25 10 35 20
Y = X.push(30);
Z = X.peek();
EX 3.6 Write Java statements to define a Bag class to store a generic type T. The class
should have two generic methods for setting and retrieving data.
EX 3.7 Write Java statements to instantiate the Bag class created in the above
example, for storing String type data items. Store a value Money as a data
item.
EX 3.8 When you type a line of text on a keyboard, you use the backspace key to
remove the previous character if you have made a mistake. Consecutive
application of the backspace key thus erases several characters. Show how the
use of backspace key can be supported by the use of a stack. Give specific
examples and draw the contents of the stack after various actions are taken.
©2014 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
Java Software Structures, 4th Edition Exercise Solutions, Ch. 3
asdfijkl
EX 3.9 Draw an example using the five integers (21, 32, 11, 54, 90) showing how a stack
could be used to reverse the order (90, 54, 11, 32, 21) of these elements.
The five values would be pushed onto the stack in order, yielding (with the top of the
stack on the left):
90 54 11 32 21
Then, popping each value produces them in the reverse order to that in which they
were first encountered.
EX 3.10 Convert the following infix expressions to postfix form by using the algorithm
discussed in this chapter.
a. (a * (b / c)) – d + e / f
b. a - ( b * c / d ) + e
a. a b c / * d – e f / +
b. a b c * d / - e +
©2014 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.