Reusable Software and Testing

CS 180 Recitation Week 14

Information

Check the accuracy of your grades in Blackboard:

● Project Average = ( (p1*0.05) + (p2*0.05) + (p3*0.07) + (p4*0.07) ) normalized to 100

● Lab Average = average of labs 02 - 09, normalized to 100

● Course Total = ( (pavg * 0.4) + (lavg * 0.15) + (T1 * 0.1) + (T2 * 0.1) ) normalized to 100

Debugging in

● Why do we need it ? - Large programs and softwares are prone to be erroneous for various reasons however careful one may be. ● The simplest way to debug ? -Use System.out.println(); and see the Control flow of the program for various inputs.

Different ways to Debug Programs

● IDEs contain their own debuggers (such as DrJava, IBM VisualAge for Java, )

● Stand-alone GUIs (such as Jikes, Java Platform Debugger Architecture javadt, and JProbe)

● Text-based and command-line driven (such as Sun JDB)

● Brute force using an editor (such as Notepad or ) and eyeballing stack traces.

DrJava Debugger

http://www.drjava.org/docs/quickstart/ch04s03.html ● Break Point: The location in programming code that, when reached, triggers a temporary halt in the program. They can be used by the program to stop and examine the data.

● In DrJava you can create a breakpoint using Crtl+B (or right click → Toggle Breakpoint) or use the debugger menu (previous slide).

Tables available in the Debug mode

● Watches: This displays the instantaneous values of specified fields and variables given the current line that the debugger is on. You can also put in expressions of variables.

● Stack: This a table that lists the current stackframes. Use this to determine the sequence of methods that have been invoked to arrive at the current line in the execution.

● Threads: This is list of the threads that are running in the virtual machine. If there are multiple threads that are suspended, you can set the active thread by double clicking.

Debugging in DrJava

● The Resume Button: runs the program until another breakpoint is reached, or the interaction has ended.

● The Step Into Button: steps through the execution of the method invocations in the current line.

● The Step Over Button: executes the current line and stops execution on the next line.

● The Step Out Button: executes the rest of the current method and stops execution at the line from which the current method was called.

Jdb – The Java Debugger

A command-line debugger for Java classes. ● jdb [ options ] [ class ] [ arguments ] ● Options: Command-line options, as specified below. ● class: Name of the class to begin debugging. arguments: Arguments passed to the main() method of class.

http://java.sun.com/j2se/1.4.2/docs/tooldocs/solaris/jdb.html Jdb – The Java Debugger

● Various commands are provided to provide functionalities with breakpoints,threads, stack traces, memory etc.

http://java.sun.com/j2se/1.4.2/docs/tooldocs/solaris/jdb.html

Java Packages

● A package is a grouping of related types providing access protection and name space management. ● Why do we need them ? ● To make types(Classes, interfaces) easier to find and use, to avoid naming conflicts (many developers can use the same name for different classes), and to control access, bundle groups of related types into packages. ● Helps in building large software.

Creating Java Packages

● Choose a name for the package say mypkg and put a package statement at the top of every source file that contains the types (classes, interfaces) that you want to include in the package. ● Eg. put the line package mypkg; at the top of every source file to be included in the package.

Creating Java Packages

● place the complied .class files for each class in the package in a directory whose name reflects the name of the package to which the type belongs ● In our example place the .class files for the package mypkg in the directory name /mypkg

● Add the in the class path. ● The package can be used by an import statement: import mypkg.*

Using packages with import statements.

● Eg. import java.awt.* ; import java.io.*;

● If the directory structure is something like dir1/dir2/mypkg/ and say dir1 is present in the class path. The classes in the package mypkg can be used another Class by the import statement

● import dir2.mypkg.*;

● To import a specific class use: import dir2.mypkg.MyClass

Quiz

● What are the main bugs that can occur in multi threaded programming ?