Docsity

Lecture Notes on Javadoc - Introduction to Programming in Java | CSIS 110, Study notes of Javascript programming

Toggle sidebar
bg1
CSIS 110 - Lecture 26
Announcements
Read 5.10 to 5.12.
Javadoc
Let's look at the Java API documentation again. Where does this all come from?
Look at the Dice programming assignment โ€“ where did the documentation for my class
come from?
Java provides a tool for generating this documentation right from your source code. The
tool is called javadoc, and you can access it directly from BlueJ.
javadoc generates documentation from the source code. Let's look at what it generates for
our tech-support project [Tool menu โ€“ project documentation].
Seems to be missing some information
- description of what each method does
- description of parameters
- description of return value
We need to add some information to our source files so that javadoc can generate the
appropriate documentation
Special form of comment โ€“ starts with /** This signifies a javadoc comment.
Placement of javadoc comments:
- before the class declaration to describe the class
- before a method declaration to describe the method
Special tags:
@version โ€“ in a class comment, to identify the version number of the class
@author โ€“ in a class comment
@param โ€“ in a method comment, to describe a formal parameter
@return โ€“ in a method comment, to describe the return value
You can also toggle from a source code view to a javadoc view directly in the BlueJ
editor window โ€“ use the interface view button.
IMPORTANT: use javadoc to describe what the method does โ€“ not how it does it. Don't
describe the code, describe the behavior. [Give an example].
EXPECTATION: I expect you to use javadoc comments in all your programs/labs from
this point on.
pf3
pf4
pf5

Partial preview of the text

Download Lecture Notes on Javadoc - Introduction to Programming in Java | CSIS 110 and more Study notes Javascript programming in PDF only on Docsity!

CSIS 110 - Lecture 26

Announcements Read 5.10 to 5.12. Javadoc Let's look at the Java API documentation again. Where does this all come from? Look at the Dice programming assignment โ€“ where did the documentation for my class come from? Java provides a tool for generating this documentation right from your source code. The tool is called javadoc, and you can access it directly from BlueJ. javadoc generates documentation from the source code. Let's look at what it generates for our tech-support project [Tool menu โ€“ project documentation]. Seems to be missing some information

  • description of what each method does
  • description of parameters
  • description of return value We need to add some information to our source files so that javadoc can generate the appropriate documentation Special form of comment โ€“ starts with /** This signifies a javadoc comment. Placement of javadoc comments:
  • before the class declaration to describe the class
  • before a method declaration to describe the method Special tags: @version โ€“ in a class comment, to identify the version number of the class @author โ€“ in a class comment @param โ€“ in a method comment, to describe a formal parameter @return โ€“ in a method comment, to describe the return value You can also toggle from a source code view to a javadoc view directly in the BlueJ editor window โ€“ use the interface view button. IMPORTANT: use javadoc to describe what the method does โ€“ not how it does it. Don't describe the code, describe the behavior. [Give an example]. EXPECTATION: I expect you to use javadoc comments in all your programs/labs from this point on.

public vs. private The key words public and private are access modifiers. Access modifiers define the visibility of fields, constructors, and methods. A public method or field can be accessed from within the same class, or from another class. A private method or field can be accessed from within the same class only. We just saw that there are two views of a class: the implementation view and the interface view. The interface is the set of details that someone using the class needs to see โ€“ it provides information about how to use the class. Sometimes this is referred to as the public part of the class. The implemenation view shows us exactly how the class works. This view includes the method bodies and the field declarations. The user of a class does not need to know about the implementation (abstraction again!) So, the public keyword declares a field or a method to be part of the interface. The private keyword declares it to be part of the implementation. There are good reasons for preventing a user of a class from knowing about the class's implementation โ€“ this principle is called information hiding.

  1. Programmers using a class should not need to know the internal details
  2. Methods using the class should not be allowed to know the internal details Why? Well, the first principle has to do with abstraction โ€“ we should be able to use a component without thinking about all of its details. The second principle has to do with modularization โ€“ we want to make sure that one class is not dependent upon how another class is implemented. Why? Well, let's say you needed to fix a bug in one class โ€“ you'd like to be able to fix it without having to change the implementation of other classes. This is much easier if you keep the implementation details of the class hidden, because then you are certain that the impact of your change is isolated to the class you are working on. Let's go back and look at the tech-support system again:

while ( input <= 0 ) { S.O.PL("The number must be greater than 0"); S.O.PL("Please enter the number of times to roll the dice" ); input = stdin.nextInt(); } This is called a user input validation loop. This form is very typical: Get input while ( input is invalid ) print error message get input

Lab Create a new project. Make a new class named Roots. This class should have one public method: public void printRoots() This method should:

  1. Ask the user for a positive , even number
  2. Validate the user input
  3. Print the even numbers and their square roots from 2 up to the user input value Your printRoots method may invoke private methods in the same class. Your solution may include as many private methods as you like. Use the Math.sqrt() method to calculate square roots. Here is an example showing how to use this method: double root = Math.sqrt( 6.2379 ); Here is an example of the output that should be produced by printRoots: Please enter a positive, even number: 5 That number is not positive and even. Please enter a positive, even number: 6 2 1. 4 2. 6 2. Don't forget to use appropriate javadoc comments! Email your Roots.java file to me. Use the subject "Lab 21".