4. Javadoc Lesson¶
API Documentation with Javadoc¶
API Documentation with Javadoc
Note
To show the solution for this activity, you will need to toggle “Show Solutions” above.
Lesson Objectives
This lesson is designed to get you acquainted with Javadoc comments. In particular, it walks you through the transformation of non-Javadoc comments at the class-level and method-level into Javadoc comments so that the Javadoc program can be used to generate a documentation website. In addition to the generated website, the comments themselves also help programmers who are reading your code. Proper comments save companies and teams real world money by lowering transaction costs associated with interpreting code.
Prerequisites
A basic understanding of Javadoc tags and generating API documentation.
[Javadoc Chapter](https://cs1302uga.github.io/cs1302-book/tools/javadoc/javadoc-index.html)
[CSCI 1302 Style Guide](https://github.com/cs1302uga/cs1302-styleguide)
Course-Specific Learning Outcomes
LO2.b: (Partial) Define, throw, and propagate exceptions appropriately in a software solution.
LO3.b: Create class, interface, method, and inline documentation that satisfies a set of requirements.
LO3.c: Generate user-facing API documentation for a software solution.
Part 1: Creating a Documentation Website (Instructor Demo)
Execute the command below to download and extract the files:
sh -c "$(curl -fsSl https://cs1302uga.github.io/cs1302-book/_bundle/cs1302-javadoc-lesson.sh)"
- downloading cs1302-javadoc-lesson bundle... - verifying integrity of downloaded files using sha256sum... - extracting downloaded archive... - removing intermediate files... subdirectory cs1302-javadoc-lesson successfully created
Change to the
cs1302-javadoc-lessondirectory that was created using and look at the files that were bundled as part of the starter code using tree. You should see output similar to the following:. └── src └── cs1302 └── doc └── House.javaMake sure you are still in the
cs1302-javadoc-lessondirectory. Write the command to generate the API documentation website for the code contained in this exercise and place it in a subdirectory nameddoc. Be sure to write the full command in your notes, specifying the-d,-sourcepath, and-subpackagesoptions. In your notes, briefly explain what each of these options mean.Note
Don’t worry about any warnings you see at this time. We will fix those warnings by adding Javadoc comments later in the lesson.
Solution
javadoc -d doc -sourcepath src -subpackages cs1302
Understanding the command: Section 4.3.
Verify that files were generated and placed in the
docdirectory before continuing.Make sure you are still in the
cs1302-javadoc-lessondirectory. Uselnto create a symbolic link (shortcut) namedcs1302-javadoc-lesson-docin yourpublic_htmldirectory. The symbolic link should link to thedocsubdirectory containing the API documentation website created in a previous step. Write the exact command used in your notes.NOTE: Remember, if you use
$(pwd)as described in the reading, then that expression will expand to the absolute path of your present working directory. That is, the string that it expands to will differ depending on where you currently are.Solution
There are two correct commands as discussed in Section 4.4.
You can use the shortcut command as seen below OR you can use the full absolute path to the
docdirectory instead of$(pwd)/doc.ln -s $(pwd)/doc ~/public_html/cs1302-javadoc-lesson-doc
The symbolic link created in the last step is called
cs1302-javadoc-lesson-doc. You can see it if you change into yourpublic_htmldirectory and perform anls -l.If you made a mistake generating the symbolic link, take a minute to review Section 4.5 for troubleshooting tips.
Navigate to the following URL in your web browser, replacing
userwith your Odin username (be sure to remember the~):https://webwork.cs.uga.edu/~user/cs1302-javadoc-lesson-doc/
You should see the API documentation website that you generated. Other than the website you generated while doing the reading, does this website look similar to any other websites that you may have visited?
Part 2: Additional Practice with Javadoc
From the API documentation website created in Part 1, click on one of the links to the
Houseclass. In your notes, list the name of each method that appears to contain commented documentation. How many such methods currently containing Javadoc comments are there?In
House.java, some of the methods already have multiline, non-Javadoc comments. Transform the comments into Javadoc using the appropriate syntax. In each Javadoc comment, include one or more properly punctuated sentences that describe the method. You may base these sentences off the existing comments. Be sure to also include tags that document, as needed, the parameters and return type. Leave out any propagated exceptions for now.Use the
@codetag where appropriate. A general rule is that nouns that refer to code identifiers (e.g., variable names, class names, method names, etc.) should be differentiated using an@codetag. If done correctly, readers of the documentation website will be able to better identify when a general word is being used versus an identifier.In
House.java, the class itself also has multiline, non-Javadoc comments. Transform the comments into Javadoc using the appropriate syntax. Try to preserve the multi-paragraph nature of the existing comment. Use the@codetag where appropriate.Run the
javadoctool to update your API documentation website on Odin. You should not need to recreate the symbolic link in~/public_html, assuming it still exists. This is because the symbolic link is pointing to yourdocfolder. When thedocfolder is updated, the link automatically points to this new content.View the updated API documentation website.
Repeat the previous three steps until you are confident that the API documentation website displays the documentation corresponding to your comments. Once done, write down the direct URL to the API documentation webpage for the
Houseclass.
Part 3: Additional Practice with Exceptions
To ensure that a
Houseobject cannot have a negative price, modify the constructor and thesetPricemethod to throw an [IllegalArgumentException](https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/lang/IllegalArgumentException.html) with an appropriate detail message when thepriceparameter is negative.In both places, create and explicitly throw the exception using the
throwkeyword inside of an appropriateifstatement.In the constructor, do not list the exception in the signature using
throws.In the
setPricemethod, do list the exception in the signature usingthrows.
Is
IllegalArgumentExceptiona checked or an unchecked exception? How do you know?Update your Javadoc comments so that users of the constructor are aware that the constructor may throw an exception under certain circumstances. At a minimum, appropriate use of the
@throwstag is needed, regardless of whether the exception is listed usingthrowsin the method signature.Run the
javadoctool to update your API documentation website on Odin. Find the constructor andsetPricemethod in the “Construtor Detail” and “Method Detail” sections of theHouseclasses documentation webpage, respectively. How does their documentation _differ_ with respect to the exception? Why?Note: In general, when a method throws an unchecked exception, we will not list it in the signature of the method. We did it in this exercise for
setPriceonly to see the difference in the documentation webpage.Run the 1302
checkstyleprogram onHouse.java. If errors are reported, look up each error message in the [Style Guide](https://github.com/cs1302uga/cs1302-styleguide), fix the error, and repeat until no style errors remain.