9. Other OOP skills
Download
Report
Transcript 9. Other OOP skills
Chapter 9
Other object-oriented
programming skills
YG - CS170
1
Objectives
Skills
Add two or more classes to a package and make the classes in that
package available to other classes.
Add javadoc comments to the classes in one or more packages and
generate the documentation for those packages.
Use your web browser to view the documentation you added to a
package.
Code more than one class per file. When necessary, use nested classes.
Declare and use an enumeration.
Enhance an enumeration by adding methods that override the methods
of the Java and Enum classes. Use methods of the enumeration
constants when necessary.
YG - CS170
2
Objectives (continued)
Use a static import to import the constants of an enumeration or the
static fields and methods of a class.
Knowledge
List three reasons that you might store classes in a package.
Describe the general procedure for creating a directory structure for a
package.
Explain why you might add javadoc comments to the packages you
create.
Explain the purpose of using HTML and javadoc tags within a
javadoc comment.
Explain when you might code two or more classes in the same file
and describe the advantage and disadvantage of doing that.
YG - CS170
3
Objectives (continued)
Describe the difference between an inner class and a static inner class
in terms of how they’re related to the outer class.
Explain what a local class is.
Explain what an enumeration is and how you use one.
Explain what static imports are and how you use them.
YG - CS170
4
The directory structure for an
application that uses packages
lineItem
LineItemApp.java
murach
business
Product.java
LineItem.java
database
ProductDB.java
presentation
Validator.java
YG - CS170
5
How to store classes in a package
The classes in a package should be saved in a subdirectory with a
path that matches the name of the package.
The package name should be unique so you don’t have conflicts
with other packages.
To include a class in a package, code a package statement as the
first statement in the file that defines the class. Then, include an
import statement in any file that uses the package.
You can use packages to organize the classes that make up an
application. In that case, you should store the subdirectories that
make up the package beneath the directory for the application.
You can also use packages to make it easy to reuse a set of
classes. In that case, you should store the subdirectories that make
up the package in a central location, such as in the java directory.
YG - CS170
6
The LineItem class in the package
package murach.business;
import java.text.NumberFormat;
import murach.business.Product;
public class LineItem{...}
The Product class
package murach.business;
import java.text.NumberFormat;
public class Product{...}
YG - CS170
7
The ProductDB class in the package
package murach.database;
import murach.business.Product;
public class ProductDB{...}
The Validator class
package murach.presentation;
import java.util.Scanner;
public class Validator{...}
YG - CS170
8
How to compile the classes in a user-defined
package
1. Make sure that the classes have all of the appropriate package and
import statements.
2. Start the command prompt and navigate to the directory that
contains the package (the parent directory).
3. Use the javac command to compile the classes. Be sure to include
the subdirectories that correspond to the package name.
Syntax
c:\parentDir>javac packagePath/ClassName.java
Example
C:\java\lineItem>javac murach/business/LineItem.java
YG - CS170
9
Compilation details
If you compile a class that uses another class in the same
directory, both classes are compiled.
Before you compile a class that imports another class in the
package, you must compile the class it imports.
Some Java tools can’t compile a class that’s in a package that
depends on one or more classes in the same or in other userdefined packages. In that case, you need to use the javac
command to compile the class.
If you’re using DOS, you can use either front or back slashes to
separate the names of the directories in the package and the name
of the class file.
If you’re using Linux, you must use front slashes as separators.
YG - CS170
10
Making the classes of a package available to other
classes
When you compile a class that contains a package statement, the
class becomes part of the package and classes outside the package
can’t access it.
Once you make a package available to an application, you can
code import statements within the classes of the application to
import the necessary classes of the package.
YG - CS170
11
How to make a package available to the classes in
a single application
Copy the directories and files of the package to the root directory
of the application.
How to make a package available to the classes in
any application
1. Create a JAR file for the subdirectories and files of the package.
2. Move the JAR file to the SDK’s \jre\lib\ext directory.
Syntax for creating a JAR file for a package
c:\parentDir>jar cvf JARFilename.jar packagePath/*[.class]
YG - CS170
12
A statement that creates a JAR file for the murach
packages
C:\java\lineItem>jar cvf murach.jar murach/*
Resulting output
added manifest
adding: murach/business/(in = 0) (out= 0)(stored 0%)
adding: murach/business/LineItem.class(in = 1226) (out= 643)(deflated 47%)
adding: murach/business/LineItem.java(in = 1078) (out= 346)(deflated 67%)
adding: murach/business/Product.class(in = 1084) (out= 549)(deflated 49%)
adding: murach/business/Product.java(in = 940) (out= 308)(deflated 67%)
adding: murach/database/(in = 0) (out= 0)(stored 0%)
adding: murach/database/ProductDB.class(in = 829) (out= 541)(deflated 34%)
adding: murach/database/ProductDB.java(in = 1175) (out= 413)(deflated 64%)
adding: murach/presentation/(in = 0) (out= 0)(stored 0%)
adding: murach/presentation/Validator.class(in = 2057) (out= 1072)(deflated 47%)
adding: murach/presentation/Validator.java(in = 2357) (out= 571)(deflated 75%)
The statements for importing the classes in the
packages
import murach.business.*;
import murach.database.*;
import murach.presentation.*;
YG - CS170
13
A statement that excludes the source files from a
JAR file
C:\java\lineItem>jar cvf murach.business.jar murach/business/*.class
Note
If you don’t want to include the source files for a package in a
JAR file, you can code the .class extension in the file specification
on the jar command. Then, only the class files will be included.
YG - CS170
14
The Product class with javadoc comments
package murach.business;
import java.text.NumberFormat;
/**********************************************************
* The Product class represents a product and is used by
* the LineItem and ProductDB classes.
**********************************************************/
public class Product
{
private String code;
private String description;
private double price;
/***********************************************************
* Creates a new Product with default values.
***********************************************************/
public Product()
{
code = "";
description = "";
price = 0;
}
YG - CS170
15
The Product class with javadoc comments
(continued)
/***********************************************************
* Sets the product code to the specified String.
***********************************************************/
public void setCode(String code)
{
this.code = code;
}
/***********************************************************
* Returns a String that represents the product code.
***********************************************************/
public String getCode(){
return code;
}
.
.
.
YG - CS170
16
How to add javadoc comments to a class
A javadoc comment begins with /** and ends with */, and
asterisks within the comment are ignored.
You can use javadoc comments to describe a class and the public
and protected fields, constructors, and methods it contains.
A comment should be placed immediately above the class or
member it describes. For a class, that means that the comment
must be placed after any import statements.
YG - CS170
17
How to use HTML and javadoc tags in javadoc
comments
Within a javadoc comment, you can code HTML tags to format
the text that’s displayed.
You can also include javadoc tags to include special entries in the
documentation.
YG - CS170
18
Common HTML tag used to format javadoc
comments
HTML tag
<code></code>
Description
Displays the text between these tags with a
monospaced font.
Common javadoc tags
Javadoc tag
@author
@version
@param
@return
Description
Identifies the author of the class. Not displayed by
default.
Describes the current version of the class. Not
displayed by default.
Describes a parameter of a constructor or method.
Describes the value that’s returned by a method.
YG - CS170
19
The Product class with comments that use HTML
and javadoc tags
package murach.business;
import java.text.NumberFormat;
/**********************************************************
* The <code>Product</code> class represents a product and is used
* by the <code>LineItem</code> and <code>ProductDB</code> classes.
* @author Joel Murach
* @version 1.0.0
**********************************************************/
public class Product
{
private String code;
private String description;
private double price;
/***********************************************************
* Creates a <code>Product</code> with default values.
***********************************************************/
public Product()
{
code = "";
description = "";
price = 0;
}
YG - CS170
20
The Product class with comments that use HTML
and javadoc tags (continued)
/***********************************************************
* Sets the product code to the specified <code>String</code>.
* @param code A <code>String</code> for the product code.
***********************************************************/
public void setCode(String code)
{
this.code = code;
}
/***********************************************************
* Returns a <code>String</code> that represents the product code.
* @return A <code>String</code> for the product code.
***********************************************************/
public String getCode()
{
return code;
}
...
YG - CS170
21
How to generate and view the documentation for a
package
The javadoc tool lets you generate HTML-based documentation
like the documentation for the Java API.
You can use the javadoc tool to generate documentation for one or
more packages or for a single class.
You can view the documentation that’s generated by the javadoc
command by starting a web browser and pointing to the
index.html file that’s created in the directory you specify.
YG - CS170
22
The syntax for generating documentation for one
or more packages
C:\parentDir>javadoc –d docsDirectory packageName1
[packageName2]...
A statement that generates documentation for a
single package
C:\java\lineItem>javadoc -d c:\java\lineItem\docs\business murach.business
A statement that generates documentation for
three packages
C:\java\lineItem>javadoc -d c:\java\lineItem\docs murach.business
murach.database murach.presentation
YG - CS170
23
The documentation that’s generated for the
three murach packages
YG - CS170
24
Four Different Ways to Write a Java Application
with More than One Class
Write classes separately, but saved in the same
directory
Write classes in the same file/program. The access
modifier for supporting class is not allowed (see
example in the next slide)
Write the class(es) using package access
Write the supporting class(es) as inner/nested
class(es). The access modifier for supporting class
is not allowed (see example in the next slide)
YG - CS170
25
Two classes declared within the same file
import java.text.NumberFormat;
public class LineItem
{
private Product product;
private int quantity;
private double total;
.
.
.
}
class Product
{
// body of Product class
}
YG - CS170
26
The class files that are generated when the code
is compiled
LineItem.class
Product.class
How to code more than one class per file
When you code two or more classes in the same file, you can only
have one public class in the file, and that class should be declared
first.
YG - CS170
27
An introduction to nested classes
Java has provided support for nested/inner classes since version
1.1.
When you nest classes, the outer class must be declared public and
must have the same name as the file name of the class.
Within an outer class, you can nest inner classes and static inner
classes. Since the inner classes are members of the outer class, they
are sometimes called member classes.
A class can also be nested inside a method or any other type of
block. These types of classes are sometimes called local classes.
YG - CS170
28
Two classes nested within another class
public class OuterClassName
{
// can contain instance variables and methods
// can contain static variables and methods
class InnerClassName
{
// can contain instance variables and methods
// can't contain static variables or methods
// can access all variables and methods of
//
OuterClass
}
static
{
//
//
//
//
//
}
class StaticInnerClassName
can contain instance variables and methods
can contain static variables and methods
can access static data from OuterClass
can't access instance variables or methods from
OuterClass
}
YG - CS170
29
The class files generated for the preceding class
OuterClassName.class
OuterClassName$InnerClassName.class
OuterClassName$StaticInnerClassName.class
YG - CS170
30
A class nested within a method
public class ClassName
{
// code for the outer class
public void methodName()
{
class InnerClassName
{
// code for the inner class
}
// code for the method
}
}
The class files generated for this class
ClassName.class
ClassName$InnerClassName.class
YG - CS170
31
The syntax for declaring an enumeration
public enum EnumerationName
{
CONSTANT_NAME1[,
CONSTANT_NAME2]...
}
How to declare and work with enumerations
An enumeration contains a set of related constants.
The constants are defined with the int type and are assigned values
from 0 to the number of constants in the enumeration minus 1.
An enumeration defines a type. Because of that, you can’t specify
another type where an enumeration type is expected. That means
that enumerations are type-safe.
YG - CS170
32
An enumeration that defines three shipping types
public enum ShippingType
{
UPS_NEXT_DAY,
UPS_SECOND_DAY,
UPS_GROUND
}
A statement that uses the enumeration and one of
its constants
ShippingType secondDay = ShippingType.UPS_SECOND_DAY;
YG - CS170
33
A method that uses the shipping enumeration as
a parameter type
public static double getShippingAmount(ShippingType st)
{
double shippingAmount = 2.99;
if (st == ShippingType.UPS_NEXT_DAY)
shippingAmount = 10.99;
else if (st == ShippingType.UPS_SECOND_DAY)
shippingAmount = 5.99;
return shippingAmount;
}
A statement that calls this method
double shippingAmount =
getShippingAmount(ShippingType.UPS_SECOND_DAY);
// double shippingAmount2 = getShippingAmount(1);
// Wrong type, not allowed
YG - CS170
34
Two methods of an enumeration constant
Method
name()
ordinal()
Description
Returns a String for the enumeration constant’s name.
Returns an int value that corresponds to the
enumeration constant’s position.
YG - CS170
35
How to add a method to an enumeration
An enumeration that overrides the toString method
public enum ShippingType
{
UPS_NEXT_DAY,
UPS_SECOND_DAY,
UPS_GROUND;
public String toString()
{
String s = "";
if (this.ordinal() == 0)
s = "UPS Next Day (1 business day)";
else if (this.ordinal() == 1)
s = "UPS Second Day (2 business days)";
else if (this.ordinal() == 2)
s = "UPS Ground (5 to 7 business days)";
return s;
}
}
YG - CS170
36
How to add a method to an enumeration
(continued)
Code that uses the toString method
ShippingType ground = ShippingType.UPS_GROUND;
System.out.println("toString: " + ground.toString() +
"\n");
Resulting output
toString: UPS Second Day (2 business days)
How to work with static imports
How to code a static import statement
import static murach.business.ShippingType.*;
The code above when a static import is used
ShippingType ground = UPS_GROUND;
System.out.println("toString: " + ground.toString() +
"\n");
YG - CS170
37
How to enhance an enumeration and work with
static imports
All enumerations inherit the java.lang.Object and java.lang.Enum
classes. They can use or override the methods of those classes or
add new methods.
By default, the toString method of an enumeration constant
returns the same string as the name method.
You can use a static import to import all of the constants of an
enumeration or all of the static fields and methods of a class.
When you use a static import, you don’t need to qualify the
constant, field, or method with the name of the enumeration or
class.
YG - CS170
38