1 CS2136: Paradigms of Computation Class 11: Java: Javadoc Classes: Data, Methods, Encapsulation...
-
Upload
peregrine-bradford -
Category
Documents
-
view
224 -
download
1
Transcript of 1 CS2136: Paradigms of Computation Class 11: Java: Javadoc Classes: Data, Methods, Encapsulation...
![Page 1: 1 CS2136: Paradigms of Computation Class 11: Java: Javadoc Classes: Data, Methods, Encapsulation Copyright 2001, 2002, 2003 Michael J. Ciaraldi and David.](https://reader030.fdocuments.net/reader030/viewer/2022020117/56649f555503460f94c78db4/html5/thumbnails/1.jpg)
1
CS2136:Paradigms of Computation
Class 11:Java:
JavadocClasses: Data, Methods, Encapsulation
Copyright 2001, 2002, 2003 Michael J. Ciaraldi and David Finkel
![Page 2: 1 CS2136: Paradigms of Computation Class 11: Java: Javadoc Classes: Data, Methods, Encapsulation Copyright 2001, 2002, 2003 Michael J. Ciaraldi and David.](https://reader030.fdocuments.net/reader030/viewer/2022020117/56649f555503460f94c78db4/html5/thumbnails/2.jpg)
2
Javadoc
![Page 3: 1 CS2136: Paradigms of Computation Class 11: Java: Javadoc Classes: Data, Methods, Encapsulation Copyright 2001, 2002, 2003 Michael J. Ciaraldi and David.](https://reader030.fdocuments.net/reader030/viewer/2022020117/56649f555503460f94c78db4/html5/thumbnails/3.jpg)
3
JavadocPurpose: automatically generate
documentation. Where it applies (section 3.7 of
language spec)What’s included (section 18.1)Summary sentence (section 18.3)Tagged paragraphs (section 18.4)Invoking Javadoc
![Page 4: 1 CS2136: Paradigms of Computation Class 11: Java: Javadoc Classes: Data, Methods, Encapsulation Copyright 2001, 2002, 2003 Michael J. Ciaraldi and David.](https://reader030.fdocuments.net/reader030/viewer/2022020117/56649f555503460f94c78db4/html5/thumbnails/4.jpg)
4
Where it Applies(section 3.7 of language spec)
“Java defines three kinds of comments: /* text */ A traditional comment: all the text from the
ASCII characters /* to the ASCII characters */ is ignored (as in C and C++).
// text A single-line comment: all the text from the ASCII characters // to the end of the line is ignored (as in C++).
/** documentation */ A documentation comment: the text enclosed by the ASCII characters /** and */ can be processed by a separate tool to prepare automatically generated documentation of the following class, interface, constructor, or member (method or field) declaration.”
Remember, you need the two stars for a doc comment.
![Page 5: 1 CS2136: Paradigms of Computation Class 11: Java: Javadoc Classes: Data, Methods, Encapsulation Copyright 2001, 2002, 2003 Michael J. Ciaraldi and David.](https://reader030.fdocuments.net/reader030/viewer/2022020117/56649f555503460f94c78db4/html5/thumbnails/5.jpg)
5
What’s Included(section 18.1)
“The text of a documentation comment consists of the characters between the /** that begins the comment and the */ that ends it. The text is divided into one or more lines. On each of these lines, leading * characters are ignored; for lines other than the first, blanks and tabs preceding the initial * characters are also discarded.
So, for example, in the comment: /**XYZ ** Initialize to pre-trial defaults. 123*/ the text of the comment has three lines. The first
line consists of the text "XYZ"; the second line consists of the text " Initialize to pre-trial defaults." and the third line consists of the text "123“. “
![Page 6: 1 CS2136: Paradigms of Computation Class 11: Java: Javadoc Classes: Data, Methods, Encapsulation Copyright 2001, 2002, 2003 Michael J. Ciaraldi and David.](https://reader030.fdocuments.net/reader030/viewer/2022020117/56649f555503460f94c78db4/html5/thumbnails/6.jpg)
6
Summary Sentence(Section 18.3)
“The first sentence of each documentation comment should be a summary sentence, containing a concise but complete description of the declared entity. This sentence ends at the first period that is followed by a blank, tab, or line terminator, or at the first tagline (as defined below).”
![Page 7: 1 CS2136: Paradigms of Computation Class 11: Java: Javadoc Classes: Data, Methods, Encapsulation Copyright 2001, 2002, 2003 Michael J. Ciaraldi and David.](https://reader030.fdocuments.net/reader030/viewer/2022020117/56649f555503460f94c78db4/html5/thumbnails/7.jpg)
7
Tagged Paragraphs(section 18.4)
“A line of a documentation comment that begins with the character @ followed by one of a few special keywords starts a tagged paragraph. The tagged paragraph also includes any following lines up to, but not including, either the first line of the next tagged paragraph or the end of the documentation comment.”
You cannot define your own keywords.In general, you can write anything after
the keyword (not “anything after the ‘@’ ”).
![Page 8: 1 CS2136: Paradigms of Computation Class 11: Java: Javadoc Classes: Data, Methods, Encapsulation Copyright 2001, 2002, 2003 Michael J. Ciaraldi and David.](https://reader030.fdocuments.net/reader030/viewer/2022020117/56649f555503460f94c78db4/html5/thumbnails/8.jpg)
8
Tagged Paragraph Keywords
For classes and interfaces: @author @version
For methods and constructors: @param @return @exception
Anywhere: @see
![Page 9: 1 CS2136: Paradigms of Computation Class 11: Java: Javadoc Classes: Data, Methods, Encapsulation Copyright 2001, 2002, 2003 Michael J. Ciaraldi and David.](https://reader030.fdocuments.net/reader030/viewer/2022020117/56649f555503460f94c78db4/html5/thumbnails/9.jpg)
9
Invoking Javadoc
javadoc –private filename
E.g. javadoc –private Dot.javaE.g. javadoc –private *.java
![Page 10: 1 CS2136: Paradigms of Computation Class 11: Java: Javadoc Classes: Data, Methods, Encapsulation Copyright 2001, 2002, 2003 Michael J. Ciaraldi and David.](https://reader030.fdocuments.net/reader030/viewer/2022020117/56649f555503460f94c78db4/html5/thumbnails/10.jpg)
10
Classes
![Page 11: 1 CS2136: Paradigms of Computation Class 11: Java: Javadoc Classes: Data, Methods, Encapsulation Copyright 2001, 2002, 2003 Michael J. Ciaraldi and David.](https://reader030.fdocuments.net/reader030/viewer/2022020117/56649f555503460f94c78db4/html5/thumbnails/11.jpg)
11
Classes
Remember, in Java almost everything is either: a class (defines data and methods) an object (instance of a class)
![Page 12: 1 CS2136: Paradigms of Computation Class 11: Java: Javadoc Classes: Data, Methods, Encapsulation Copyright 2001, 2002, 2003 Michael J. Ciaraldi and David.](https://reader030.fdocuments.net/reader030/viewer/2022020117/56649f555503460f94c78db4/html5/thumbnails/12.jpg)
12
Defining a Class
Define the data fields (a.k.a. attributes).
Define the methods.
![Page 13: 1 CS2136: Paradigms of Computation Class 11: Java: Javadoc Classes: Data, Methods, Encapsulation Copyright 2001, 2002, 2003 Michael J. Ciaraldi and David.](https://reader030.fdocuments.net/reader030/viewer/2022020117/56649f555503460f94c78db4/html5/thumbnails/13.jpg)
13
Data
Can be: Primitives References to objects
You could have a class with only data (no methods). Like a data structure in C or Pascal. You would have to access the data
directly.
![Page 14: 1 CS2136: Paradigms of Computation Class 11: Java: Javadoc Classes: Data, Methods, Encapsulation Copyright 2001, 2002, 2003 Michael J. Ciaraldi and David.](https://reader030.fdocuments.net/reader030/viewer/2022020117/56649f555503460f94c78db4/html5/thumbnails/14.jpg)
14
Methods
Invoke them on the object.Specify zero or more arguments
(parameters).Return value can be any valid type
Including void.Disambiguate methods by their
signatures I.e., calling sequences. Not by return type.
![Page 15: 1 CS2136: Paradigms of Computation Class 11: Java: Javadoc Classes: Data, Methods, Encapsulation Copyright 2001, 2002, 2003 Michael J. Ciaraldi and David.](https://reader030.fdocuments.net/reader030/viewer/2022020117/56649f555503460f94c78db4/html5/thumbnails/15.jpg)
15
Allocating an Object:The Constructor
(Almost) every class has a constructor method. Constructor has same name as class. Triggered by keyword new.
A constructor might take a variable number of parameters. Including none at all!
![Page 16: 1 CS2136: Paradigms of Computation Class 11: Java: Javadoc Classes: Data, Methods, Encapsulation Copyright 2001, 2002, 2003 Michael J. Ciaraldi and David.](https://reader030.fdocuments.net/reader030/viewer/2022020117/56649f555503460f94c78db4/html5/thumbnails/16.jpg)
16
A Sample Class: DotSee Dot.java and DotTest.java
This class holds one point on a two-dimensional plane.
Need to access it with rectangular or polar coordinates.
![Page 17: 1 CS2136: Paradigms of Computation Class 11: Java: Javadoc Classes: Data, Methods, Encapsulation Copyright 2001, 2002, 2003 Michael J. Ciaraldi and David.](https://reader030.fdocuments.net/reader030/viewer/2022020117/56649f555503460f94c78db4/html5/thumbnails/17.jpg)
17
Coordinates
Rectangular x, y Measures distance from origin along x-
axis and y-axis.Polar
r, Θ (theta) Measures radius from origin, angle
counter-clockwise from x-axis.
![Page 18: 1 CS2136: Paradigms of Computation Class 11: Java: Javadoc Classes: Data, Methods, Encapsulation Copyright 2001, 2002, 2003 Michael J. Ciaraldi and David.](https://reader030.fdocuments.net/reader030/viewer/2022020117/56649f555503460f94c78db4/html5/thumbnails/18.jpg)
18
Coordinates ExampleThanks to Johns Hopkins Calculus II
http://mathnt.mat.jhu.edu/109/
![Page 19: 1 CS2136: Paradigms of Computation Class 11: Java: Javadoc Classes: Data, Methods, Encapsulation Copyright 2001, 2002, 2003 Michael J. Ciaraldi and David.](https://reader030.fdocuments.net/reader030/viewer/2022020117/56649f555503460f94c78db4/html5/thumbnails/19.jpg)
19
Coordinates Conversion
![Page 20: 1 CS2136: Paradigms of Computation Class 11: Java: Javadoc Classes: Data, Methods, Encapsulation Copyright 2001, 2002, 2003 Michael J. Ciaraldi and David.](https://reader030.fdocuments.net/reader030/viewer/2022020117/56649f555503460f94c78db4/html5/thumbnails/20.jpg)
20
Defining the Class:Fields & Constructors
import java.util.*;import java.lang.*;public class Dot { private double x, y; Dot() { // No parameters: use defaults x = 0.0d; y = 0.0d; } Dot(double xarg, double yarg) { // 2 parameters: use both x = xarg; y = yarg; }}
![Page 21: 1 CS2136: Paradigms of Computation Class 11: Java: Javadoc Classes: Data, Methods, Encapsulation Copyright 2001, 2002, 2003 Michael J. Ciaraldi and David.](https://reader030.fdocuments.net/reader030/viewer/2022020117/56649f555503460f94c78db4/html5/thumbnails/21.jpg)
21
Access Methods: Get
public double getX() { return x; }public double getY() { return y; }public double getR() { return Math.sqrt((x * x) + (y * y));}public double getTheta() { return Math.atan2(y, x);}
![Page 22: 1 CS2136: Paradigms of Computation Class 11: Java: Javadoc Classes: Data, Methods, Encapsulation Copyright 2001, 2002, 2003 Michael J. Ciaraldi and David.](https://reader030.fdocuments.net/reader030/viewer/2022020117/56649f555503460f94c78db4/html5/thumbnails/22.jpg)
22
Access Methods: Set
public void setX(double xarg) { x = xarg; }
public void setY(double yarg) { y = yarg; }
Setting with polar coordinates is left as an exercise for the interested student.
![Page 23: 1 CS2136: Paradigms of Computation Class 11: Java: Javadoc Classes: Data, Methods, Encapsulation Copyright 2001, 2002, 2003 Michael J. Ciaraldi and David.](https://reader030.fdocuments.net/reader030/viewer/2022020117/56649f555503460f94c78db4/html5/thumbnails/23.jpg)
23
toString()
public String toString() { // converts a dot's location to a string String ts; ts = "x = " + Double.toString(x) + " y = " + Double.toString(y); return ts; }Can be called any time, or automatically
by print() and println().
![Page 24: 1 CS2136: Paradigms of Computation Class 11: Java: Javadoc Classes: Data, Methods, Encapsulation Copyright 2001, 2002, 2003 Michael J. Ciaraldi and David.](https://reader030.fdocuments.net/reader030/viewer/2022020117/56649f555503460f94c78db4/html5/thumbnails/24.jpg)
24
Next Time
More Java: More on Classes and Inheritance