Lab 4: Javdoc
The Java API Documentation
If msg is an instance of the Java String class, then the call:
Sometimes the vast quantity of obscure Java methods like this one can be depressing. How can anyone be expected to know all of the methods for all of the classes? Relax. No one does. But a good programmer is proficient at sniffing through reams of documentation to come up with the perfect method for the situationj at hand.
Fortunately, Sun has thought a lot about Java documentation. Every class in every package has a web page devoted exclusively to the public methods and fields of that class. Furthermore, each reference to another class is a link to the corresponding web page. The collection of all Java web pages can be found on the Web at:
Problem 1: Write a Java program to calculate the number of seconds that have elapsed since the stroke of midnight in London on New Years Eve, 1970. Your program should be no more than a few lines long. Is there some pre-defined method you can use to help. What class does it belong to? What package does the class belong to?
Javadoc: The Java API Documentation Generator
Generally speaking, there are two types of comments: interface and implementation. Implementation comments describe which algorithm a method uses or how to interpret the private fields of a class. This information is useful for programmers who must maintain or debug the class. Interface comments describe what task a method performs or how to interpret instances of a class. These types of comments are useful for clients- programmers who must use the class.
javadoc is a tool that comes with the compiler, virtual machine, and other tools bundled in the Java development kit. Given one or more .java files as input, javadoc extracts all of the interface comments and uses them to generate a collection of web pages formatted like the pages that describe the pre-defined JDK classes. javadoc recognizes interface comments by their distinctive format:
The following examples involve the declarations of two associated classes: Point and Box. Instances of the Box class represent rectangles in an integer plane with an upside down y axis. The important fields of a Box are its integer height and width as well as its upper left corner, which is a point:

Here are some uncommented declarations from Point.java and Box.java respectively:
public class Box {
protected int height
= 0, width = 0;
protected Point
topLeft;
public void move(Point
p) { topLeft = p; }
public void resize(int
h, int w) { height = h; width = w; }
// etc.
}
Class (and interface) comments begin with a summary sentence describing what instances of the class represent. The summary sentence is terminated by a tag or by a period followed by a blank, tab, or newline character.
A summary is followed by a blank line followed by the names of the authors and the version number. Java provides special @author and @version tags that preface this information.
For example, here is a class comment for our Point class:
For example, the comment for our Box class begins with a summary that is several paragraphs long and contains a Java expression. To keep javadoc from prematurely terminating the summary, each period is immediately followed by an HTML tag. Periods that end sentences are followed by paragraph tags, periods that don't end paragraphs are followed by non-breaking space tags ( ). We wanted to control the formatting of the Java expression, so we bracketed it with HTML code tags. To force the indentation we added several HTML non-breaking space tags just before the code segment. It's important to remember that browsers ignore the tabs, blank lines, and spaces we insert into our HTML.
Here's what the class comment looks like in a browser:
Instances of the Box class represent rectangles in the first quadrant of an integer plane with an inverted y axis.
If p and q are two points in such a plane with the same x coordinates but with the y coordinate of p larger than the y coordinate of q:
(p.xc == q.xc && p.yc > q.yc)
then p is "below" q. In particular, the origin of this type of plane is always located at the top left corner of the page or window.
This can be confusing, because it means the y coordinate of the top left coordinate of a Box is smaller than the y coordinate of its center point or its bottom right corner.
This type of coordinate system is chosen for compatability with the coordinate system used by the Graphics class in Java's awt package.
Version:
1.0
Author:
Bart Simpson
See Also:
Graphics
Interface documentation doesn't mention private fields or methods, because they are part of the implementation of a class. However, public and protected field comments must include a summary phrase or sentence:
/**
* The x-coordinate
of this Point.
*/
public int xc;
/**
* The y-coordinate
of this Point.
*/
public int yc;
// etc.
}
Of course protected methods and fields are part of the interface seen by class extenders, so they too must be documented. For example, the Box class will grant access to its height, width, and topLeft fields to the classes that extend it:
/**
* The height
of this Box.
*/
protected int height
= 0;
/**
* The width
of this Box.
*/
protected int width
= 0;
/**
* The top
left corner of this Box.
*/
protected Point
topLeft;
// etc.
}
Like a class comment, a method comment begins with a summary sentence or phrase. However, the summary of a method comment is followed by one @param tag describing each parameter, and a @return tag describing how clients should interpret the return type. Often, this is just a repeat of the summary, but that's okay. Constructors and void methods aren't required to have @return tags.
For example, here is the method tag for the Point.equals() method.
Here's the method comment for the Point.toString() method:
/**
* computes the area of this
Box
*
* @return the area of this Box
*/
public int getArea() { return
height * width; }
public void resize(int
h,
int w)
throws java.lang.Exception
sets the height and width of
this Box to the specified values;
displays this Box
Parameters:
h - the new height of this Box
w - the new width of this Box
Throws:
java.lang.Exception - if height
or width is negative
getHeight
public int getHeight()
returns the height of this Box
Returns:
the height of this Box
Constructor comments are a special case of method comments that don't include @return tags:
/**
* Constructs
and initializes this Point with x & y
* coordinates
equal to 0.
*/
public Point()
{ xc = yc = 0; }
Problem 3: Create and document a Label class that extends the Box class. A label is a box that contains a message:
The javadoc Command
The javadoc command is typed at a command console prompt, together with options and a list of .java files: