240 likes | 422 Views
Topics:. jGRASP editor ideosyncrasies assert debugger. jGRASP editor-syncrasies (idiosyncrasies). The jGRASP editor. Problem: Indentation seems to befuddle the jGRASP editor. Solution: Tell jGRASP to use spaces instead of tabs. after. before.
E N D
Topics: • jGRASP editor ideosyncrasies • assert • debugger
The jGRASP editor • Problem: Indentation seems to befuddle the jGRASP editor. • Solution: Tell jGRASP to use spaces instead of tabs.
after before Check Soft Tabs and set the Tab Size to 4. Click Apply and then OK. Now pressing the tab key will insert 4 spaces (and existing tabs will be converted to 4 space).
assert (from http://java.sun.com/developer/technicalArticles/JavaLP/assertions/) • An assertion has a Boolean expression that, if evaluated as false, indicates a bug in the code. • This mechanism provides a way to detect when a program starts falling into an inconsistent state. • Assertions are excellent for documenting assumptions and invariants about a class.
assert (from http://java.sun.com/developer/technicalArticles/JavaLP/assertions/) • Here is a simple example of assertion: BankAccount acct = null; // ... // Get a BankAccount object // ... // Check to ensure we have one assert acct != null; • This asserts that acct is not null. If acct is null, an AssertionError is thrown. Any line that executes after the assert statement can safely assume that acct is not null.
assert • Part of C/C++ through assert.h since the dawn of time. • Officially added to Java 1.4 (and newer).
assert (from http://java.sun.com/developer/technicalArticles/JavaLP/assertions/) • Design by Contract • We can view program correctness as proof that the computation, given correct input, terminated with correct output. The user invoking the computation has the responsibility of providing the correct input, which is a precondition. If the computation is successful, we say that the computation has satisfied the postcondition.
assert (from http://java.sun.com/developer/technicalArticles/JavaLP/assertions/) • Design by Contract • Some programming languages (such as Eiffel) encourage developers to provide formal proof of correctness by writing assertions that may appear in the following roles: • precondition • postcondition • invariant
assert (from http://java.sun.com/developer/technicalArticles/JavaLP/assertions/) • Design by Contract (formal) • precondition • A condition that the caller of an operation agrees to satisfy. • postcondition • A condition that the method itself promises to achieve. • invariant • A condition that a class must satisfy anytime a client could invoke an object's method, or a condition that should always be true for a specified segment or at a specified point of a program.
assert (from http://java.sun.com/developer/technicalArticles/JavaLP/assertions/) • Design by Contract (informal) • precondition What does it expect? • postcondition What does it guarantee? • invariant What does it maintain? • These three roles collectively support what is called the design-by-contract model of programming.
Asserts must first be enabled.In jGRASP . . . (or File) Note: Must be FLAGS2 – not FLAGS!
Using the debugger • A primitive way of debugging is to insert print statements.
Using the debugger • A primitive way of debugging is to insert print statements. • That’s OK but a debugger is much more powerful.
Using the debugger • A primitive way of debugging is to insert print statements. • That’s OK but a debugger is much more powerful. • It allows us to execute our code one line at a time. • It allows us to set breakpoints (stop points) in our code. • We can even examine (and even change) the contents of variables as our program runs!
Start the debugger • Build -> Debug • The program then runs and stops at our first breakpoint.
step over step in step out variables (r-click to change value) next line to be executed end debugging