23.11.2014 Views

Data Structures and Algorithms in Java[1].pdf - Fulvio Frisone

Data Structures and Algorithms in Java[1].pdf - Fulvio Frisone

Data Structures and Algorithms in Java[1].pdf - Fulvio Frisone

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

low-level implementation details. At the same time, we should not gloss over<br />

important steps. Like many forms of human communication, f<strong>in</strong>d<strong>in</strong>g the right<br />

balance is an important skill that is ref<strong>in</strong>ed through practice.<br />

1.9.3 Cod<strong>in</strong>g<br />

As mentioned above, one of the key steps <strong>in</strong> cod<strong>in</strong>g up an object-oriented program<br />

is cod<strong>in</strong>g up the descriptions of classes <strong>and</strong> their respective data <strong>and</strong> methods. In<br />

order to accelerate the development of this skill, we discuss various design patterns<br />

for design<strong>in</strong>g object-oriented programs (see Section 2.1.3) at various po<strong>in</strong>ts<br />

throughout this text. These patterns provide templates for def<strong>in</strong><strong>in</strong>g classes <strong>and</strong> the<br />

<strong>in</strong>teractions between these classes.<br />

Many programmers do their <strong>in</strong>itial cod<strong>in</strong>g not on a computer, but by us<strong>in</strong>g CRC<br />

cards. Component-responsibility-collaborator, or CRC cards, are simple <strong>in</strong>dex cards<br />

that subdivide the work required of a program. The ma<strong>in</strong> idea beh<strong>in</strong>d this tool is to<br />

have each card represent a component, which will ultimately become a class <strong>in</strong> our<br />

program. We write the name of each component on the top of an <strong>in</strong>dex card. On the<br />

left-h<strong>and</strong> side of the card, we beg<strong>in</strong> writ<strong>in</strong>g the responsibilities for this component.<br />

On the right-h<strong>and</strong> side, we list the collaborators for this component, that is, the<br />

other components that this component will have to <strong>in</strong>teract with to perform its<br />

duties. The design process iterates through an action/actor cycle, where we first<br />

identify an action (that is, a responsibility), <strong>and</strong> we then determ<strong>in</strong>e an actor (that is,<br />

a component) that is best suited to perform that action. The design is complete when<br />

we have assigned all actions to actors.<br />

By the way, <strong>in</strong> us<strong>in</strong>g <strong>in</strong>dex cards to beg<strong>in</strong> our cod<strong>in</strong>g, we are assum<strong>in</strong>g that each<br />

component will have a small set of responsibilities <strong>and</strong> collaborators. This<br />

assumption is no accident, for it helps keep our programs manageable.<br />

An alternative to CRC cards is the use of UML (Unified Model<strong>in</strong>g Language)<br />

diagrams to express the organization of a Program, <strong>and</strong> the use of pseudo-code to<br />

describe the algorithms. UML diagrams are a st<strong>and</strong>ard visual notation to express<br />

object-oriented software designs. Several computer-aided tools are available to<br />

build UML diagrams. Describ<strong>in</strong>g algorithms <strong>in</strong> pseudo-code, on the other h<strong>and</strong>, is a<br />

technique that we utilize throughout this book.<br />

Once we have decided on the classes for our program <strong>and</strong> their responsibilities, we<br />

are ready to beg<strong>in</strong> the actual cod<strong>in</strong>g on a computer. We create the actual code for<br />

the classes <strong>in</strong> our program by us<strong>in</strong>g either an <strong>in</strong>dependent text editor (such as<br />

emacs, WordPad, or vi), or the editor embedded <strong>in</strong> an <strong>in</strong>tegrated development<br />

environment (IDE), such as Eclipse or Borl<strong>and</strong> JBuilder.<br />

Once we have completed cod<strong>in</strong>g for a class (or package), we compile this file <strong>in</strong>to<br />

work<strong>in</strong>g code by <strong>in</strong>vok<strong>in</strong>g a compiler. If we are not us<strong>in</strong>g an IDE, then we compile<br />

our program by call<strong>in</strong>g a program, such as javac, on our file. If we are us<strong>in</strong>g an<br />

77

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

Saved successfully!

Ooh no, something went wrong!