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.

method B to get the first l<strong>in</strong>e of a file, when test<strong>in</strong>g A we can replace B with a stub<br />

that returns a fixed str<strong>in</strong>g.<br />

Debugg<strong>in</strong>g<br />

The simplest debugg<strong>in</strong>g technique consists of us<strong>in</strong>g pr<strong>in</strong>t statements (us<strong>in</strong>g<br />

method System.out.pr<strong>in</strong>tln(str<strong>in</strong>g)) to track the values of variables dur<strong>in</strong>g<br />

the execution of the program. A problem with this approach is that the pr<strong>in</strong>t<br />

statements need to be eventually removed or commented out before the software<br />

is f<strong>in</strong>ally released.<br />

A better approach is to run the program with<strong>in</strong> a debugger, which is a specialized<br />

environment for controll<strong>in</strong>g <strong>and</strong> monitor<strong>in</strong>g the execution of a program. The basic<br />

functionality provided by a debugger is the <strong>in</strong>sertion of breakpo<strong>in</strong>ts with<strong>in</strong> the<br />

code. When the program is executed with<strong>in</strong> the debugger, it stops at each<br />

breakpo<strong>in</strong>t. While the program is stopped, the current value of variables can be<br />

<strong>in</strong>spected. In addition to fixed breakpo<strong>in</strong>ts, advanced debuggers allow for<br />

specification of conditional breakpo<strong>in</strong>ts, which are triggered only if a given<br />

expression is satisfied.<br />

The st<strong>and</strong>ard <strong>Java</strong> tools <strong>in</strong>clude a basic debugger called jdb, which is<br />

comm<strong>and</strong>l<strong>in</strong>e oriented. IDEs for <strong>Java</strong> programm<strong>in</strong>g provide advanced debugg<strong>in</strong>g<br />

environments with graphical user <strong>in</strong>terfaces.<br />

1.10 Exercises<br />

For source code <strong>and</strong> help with exercises, please visit<br />

java.datastructures.net.<br />

Re<strong>in</strong>forcement<br />

R-1.1<br />

Suppose that we create an array A of GameEntry objects, which has an <strong>in</strong>teger<br />

scores field, <strong>and</strong> we clone A <strong>and</strong> store the result <strong>in</strong> an array B. If we then<br />

immediately set A [4].score equal to 550, what is the score value of the<br />

GameEntry object referenced by B[4]?<br />

R-1.2<br />

84

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

Saved successfully!

Ooh no, something went wrong!