Version 6.11

Handouts:

Reading:

Principal points to understand:

  1. The Stack interface: how to declare it, what it does.
  2. Generics: why they were added to Java, how to use them.
  3. The call stack: how it supports execution of method calls.
  4. Exceptions: what are they, how to use them.
  5. The Queue interface: how to declare it, what it does.

Part A: Pages 87–100

Stacks and queues are similar: both are collections ordered by time of insertion.

Differences: stacks allow access to the most recently inserted element; queues, least recently inserted.

We study the stack and queue ADTs (represented as interfaces) in this chapter, deferring their implementations to the next two chapters.

[So now we see some of the value of interfaces!]

4.1 A Stack Interface

The Class java.util.Stack

Although Drake presents the stack ADT as an interface, that is not the only way. The standard Java library includes the class java.util.Stack. Find its documentation in the Java API documentation. How does it compare with Drake's stack interface?

Note: although java.util.Stack is a class, not an interface, to us it's still an abstract data type, because we do not know how it is implemented. (Since Java is now largely open, we could peek at the source code to find out. But we don't have to!)

Generics

A Stack Example

Exercises

(???) p. 97: #4.1, 4.2, 4.3.

4.2 The Call Stack

[The problem that a call stack solves: Suppose we have the method calls graphed below (method name: local variables, including formal arguments):

main (args)
   ↓
A(x, y)
   ↓
B(y, z)
   ↓
C(z, w)

Two problems arise:

  1. Since A, B, and C all share variable names, how can these be kept from conflicting?
  2. How does each method know where to return to?

The problem also occurs, of course, with recursive method calls:

main(args)
   ↓
fact(n)
   ↓
fact(n)
   ↓
fact(n)

Here, each call to fact requires its own argument n, typically proceeding as nn − 1. How can these multiple n's keep out of each other's hair?]

Solution: Java, like most programming languages, uses a stack, known as the call stack (or just the stack) to keep track of what's going on (the state of each method call, including local variables and where to return to—also which instruction is next, but that's tracked by the hardware).

Exercises

(???) Page 100: #4-5, 4-6.

(???) Optional: p. 115, #4.16

(???) Lab 2: p. 116, Project 4.20


Part B: Pages 100–110

4.3 Exceptions

4.4 A Queue Interface

A Queue Example

End of Chapter

Problems, p. 115–116:

Project (Lab L2)

Footnotes


  1. Revision history:
    • Version 6.1, 2012 Sep 18. Added links to solutions of end of chapter problems; corrected definition of "FIFO"; other minor editing.
    • Version 6, 2010 Sep 13–?, converted to markdown, new examples, still needs a little revision.
    • Version 5, 9/14/2009, converted to reStructuredText.
    • Version 4, 9/8/2008, added mention of java.util.Stack.
    • Version 3, 9/14/2007, minor revisions.
    • Version 2, 9/9/2007.
    • Version 1, 9/11/2006.