SW08 - Object-Oriented Programming and Principles
More sophisticated behavior
Using library classes to implement
some more advanced functionality
2.0
Main concepts to be covered
• Using library classes
• Reading documentation
• Writing documentation
© M. Kölling, University of Southern Denmark 1
SW08 - Object-Oriented Programming and Principles
The Java class library
• Thousands of classes
• Tens of thousands of methods
• Many useful classes that make life
much easier
• A competent Java programmer must
be able to work with the libraries.
Working with the library
You should:
• know some important classes by
name;
• know how to find out about other
classes.
Remember:
• We only need to know the interface,
not the implementation.
© M. Kölling, University of Southern Denmark 2
SW08 - Object-Oriented Programming and Principles
Main loop structure
boolean finished = false;
while(!finished) {
do something
if(exit condition) {
finished = true;
}
else {
do something more
}
}
Main loop body
String input = [Link]();
...
String response = [Link]();
[Link](response);
© M. Kölling, University of Southern Denmark 3
SW08 - Object-Oriented Programming and Principles
The exit condition
String input = [Link]();
if([Link]("bye")) {
finished = true;
}
• Where does ‘startsWith’ come from?
• What is it? What does it do?
• How can we find out?
Reading class documentation
• Documentation of the Java libraries
in HTML format;
• Readable in a web browser
• Class API: Application Programmers’
Interface
• Interface description for all library
classes
© M. Kölling, University of Southern Denmark 4
SW08 - Object-Oriented Programming and Principles
Interface vs implementation
The documentation includes
• the name of the class;
• a general description of the class;
• a list of constructors and methods
• return values and parameters for
constructors and methods
• a description of the purpose of each
constructor and method
the interface of the class
Interface vs implementation
The documentation does not include
• private fields (most fields are private)
• private methods
• the bodies (source code) for each method
the implementation of the class
10
© M. Kölling, University of Southern Denmark 5
SW08 - Object-Oriented Programming and Principles
Using library classes
• Classes from the library must be
imported using an import statement
(except classes from [Link]).
• They can then be used like classes
from the current project.
11
Packages and import
• Classes are organised in packages.
• Single classes may be imported:
import [Link];
• Whole packages can be imported:
import [Link].*;
12
© M. Kölling, University of Southern Denmark 6
SW08 - Object-Oriented Programming and Principles
Side note: String equality
if(input == "bye") { tests identity
...
}
if([Link]("bye")) { tests equality
...
}
• Strings should (almost) always be compared with .equals
13
Identity vs equality 1
Other (non-String) objects:
:Person :Person
“Fred” “Jill”
person1 person2
person1 == person2 ?
14
© M. Kölling, University of Southern Denmark 7
SW08 - Object-Oriented Programming and Principles
Identity vs equality 2
Other (non-String) objects:
:Person :Person
“Fred” “Fred”
person1 person2
person1 == person2 ?
15
Identity vs equality 3
Other (non-String) objects:
:Person :Person
“Fred” “Fred”
person1 person2
person1 == person2 ?
16
© M. Kölling, University of Southern Denmark 8
SW08 - Object-Oriented Programming and Principles
Identity vs equality (Strings)
String input = [Link]();
== tests identity
if(input == "bye") {
...
}
:String :String
"bye" == "bye" ?
input
à (may be) false!
17
Identity vs equality (Strings)
String input = [Link](); equals tests
if([Link]("bye")) { equality
...
}
:String :String
"bye"
equals "bye" ?
input
à true!
18
© M. Kölling, University of Southern Denmark 9
SW08 - Object-Oriented Programming and Principles
Using Random
• The library class Random can be used
to generate random numbers
import [Link];
...
Random randomGenerator = new Random();
...
int index1 = [Link]();
int index2 = [Link](100);
19
Generating random responses
public Responder()
{
randomGenerator = new Random();
responses = new ArrayList();
fillResponses();
}
public String generateResponse()
{
int index = [Link]([Link]());
return (String) [Link](index);
}
public void fillResponses()
...
20
© M. Kölling, University of Southern Denmark 10
SW08 - Object-Oriented Programming and Principles
Maps
• Maps are collections that contain
pairs of values.
• Pairs consist of a key and a value.
• Lookup works by supplying a key, and
retrieving a value.
• An example: a telephone book.
21
Using maps
• A map with Strings as keys and values
:HashMap
"Charles Nguyen" "(531) 9392 4587"
"Lisa Jones" "(402) 4536 4674"
"William H. Smith" "(998) 5488 0123"
22
© M. Kölling, University of Southern Denmark 11
SW08 - Object-Oriented Programming and Principles
Using maps
HashMap phoneBook = new HashMap();
[Link]("Charles Nguyen", "(531) 9392 4587");
[Link]("Lisa Jones", "(402) 4536 4674");
[Link]("William H. Smith", "(998) 5488 0123");
String number = (String)[Link]("Lisa Jones");
[Link](number);
23
Using sets
import [Link];
import [Link];
...
HashSet mySet = new HashSet();
Compare this
[Link]("one"); to ArrayList
[Link]("two"); code!
[Link]("three");
Iterator it = [Link]();
while([Link]()) {
call [Link]() to get the next object
do something with that object
}
24
© M. Kölling, University of Southern Denmark 12
SW08 - Object-Oriented Programming and Principles
Tokenizing Strings
public HashSet getInput()
{
[Link]("> ");
String inputLine =
readInputLine().trim().toLowerCase();
StringTokenizer tokenizer =
new StringTokenizer(inputLine);
HashSet words = new HashSet();
while([Link]()) {
[Link]([Link]());
}
return words;
}
25
Writing class documentation
• Your own classes should be
documented the same way library
classes are.
• Other people should be able to use
your class without reading the
implementation.
• Make your class a 'library class'!
26
© M. Kölling, University of Southern Denmark 13
SW08 - Object-Oriented Programming and Principles
Elements of documentation
Documentation for a class should include:
• the class name
• a comment describing the overall purpose
and characteristics of the class
• a version number
• the authors’ names
• documentation for each constructor and
each method
27
Elements of documentation
The documentation for each constructor and
method should include:
• the name of the method
• the return type
• the parameter names and types
• a description of the purpose and function
of the method
• a description of each parameter
• a description of the value returned
28
© M. Kölling, University of Southern Denmark 14
SW08 - Object-Oriented Programming and Principles
javadoc
Class comment:
/**
* The Responder class represents a response
* generator object. It is used to generate an
* automatic response.
*
* @author Michael Kölling and David J. Barnes
* @version 1.0 ([Link].2002)
*/
29
javadoc
Method comment:
/**
* Read a line of text from standard input (the text
* terminal), and return it as a set of words.
*
* @param prompt A prompt to print to screen.
* @return A set of Strings, where each String is
* one of the words typed by the user
*/
public HashSet getInput(String prompt)
{
...
}
30
© M. Kölling, University of Southern Denmark 15
SW08 - Object-Oriented Programming and Principles
Public vs private
• Public attributes (fields,
constructors, methods) are accessible
to other classes.
• Fields should not be public.
• Private attributes are accessible only
within the same class.
• Only methods that are intended for
other classes should be public.
31
Information hiding
• Data belonging to one object is hidden
from other objects.
• Know what an object can do, not how
it does it.
• Information hiding increases the level
of independence.
• Independence of modules is important
for large systems and maintenance.
32
© M. Kölling, University of Southern Denmark 16
SW08 - Object-Oriented Programming and Principles
Class variables
33
Constants
private static final int gravity = 3;
• private: access modifier, as usual
• static: class variable
• final: constant
34
© M. Kölling, University of Southern Denmark 17
SW08 - Object-Oriented Programming and Principles
Review
• Java has an extensive class library.
• A good programmer must be familiar with
the library.
• The documentation tells us what we need
to know to use a class (interface).
• The implementation is hidden (information
hiding).
• We document our classes so that the
interface can be read on its own (class
comment, method comments).
35
© M. Kölling, University of Southern Denmark 18