Download presentation
Presentation is loading. Please wait.
1
CMPE212 – Reminders Assignment 2 due in a week.
Winter 2019 CMPE212 5/10/2019 CMPE212 – Reminders Assignment 2 due in a week. Assn 1 solution posted. Halloween zip archive linked to the lecture notes page has been updated. Winter 2019 CMPE212 - Prof. McLeod Prof. Alan McLeod
2
Today Encapsulation, Cont. Javadoc Documentation. Winter 2019
Winter 2019 CMPE212 5/10/2019 Today Encapsulation, Cont. Javadoc Documentation. Winter 2019 CMPE212 - Prof. McLeod Prof. Alan McLeod
3
Halloween Encapsulation, So Far
private attributes. Overloaded constructors. Mutators. Accessors. toString(). equals(). Next: compareTo(), clone(), some refinement to yield Halloween5, and Javadoc comments. Winter 2019 CMPE212 - Prof. McLeod
4
compareTo() Method Compares a supplied Halloween4 object with the current one, based on your comparison criteria. It returns an int value. (Like the compareTo() method in the String class.) You have to decide on the basis for comparison. For Halloween4 you could just use numMunchkins, for example. The base Object class does not have this method, so you don’t have to worry about writing one to take an Object as a parameter. Winter 2019 CMPE212 - Prof. McLeod
5
clone() Method Returns a deep copy of the current object.
A “deep copy” is not aliased in any way to the original object, but contains the same values for each attribute. Why not just write return this; ? See TestHalloween4.java for our object in action! Winter 2019 CMPE212 - Prof. McLeod
6
Test Halloween4 TestHalloween4 puts the Halloween4 class through its paces. Note the poor attempt to deal with the dependent attribute problem in Halloween4: If you wanted to edit {12, 10, 11, 9} and “rain” to {-6, -7, -9} and “snow”, you would have to change the weather string to “clear” or “unknown” first and then change it to the actual value after you changed the temperatures. Yuk! Winter 2019 CMPE212 - Prof. McLeod
7
Exercises and Assignment
You are now able to work on both Exercises 7 and 8. After we see how to implement a couple of interfaces in Halloween5 and discuss Javadoc commenting you will know what you need for assignment 3. Winter 2019 CMPE212 - Prof. McLeod
8
Halloween5 Same structure as Halloween4, but the weather mutator is fixed and it has implemented two interfaces: Comparable<Halloween5> This is a generic interface that allows this object to be sorted with sort methods built into Java. Serializable This allows this object to be saved directly to a file. (Both of these are used in Assignment 3.) Winter 2019 CMPE212 - Prof. McLeod
9
Aside – What is an Interface?
An interface is a totally abstract class – none of the methods in the class are implemented – they don’t contain any concrete code. (Weird!) Instead of doing something – an interface acts as a design specification and can be used as a type. A class that implements an interface promises to have concrete versions of all the abstract methods in the interface. More on these little beasties later. Winter 2019 CMPE212 - Prof. McLeod
10
Halloween5, Cont. See the changes in Halloween5.
See TestHalloween5.java for tests that focus on what the interface implementations allow us to do with this class that we could not do with Halloween4. Winter 2019 CMPE212 - Prof. McLeod
11
Aside - Serialization Uses ObjectOutputStream and ObjectInputStream to read and write an entire object to a file with a single method call. The file will contain the structure of the object as well as the current values of all the object’s attributes. If a class’ attribute cannot be serialized for some reason, then mark it as transient and it will not be saved to the file. The .readObject() method from ObjectInputStream returns an Object which must then be cast to the proper type. Winter 2019 CMPE212 - Prof. McLeod
12
Aside – The serialVersionUID Thing
This is a unique code ID value that is used to identify your object. Auto-generation uses various aspects of the structure of your class to generate this value. If you change the structure, you should re-generate this number. It is saved with the contents of your object in the file (“serialized”). When you read the object (“deserialize”), the number is compared to the number stored in the class to make sure the cast will work. If readObject() cannot find the bytecode file of the definition of your class, you will get a ClassNotFoundException. Winter 2019 CMPE212 - Prof. McLeod
13
The Comparable<T> Interface
More on this interface later, but: We have already implemented this interface by the way we wrote compareTo(Halloween5), so no further changes are required. However, it is important for us to implement the interface, so our instance can be typed to be of type Comparable<Halloween5>. Winter 2019 CMPE212 - Prof. McLeod
14
Variations on a Theme… This standard encapsulated class structure is followed by every class in Java. Some classes don’t need all the pieces. For example: Attributes but no mutators. (Immutable) No attributes – then you don’t need constructors, mutators or accessors. (This class is probably just composed of utility methods, maybe all static.) An object that will never be part of a collection – you might not need equals() or compareTo(). A class that just has a main method. This is just the starting point of a program – possibly no other methods are required. Winter 2019 CMPE212 - Prof. McLeod
15
More Variations Of course, a class may need more methods than those we have discussed. Think of the additional behaviour that will be useful to the user of your object. A majority of methods should have something to do with the attributes of the object. But you might add a few static methods if you cannot think of anywhere else to put them (the wrapper classes are like this…). Winter 2019 CMPE212 - Prof. McLeod
16
Fall 2013 CMPE212 Javadoc Javadoc.exe is a program that comes with the JDK. (It is in the same directory as javac.exe and java.exe – the “bin” folder). It is not included in the JRE only. If I have written a class, “MyClass.java”, that contains properly formatted comments (more below), then running “javadoc MyClass.java” generates a file “MyClass.html”. (And lots of other files, actually.) The html file contains external documentation generated from the formatted comments in the source code. Winter 2019 CMPE212 - Prof. McLeod Prof. Alan McLeod
17
Javadoc - Cont. Normal block comments in Java are delimited by “/*” and “*/”. Everything between these delimiters, usually multiple lines, is a comment. Javadoc block comments are delimited by “/**” and “*/”. Winter 2019 CMPE212 - Prof. McLeod
18
Javadoc - Cont. The general form of a Javadoc comment: /**
* Summary sentence. * More general information about the * class, attribute or method which * follows the comment, using as many lines * as necessary. (html tags can be included) * * javadoc tags to specify more specific * information, such as parameters and * return values for a method */ Winter 2019 CMPE212 - Prof. McLeod
19
Javadoc - Cont. The general form of a Javadoc tag is:
@tagName comment The tags you use depend on what you are describing (class, method or attribute). In the case of methods, you can have a tag for each parameter, the return value, and a tag for each thrown exception. Eclipse (really nice!!) will generate a blank tag for you after you type “/**”. Typically, you will only write javadoc comments for public attributes and methods… Winter 2019 CMPE212 - Prof. McLeod
20
Eclipse Javadoc Assist
For example, if you have the method header: public static double[] generateRandomArray(int size) Type /** right above this line and press <enter>. You will get: /** * size */ Winter 2019 CMPE212 - Prof. McLeod
21
Eclipse Javadoc Assist, Cont.
Then you have to finish the comment: /** * Generates an array of random doubles. * * The method uses the Math.random() method to generate * an array of doubles between 0.0 and 1.0. size The desired size of the array. The array of random doubles. */ Winter 2019 CMPE212 - Prof. McLeod
22
Eclipse Javadoc Assist, Cont.
Hold your cursor over the method header and you will get a preview of what the processed Javadoc will look like: (Or view the Javadoc tag in the same view as the Console tag.) Winter 2019 CMPE212 - Prof. McLeod
23
Common Javadoc Tags @param Parameter_name description @throws
Exception_name description @return description @see packageName.ClassName, packageNamme.ClassName#methodName, etc. @author @version Winter 2019 CMPE212 - Prof. McLeod
24
Javadoc - Cont. Html tags can also be added to the comments to add more formatting to the resulting document: <em> for emphasis <code> for code font <img> for images <ul><li> </li></ul> for bulleted lists Etc… Winter 2019 CMPE212 - Prof. McLeod
25
Javadoc Reference The best reference for javadoc is at:
Winter 2019 CMPE212 - Prof. McLeod
26
Javadoc - Cont. The output from Javadoc looks exactly like the API documentation you have already seen - since that is the way it has been generated! The advantage is that when source code is changed, the Javadoc comments can be changed in the source, at the same time. The external documentation is then easily re-generated. Javadoc also provides a consistent look and feel to these API documents. Winter 2019 CMPE212 - Prof. McLeod
27
Javadoc - Cont. Most modern IDE’s (like NetBeans and Eclipse) allow you to run Javadoc from within the environment, and provide tools and wizards to help you create comments. In Eclipse, select “Project”, then “Generate Javadoc…”. Let’s run javadoc on the Halloween5 class. (Note that the first time you do this, you will have to tell Eclipse where javadoc.exe resides.) Winter 2019 CMPE212 - Prof. McLeod
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.