19.09.2015 Views

Prentice.Hall.Introduction.to.Java.Programming,.Brief.Version.9th.(2014).[sharethefiles.com]

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

386 Chapter 10 Thinking in Objects<br />

user doesn’t need <strong>to</strong> know how these methods are implemented. The Course class encapsulates<br />

the internal implementation. This example uses an array <strong>to</strong> s<strong>to</strong>re students, but you could<br />

use a different data structure <strong>to</strong> s<strong>to</strong>re students. The program that uses Course does not<br />

need <strong>to</strong> change as long as the contract of the public methods remains unchanged.<br />

stack<br />

Key<br />

Point<br />

10.9 Case Study: Designing a Class for Stacks<br />

This section designs a class for modeling stacks.<br />

Recall that a stack is a data structure that holds data in a last-in, first-out fashion, as shown in<br />

Figure 10.10.<br />

Data1<br />

Data2<br />

Data3<br />

Data1<br />

Data2<br />

Data1<br />

Data3<br />

Data2<br />

Data1<br />

Data3<br />

Data2<br />

Data1<br />

Data2<br />

Data1<br />

Data1<br />

FIGURE 10.10<br />

A stack holds data in a last-in, first-out fashion.<br />

VideoNote<br />

The StackOfIntegers class<br />

Stacks have many applications. For example, the <strong>com</strong>piler uses a stack <strong>to</strong> process method<br />

invocations. When a method is invoked, its parameters and local variables are pushed in<strong>to</strong> a<br />

stack. When a method calls another method, the new method’s parameters and local variables<br />

are pushed in<strong>to</strong> the stack. When a method finishes its work and returns <strong>to</strong> its caller, its associated<br />

space is released from the stack.<br />

You can define a class <strong>to</strong> model stacks. For simplicity, assume the stack holds the int values.<br />

So name the stack class StackOfIntegers. The UML diagram for the class is shown in<br />

Figure 10.11.<br />

StackOfIntegers<br />

-elements: int[]<br />

-size: int<br />

+StackOfIntegers()<br />

+StackOfIntegers(capacity: int)<br />

+empty(): boolean<br />

+peek(): int<br />

+push(value: int): void<br />

+pop(): int<br />

+getSize(): int<br />

An array <strong>to</strong> s<strong>to</strong>re integers in the stack.<br />

The number of integers in the stack.<br />

Constructs an empty stack with a default capacity of 16.<br />

Constructs an empty stack with a specified capacity.<br />

Returns true if the stack is empty.<br />

Returns the integer at the <strong>to</strong>p of the stack without<br />

removing it from the stack.<br />

S<strong>to</strong>res an integer in<strong>to</strong> the <strong>to</strong>p of the stack.<br />

Removes the integer at the <strong>to</strong>p of the stack and returns it.<br />

Returns the number of elements in the stack.<br />

FIGURE 10.11<br />

the stack.<br />

The StackOfIntegers class encapsulates the stack s<strong>to</strong>rage and provides the operations for manipulating<br />

Suppose that the class is available. The test program in Listing 10.7 uses the class <strong>to</strong> create<br />

a stack (line 3), s<strong>to</strong>re ten integers 0, 1, 2, . . . , and 9 (line 6), and displays them in reverse order<br />

(line 9).

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!