0% found this document useful (0 votes)
96 views3 pages

Chap03 EX Solutions

VCXVFDGFDG

Uploaded by

VIKRAM KUMAR
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
96 views3 pages

Chap03 EX Solutions

VCXVFDGFDG

Uploaded by

VIKRAM KUMAR
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Java Software Structures, 4th Edition Exercise Solutions, Ch.

Chapter 3 Exercise 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));

The stack, after each operation (top of stack on the left):


20
15 20
20
35 20
10 35 20
25 10 35 20
45 25 10 35 20
25 10 35 20
15 25 10 35 20
45 15 25 10 35 20

©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();

The value of Y would be 45 and the stack would remain unchanged.

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();

The value of Z would be 30, and the stack would contain:


30 15 25 10 35 20

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.

public class Bag<T>


{
private T item;
public void setItem (T newItem)
{
item = newItem;
}
public T getItem()
{
return item;
}
}

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.

Bag<String> purse = new Bag<String>();


purse.setItem(“Money”);

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.

Suppose the user has typed

©2014 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
Java Software Structures, 4th Edition Exercise Solutions, Ch. 3

asdfijkl

where the  stands for the backspace key.


Every character entered on the keyboard should be stored in a stack. Each time a
backspace key is typed, the top of the stack would be popped and the character
removed. So, in the above example, the stack would be: a s i j (top of stack on the left).

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.

You might also like