Exercise 3-12: (2) Find the code for the second version of HelloDate.java, which is the simple comment documentation example. Execute Javadoc on the file and view the results with your Web browser.
// HelloDate.java
import java.util.Date;
/**
* The first Thinking in Java example program.
* Displays a string and today’s date.
* @author Bruce Eckel
* @author www.MindView.net
* @version 4.0
*/
public class HelloDate
{
/**
* Entry point to class and application.
* @param args array of {@link java.lang.String String} arguments
* @throws exceptions No exceptions thrown
*/
public static void main(String[] args)
{
System.out.print("Hello, it’s: "); // no newline
System.out.println(new Date());
// System.out.println(new Date().toString());
}
}
/*
javac HelloDate.java
java HelloDate
Hello, it’s: Thu Jul 13 14:24:22 EEST 2023
javadoc HelloDate.java -d doc -author -version
javadoc HelloDate.java -d doc -author -version \
-link https://docs.oracle.com/en/java/javase/21/docs/api/
*/
-d doc // specify destination directory for javadoc output; if doc does not exist, it is created;
-author -version // include information about authors and version in the output;
-link https://docs.oracle.com/en/java/javase/21/docs/api/ // link online Java API.
\ at the end of a line allows a command to continue on the next line.
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
Exercise 3-13: (1) Run Documentation1.java, Documentation2.java, and Documentation3.java through Javadoc. Verify the resulting documentation with your Web browser.
Exercise 3-14: (1) Add an HTML list of items to the documentation in the previous exercise.
/** A class comment */
public class Documentation1
{
/** A field comment */
public int i;
/** A method comment */
public void f() {}
}
/*
javac Documentation1.java
java Documentation1 // runtime error: no main()
javadoc Documentation1.java -d doc1
javadoc Documentation1.java -d doc1 \
-link https://docs.oracle.com/en/java/javase/21/docs/api/
*/
java Documentation1
(ignoring the comment) // runtime error: no main()
Look also over the Notes at the end of the previous exercise (Exercise_3-12).
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
import java.util.Date;
public class Documentation2
{
/**
* <pre>
* {@link java.lang.System System}.out.println(new {@link java.util.Date Date}());
* </pre>
*/
public static void main(String[] args)
{
System.out.println(new Date());
}
}
/*
javac Documentation2.java
java Documentation2
Fri Jul 14 17:26:38 EEST 2023
javadoc Documentation2.java -d doc2
javadoc Documentation2.java -d doc2 \
-link https://docs.oracle.com/en/java/javase/21/docs/api/
*/
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
/**
* You can <em>even</em> insert a list:
* <ol>
* <li> Item one
* <li> Item two
* <li> Item three
* </ol>
*/
public class Documentation3 {}
/*
javac Documentation3.java
java Documentation3 // runtime error: no main()
javadoc Documentation3.java -d doc3
javadoc Documentation3.java -d doc3 \
-link https://docs.oracle.com/en/java/javase/21/docs/api/
*/
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
Exercise 3-15: (1) Take the program in Exercise_3-2 and add comment documentation to it. Extract this comment documentation into an HTML file using Javadoc and view it with your Web browser.
/**
* Main (and only) class of the greeting program.
*/
public class Hello
{
/**
* Prints a greeting message.
* @param args Command line arguments as
* {@link java.lang.String Strings}.
* @throws exceptions No exceptions thrown
*/
public static void main(String[] args)
{
System.out.println("Hello, world!");
}
}
/*
javac Hello.java
java Hello
Hello, world!
javadoc Hello.java -d doc \
-link https://docs.oracle.com/en/java/javase/21/docs/api/
*/