How, when, and why to document Java programs using single-line, multi-line, and Javadoc comments.
In AP CSA, documentation is the written explanation of what your code does and why you wrote it that way. Clear, accurate comments make your program easier to read, debug, maintain, and share. On teams, good comments are as important as good variable names.
//Use for short notes that clarify a line or two.
// Calculate the area of a circle
double area = Math.PI * radius * radius;
/* ... */Use when you need to explain a whole block or summarize an algorithm.
/*
Reads input scores, computes the average,
and prints a rounded result.
*/
/** ... */Use above classes and methods to generate API docs with Javadoc. Include @param and @return tags.
/**
* Calculates the area of a circle.
* @param radius the circle's radius
* @return the computed area
*/
public static double calculateArea(double radius) {
return Math.PI * radius * radius;
}
// add 1 to x
// Adjust for zero-based index
/**
* Program: CircleAreaCalculator
* Author: Your Name
* Purpose: Reads a radius and prints the circle's area.
*/
public class CircleAreaCalculator {
/**
* Entry point: computes the area using A = πr².
* Assumes radius is non-negative.
* @param args unused
*/
public static void main(String[] args) {
double radius = 5.0; // example user-provided radius
double area = Math.PI * radius * radius; // formula for area
System.out.println("Area: " + area); // display result
}
}