Debugging technique
every class to have a main() method
A technique often used by
Java developers working on large software projects is to write a main()
method for every class they write. Each main() method
creates one or more instance objects of the class, sends the object(s) some message(s) and
displays the changing state of the object(s).
In this way each
individual class can be tested in isolation. For large software systems this can help
reduce bugs, or mean that they are found earlier, since it can be very difficult debugging
a system with many objects from many class.
Although for the programs
you are developing for this module you probably do not need to test each class in
isolation (since it almost as easy to debug programs containing only 2 or 3 classes), an
example is given here to illustrate this technique.
(Note - this is only
appropriate for non-abstract classes).
Consider a simple applet
to draw 2 triangles in its window. This applet makes use of a class MyTriangle.
Lets
assume the output we want from the applet is as follows:
However,
the output from the applet we have written is as follows:
The writer of our applet
has made the mistake of forgetting that the origin of Java graphics is the top left (i.e.
the smaller the Y co-ordinate the higher up the screen a point is). The writer of this
applet can test that their MyTriangle class works in
isolation as follows:
class
MyTriangle
{
// variables
public int top;
public int bottom;
public int left;
public int right;
// constructor method MyTriangle()
public MyTriangle(int newX, int newY, int height, int width)
{
left = newX;
right = (newX + width);
bottom = newY;
top = (newY + height);
}
// draw() method
public void draw( Graphics g )
{
g.drawLine( left, bottom, right, bottom);
g.drawLine( right, bottom, getMiddleX(), top);
g.drawLine(getMiddleX(), top, left, bottom);
}
// getMiddleX() - attribute that is calculated
public int getMiddleX()
{
return (int) ( (left + right) / 2 );
}
// main() method for debugging
public static void main( String args[] )
{
MyTriangle t = new MyTriangle(50, 50, 20, 10);
System.out.println( "------ test of MyTriangle -------");
System.out.println( "top = " + t.top );
System.out.println( "bottom = " + t.bottom );
System.out.println( "left = " + t.left );
System.out.println( "right = " + t.right );
System.out.println( "getMiddleX() = " + t.getMiddleX() );
}
} //
class
We can run the main method of the MyTriangle class using the java command:

Had the programmer
used such a method, they might have remembered that for Java graphics the bottom of
the triangle has a larger Y value than the top, which explains why the triangle is
draw with the point to the top of the screen. Even if they cannot remember that, at least
they now know the values of the variables forming arguments for the drawLine() messages and so on.
By the way, the
code of an applet to produce such triangles is as follows:
import
java.applet.Applet;
import
java.awt.*;
public
class TriangleApplet extends Applet
{
// variables
MyTriangle t1 = new MyTriangle(50,50,20,10);
MyTriangle t2 = new MyTriangle(100,50,50,40);
public void paint( Graphics g )
{
t1.draw( g );
t2.draw( g );
}
} //
class
Back
to top

RITSEC - Global Campus
Copyright ?1999 RITSEC- Middlesex University. All rights reserved.
webmaster@globalcampus.com.eg
|