SlideShare a Scribd company logo
1 of 44
More sophisticated behavior
Using library classes to implement
some more advanced functionality
5.0
2
Main concepts to be covered
• Using library classes
• Reading documentation
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
3
The Java class library
• Thousands of classes.
• Tens of thousands of methods.
• Many useful classes that make life
much easier.
• Library classes are often inter-
related.
• Arranged into packages.
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
4
Working with the library
• A competent Java programmer must
be able to work with the libraries.
• 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.
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
5
A Technical Support System
• A textual, interactive dialog system
• Idea based on ‘Eliza’ by Joseph
Weizenbaum (MIT, 1960s)
• Explore tech-support-complete …
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
6
Main loop structure
boolean finished = false;
while(!finished) {
do something
if(exit condition) {
finished = true;
}
else {
do something more
}
}
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
A common
iteration
pattern.
7
Main loop body
String input = reader.getInput();
...
String response = responder.generateResponse();
System.out.println(response);
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
8
The exit condition
String input = reader.getInput();
if(input.startsWith("bye")) {
finished = true;
}
• Where does ‘startsWith’ come
from?
• What is it? What does it do?
• How can we find out?
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
9
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
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
10Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
11
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
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
12
Interface vs implementation
The documentation does not include
• private fields (most fields are private)
• private methods
• the bodies (source code) of methods
the implementation of the class
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
13
Documentation for
startsWith
• startsWith
– public boolean startsWith(String prefix)
• Tests if this string starts with the
specified prefix.
• Parameters:
– prefix - the prefix.
• Returns:
– true if the …; false otherwise
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
14
Methods from String
• contains
• endsWith
• indexOf
• substring
• toUpperCase
• trim
• Beware: strings are immutable!
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
15
Using library classes
• Classes organized into packages.
• Classes from the library must be
imported using an import statement
(except classes from the java.lang
package).
• They can then be used like classes
from the current project.
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
16
Packages and import
• Single classes may be imported:
import java.util.ArrayList;
• Whole packages can be imported:
import java.util.*;
• Importation does not involve source
code insertion.
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
17
Using Random
• The library class Random can be used
to generate random numbers
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
import java.util.Random;
...
Random rand = new Random();
...
int num = rand.nextInt();
int value = 1 + rand.nextInt(100);
int index = rand.nextInt(list.size());
18
Selecting random responses
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
public Responder()
{
randomGenerator = new Random();
responses = new ArrayList<String>();
fillResponses();
}
public void fillResponses()
{
fill responses with a selection of response strings
}
public String generateResponse()
{
int index = randomGenerator.nextInt(responses.size());
return responses.get(index);
}
19
Parameterized classes
• The documentation includes provision
for a type parameter:
– ArrayList<E>
• These type names reappear in the
parameters and return types:
– E get(int index)
– boolean add(E e)
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
20
Parameterized classes
• The types in the documentation are
placeholders for the types we use in
practice:
– An ArrayList<TicketMachine>
actually has methods:
– TicketMachine get(int index)
– boolean add(TicketMachine e)
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
21
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 (its interface).
• Some classes are parameterized with
additional types.
• Parameterized classes are also known as
generic classes or generic types.
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
More sophisticated behavior
Using library classes to implement
some more advanced functionality
23
Main concepts to be covered
• Further library classes
• Set
• Map
• Writing documentation
• javadoc
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
24
Using sets
import java.util.HashSet;
...
HashSet<String> mySet = new HashSet<String>();
mySet.add("one");
mySet.add("two");
mySet.add("three");
for(String element : mySet) {
do something with element
}
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
Compare
with code
for an
ArrayList!
25
Tokenising Strings
public HashSet<String> getInput()
{
System.out.print("> ");
String inputLine =
reader.nextLine().trim().toLowerCase();
String[] wordArray = inputLine.split(" ");
HashSet<String> words = new HashSet<String>();
for(String word : wordArray) {
words.add(word);
}
return words;
}
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
26
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.
• Example: a telephone book.
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
27
Using maps
• A map with strings as keys and values
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
"Charles Nguyen"
:HashMap
"(531) 9392 4587"
"Lisa Jones" "(402) 4536 4674"
"William H. Smith" "(998) 5488 0123"
28
Using maps
HashMap <String, String> phoneBook =
new HashMap<String, String>();
phoneBook.put("Charles Nguyen", "(531) 9392 4587");
phoneBook.put("Lisa Jones", "(402) 4536 4674");
phoneBook.put("William H. Smith", "(998) 5488 0123");
String phoneNumber = phoneBook.get("Lisa Jones");
System.out.println(phoneNumber);
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
29
List, Map and Set
• Alternative ways to group objects.
• Varying implementations available:
– ArrayList, LinkedList
– HashSet, TreeSet
• But HashMap is unrelated to
HashSet, despite similar names.
• The second word reveals
organizational relatedness.
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
30
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 potential 'library
class'!
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
31
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
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
32
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
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
33
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 (2011.07.31)
*/
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
34
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<String> getInput(String prompt)
{
...
}
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
35
Public vs private
• Public elements are accessible to
objects of other classes:
• Fields, constructors and methods
• Fields should not be public.
• Private elements are accessible only
to objects of the same class.
• Only methods that are intended for
other classes should be public.
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
36
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.
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
37
Code completion
• The BlueJ editor supports lookup of
methods.
• Use Ctrl-space after a method-call
dot to bring up a list of available
methods.
• Use Return to select a highlighted
method.
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
38
Code completion in BlueJ
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
39
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).
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
Class and constant variables
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
41
Class variables
• A class variable is shared between all
instances of the class.
• In fact, it belongs to the class and
exists independent of any instances.
• Designated by the static keyword.
• Public static variables are accessed
via the class name; e.g.:
– Thermometer.boilingPoint
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
42
Class variables
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
43
Constants
• A variable, once set, can have its
value fixed.
• Designated by the final keyword.
– final int max = list.size();
• Final fields must be set in their
declaration or the constructor.
• Combing static and final is
common.
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
44
Class constants
• static: class variable
• final: constant
private static final int gravity = 3;
• Public visibility is less of an issue
with final fields.
• Upper-case names often used for
class constants:
public static final int BOILING_POINT = 100;
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

More Related Content

What's hot

Groovy DSLs (JavaOne Presentation)
Groovy DSLs (JavaOne Presentation)Groovy DSLs (JavaOne Presentation)
Groovy DSLs (JavaOne Presentation)Jim Driscoll
 
Oop in php lecture 2
Oop in  php lecture 2Oop in  php lecture 2
Oop in php lecture 2Mudasir Syed
 
Dojo for programmers (TXJS 2010)
Dojo for programmers (TXJS 2010)Dojo for programmers (TXJS 2010)
Dojo for programmers (TXJS 2010)Eugene Lazutkin
 
MVC and Entity Framework
MVC and Entity FrameworkMVC and Entity Framework
MVC and Entity FrameworkJames Johnson
 
Java basic part 2 : Datatypes Keywords Features Components Security Exceptions
Java basic part 2 : Datatypes Keywords Features Components Security Exceptions Java basic part 2 : Datatypes Keywords Features Components Security Exceptions
Java basic part 2 : Datatypes Keywords Features Components Security Exceptions Soumen Santra
 
A Tour Through the Groovy Ecosystem
A Tour Through the Groovy EcosystemA Tour Through the Groovy Ecosystem
A Tour Through the Groovy EcosystemLeonard Axelsson
 
Vk.amberfog.com gtug part1_introduction2_javaandroid_gtug
Vk.amberfog.com gtug part1_introduction2_javaandroid_gtugVk.amberfog.com gtug part1_introduction2_javaandroid_gtug
Vk.amberfog.com gtug part1_introduction2_javaandroid_gtugketan_patel25
 
Core Java- An advanced review of features
Core Java- An advanced review of featuresCore Java- An advanced review of features
Core Java- An advanced review of featuresvidyamittal
 
Java 102 intro to object-oriented programming in java - exercises
Java 102   intro to object-oriented programming in java - exercisesJava 102   intro to object-oriented programming in java - exercises
Java 102 intro to object-oriented programming in java - exercisesagorolabs
 
Lecture 13, 14 & 15 c# cmd let programming and scripting
Lecture 13, 14 & 15   c# cmd let programming and scriptingLecture 13, 14 & 15   c# cmd let programming and scripting
Lecture 13, 14 & 15 c# cmd let programming and scriptingWiliam Ferraciolli
 
Clojure, Plain and Simple
Clojure, Plain and SimpleClojure, Plain and Simple
Clojure, Plain and SimpleBen Mabey
 
Javascript Prototype Visualized
Javascript Prototype VisualizedJavascript Prototype Visualized
Javascript Prototype Visualized军 沈
 
Java - OOPS and Java Basics
Java - OOPS and Java BasicsJava - OOPS and Java Basics
Java - OOPS and Java BasicsVicter Paul
 

What's hot (20)

Sep 15
Sep 15Sep 15
Sep 15
 
java review
java reviewjava review
java review
 
04 sorting
04 sorting04 sorting
04 sorting
 
Groovy DSLs (JavaOne Presentation)
Groovy DSLs (JavaOne Presentation)Groovy DSLs (JavaOne Presentation)
Groovy DSLs (JavaOne Presentation)
 
Java Day-3
Java Day-3Java Day-3
Java Day-3
 
hibernate
hibernatehibernate
hibernate
 
Oop in php lecture 2
Oop in  php lecture 2Oop in  php lecture 2
Oop in php lecture 2
 
Dojo for programmers (TXJS 2010)
Dojo for programmers (TXJS 2010)Dojo for programmers (TXJS 2010)
Dojo for programmers (TXJS 2010)
 
MVC and Entity Framework
MVC and Entity FrameworkMVC and Entity Framework
MVC and Entity Framework
 
Java basic part 2 : Datatypes Keywords Features Components Security Exceptions
Java basic part 2 : Datatypes Keywords Features Components Security Exceptions Java basic part 2 : Datatypes Keywords Features Components Security Exceptions
Java basic part 2 : Datatypes Keywords Features Components Security Exceptions
 
A Tour Through the Groovy Ecosystem
A Tour Through the Groovy EcosystemA Tour Through the Groovy Ecosystem
A Tour Through the Groovy Ecosystem
 
Vk.amberfog.com gtug part1_introduction2_javaandroid_gtug
Vk.amberfog.com gtug part1_introduction2_javaandroid_gtugVk.amberfog.com gtug part1_introduction2_javaandroid_gtug
Vk.amberfog.com gtug part1_introduction2_javaandroid_gtug
 
Core Java- An advanced review of features
Core Java- An advanced review of featuresCore Java- An advanced review of features
Core Java- An advanced review of features
 
Java 102 intro to object-oriented programming in java - exercises
Java 102   intro to object-oriented programming in java - exercisesJava 102   intro to object-oriented programming in java - exercises
Java 102 intro to object-oriented programming in java - exercises
 
Java 103
Java 103Java 103
Java 103
 
Lecture 13, 14 & 15 c# cmd let programming and scripting
Lecture 13, 14 & 15   c# cmd let programming and scriptingLecture 13, 14 & 15   c# cmd let programming and scripting
Lecture 13, 14 & 15 c# cmd let programming and scripting
 
Clojure, Plain and Simple
Clojure, Plain and SimpleClojure, Plain and Simple
Clojure, Plain and Simple
 
Advanced oops concept using asp
Advanced oops concept using aspAdvanced oops concept using asp
Advanced oops concept using asp
 
Javascript Prototype Visualized
Javascript Prototype VisualizedJavascript Prototype Visualized
Javascript Prototype Visualized
 
Java - OOPS and Java Basics
Java - OOPS and Java BasicsJava - OOPS and Java Basics
Java - OOPS and Java Basics
 

Similar to Using Library Classes to Implement Advanced Functionality

ITFT-Classes and object in java
ITFT-Classes and object in javaITFT-Classes and object in java
ITFT-Classes and object in javaAtul Sehdev
 
Object Oriented Programming Tutorial.pptx
Object Oriented Programming Tutorial.pptxObject Oriented Programming Tutorial.pptx
Object Oriented Programming Tutorial.pptxethiouniverse
 
Java 102 intro to object-oriented programming in java
Java 102   intro to object-oriented programming in javaJava 102   intro to object-oriented programming in java
Java 102 intro to object-oriented programming in javaagorolabs
 
java02.ppt
java02.pptjava02.ppt
java02.pptMENACE4
 
lecture-a-java-review.ppt
lecture-a-java-review.pptlecture-a-java-review.ppt
lecture-a-java-review.pptkavitamittal18
 
Java basics with datatypes, object oriented programming
Java basics with datatypes, object oriented programmingJava basics with datatypes, object oriented programming
Java basics with datatypes, object oriented programmingkalirajonline
 
lecture-a-java-review.ppt
lecture-a-java-review.pptlecture-a-java-review.ppt
lecture-a-java-review.pptNadiSarj2
 
lecture-a-java-review.ppt
lecture-a-java-review.pptlecture-a-java-review.ppt
lecture-a-java-review.pptJemarManatad1
 
lecture-a-java-review.ppt
lecture-a-java-review.pptlecture-a-java-review.ppt
lecture-a-java-review.pptSquidTurbo
 
UNIT - IIInew.pptx
UNIT - IIInew.pptxUNIT - IIInew.pptx
UNIT - IIInew.pptxakila m
 
java review by prof kmt.ppt
java review by prof kmt.pptjava review by prof kmt.ppt
java review by prof kmt.pptssuser1d3c311
 
JavaBasicsCore1.ppt
JavaBasicsCore1.pptJavaBasicsCore1.ppt
JavaBasicsCore1.pptbuvanabala
 
Core java Basics
Core java BasicsCore java Basics
Core java BasicsRAMU KOLLI
 
Intro to java programming
Intro to java programmingIntro to java programming
Intro to java programmingLeah Stephens
 

Similar to Using Library Classes to Implement Advanced Functionality (20)

oop 3.pptx
oop 3.pptxoop 3.pptx
oop 3.pptx
 
ITFT-Classes and object in java
ITFT-Classes and object in javaITFT-Classes and object in java
ITFT-Classes and object in java
 
Object Oriented Programming Tutorial.pptx
Object Oriented Programming Tutorial.pptxObject Oriented Programming Tutorial.pptx
Object Oriented Programming Tutorial.pptx
 
ppt_on_java.pptx
ppt_on_java.pptxppt_on_java.pptx
ppt_on_java.pptx
 
Core_java_ppt.ppt
Core_java_ppt.pptCore_java_ppt.ppt
Core_java_ppt.ppt
 
Java 102 intro to object-oriented programming in java
Java 102   intro to object-oriented programming in javaJava 102   intro to object-oriented programming in java
Java 102 intro to object-oriented programming in java
 
java02.ppt
java02.pptjava02.ppt
java02.ppt
 
lecture-a-java-review.ppt
lecture-a-java-review.pptlecture-a-java-review.ppt
lecture-a-java-review.ppt
 
lecture-a-java-review.ppt
lecture-a-java-review.pptlecture-a-java-review.ppt
lecture-a-java-review.ppt
 
lecture-a-java-review.ppt
lecture-a-java-review.pptlecture-a-java-review.ppt
lecture-a-java-review.ppt
 
Java basics with datatypes, object oriented programming
Java basics with datatypes, object oriented programmingJava basics with datatypes, object oriented programming
Java basics with datatypes, object oriented programming
 
lecture-a-java-review.ppt
lecture-a-java-review.pptlecture-a-java-review.ppt
lecture-a-java-review.ppt
 
lecture-a-java-review.ppt
lecture-a-java-review.pptlecture-a-java-review.ppt
lecture-a-java-review.ppt
 
lecture-a-java-review.ppt
lecture-a-java-review.pptlecture-a-java-review.ppt
lecture-a-java-review.ppt
 
UNIT - IIInew.pptx
UNIT - IIInew.pptxUNIT - IIInew.pptx
UNIT - IIInew.pptx
 
java review by prof kmt.ppt
java review by prof kmt.pptjava review by prof kmt.ppt
java review by prof kmt.ppt
 
C# classes objects
C#  classes objectsC#  classes objects
C# classes objects
 
JavaBasicsCore1.ppt
JavaBasicsCore1.pptJavaBasicsCore1.ppt
JavaBasicsCore1.ppt
 
Core java Basics
Core java BasicsCore java Basics
Core java Basics
 
Intro to java programming
Intro to java programmingIntro to java programming
Intro to java programming
 

More from Fajar Baskoro

Generasi Terampil Digital Skill-2023.pptx
Generasi Terampil Digital Skill-2023.pptxGenerasi Terampil Digital Skill-2023.pptx
Generasi Terampil Digital Skill-2023.pptxFajar Baskoro
 
Cara Membuat Kursus Online Wordpress-tutorstarter
Cara Membuat Kursus Online Wordpress-tutorstarterCara Membuat Kursus Online Wordpress-tutorstarter
Cara Membuat Kursus Online Wordpress-tutorstarterFajar Baskoro
 
PPT-Kick Off Double Track 2024 melaksanakan Festival Ramadhan
PPT-Kick Off Double Track 2024 melaksanakan Festival RamadhanPPT-Kick Off Double Track 2024 melaksanakan Festival Ramadhan
PPT-Kick Off Double Track 2024 melaksanakan Festival RamadhanFajar Baskoro
 
Buku Inovasi 2023 - 2024 konsep capaian KUS
Buku Inovasi 2023 - 2024 konsep capaian  KUSBuku Inovasi 2023 - 2024 konsep capaian  KUS
Buku Inovasi 2023 - 2024 konsep capaian KUSFajar Baskoro
 
Pemaparan Sosialisasi Program Dual Track 2024.pptx
Pemaparan Sosialisasi Program Dual Track 2024.pptxPemaparan Sosialisasi Program Dual Track 2024.pptx
Pemaparan Sosialisasi Program Dual Track 2024.pptxFajar Baskoro
 
Executive Millennial Entrepreneur Award 2023-1a-1.pdf
Executive Millennial Entrepreneur Award  2023-1a-1.pdfExecutive Millennial Entrepreneur Award  2023-1a-1.pdf
Executive Millennial Entrepreneur Award 2023-1a-1.pdfFajar Baskoro
 
1-Executive Millennial Entrepreneur Award 2023-1-cetak.pptx
1-Executive Millennial Entrepreneur Award  2023-1-cetak.pptx1-Executive Millennial Entrepreneur Award  2023-1-cetak.pptx
1-Executive Millennial Entrepreneur Award 2023-1-cetak.pptxFajar Baskoro
 
Executive Millennial Entrepreneur Award 2023-1.pptx
Executive Millennial Entrepreneur Award  2023-1.pptxExecutive Millennial Entrepreneur Award  2023-1.pptx
Executive Millennial Entrepreneur Award 2023-1.pptxFajar Baskoro
 
Pemrograman Mobile - JetPack Compose1.pptx
Pemrograman Mobile - JetPack Compose1.pptxPemrograman Mobile - JetPack Compose1.pptx
Pemrograman Mobile - JetPack Compose1.pptxFajar Baskoro
 
Evaluasi KPP Program Dual Track Provinsi Kaltim
Evaluasi KPP Program Dual Track Provinsi KaltimEvaluasi KPP Program Dual Track Provinsi Kaltim
Evaluasi KPP Program Dual Track Provinsi KaltimFajar Baskoro
 
foto tenda digital skill program dari sekolah
foto tenda digital skill program dari sekolahfoto tenda digital skill program dari sekolah
foto tenda digital skill program dari sekolahFajar Baskoro
 
Meraih Peluang di Gig Economy yang cocok bagi remaja
Meraih Peluang di Gig Economy yang cocok bagi remajaMeraih Peluang di Gig Economy yang cocok bagi remaja
Meraih Peluang di Gig Economy yang cocok bagi remajaFajar Baskoro
 
Membangun aplikasi mobile dengan Appsheet
Membangun aplikasi mobile dengan AppsheetMembangun aplikasi mobile dengan Appsheet
Membangun aplikasi mobile dengan AppsheetFajar Baskoro
 
Transition education to employment.pdf
Transition education to employment.pdfTransition education to employment.pdf
Transition education to employment.pdfFajar Baskoro
 

More from Fajar Baskoro (20)

Generasi Terampil Digital Skill-2023.pptx
Generasi Terampil Digital Skill-2023.pptxGenerasi Terampil Digital Skill-2023.pptx
Generasi Terampil Digital Skill-2023.pptx
 
Cara Membuat Kursus Online Wordpress-tutorstarter
Cara Membuat Kursus Online Wordpress-tutorstarterCara Membuat Kursus Online Wordpress-tutorstarter
Cara Membuat Kursus Online Wordpress-tutorstarter
 
PPT-Kick Off Double Track 2024 melaksanakan Festival Ramadhan
PPT-Kick Off Double Track 2024 melaksanakan Festival RamadhanPPT-Kick Off Double Track 2024 melaksanakan Festival Ramadhan
PPT-Kick Off Double Track 2024 melaksanakan Festival Ramadhan
 
Buku Inovasi 2023 - 2024 konsep capaian KUS
Buku Inovasi 2023 - 2024 konsep capaian  KUSBuku Inovasi 2023 - 2024 konsep capaian  KUS
Buku Inovasi 2023 - 2024 konsep capaian KUS
 
Pemaparan Sosialisasi Program Dual Track 2024.pptx
Pemaparan Sosialisasi Program Dual Track 2024.pptxPemaparan Sosialisasi Program Dual Track 2024.pptx
Pemaparan Sosialisasi Program Dual Track 2024.pptx
 
Executive Millennial Entrepreneur Award 2023-1a-1.pdf
Executive Millennial Entrepreneur Award  2023-1a-1.pdfExecutive Millennial Entrepreneur Award  2023-1a-1.pdf
Executive Millennial Entrepreneur Award 2023-1a-1.pdf
 
1-Executive Millennial Entrepreneur Award 2023-1-cetak.pptx
1-Executive Millennial Entrepreneur Award  2023-1-cetak.pptx1-Executive Millennial Entrepreneur Award  2023-1-cetak.pptx
1-Executive Millennial Entrepreneur Award 2023-1-cetak.pptx
 
Executive Millennial Entrepreneur Award 2023-1.pptx
Executive Millennial Entrepreneur Award  2023-1.pptxExecutive Millennial Entrepreneur Award  2023-1.pptx
Executive Millennial Entrepreneur Award 2023-1.pptx
 
Pemrograman Mobile - JetPack Compose1.pptx
Pemrograman Mobile - JetPack Compose1.pptxPemrograman Mobile - JetPack Compose1.pptx
Pemrograman Mobile - JetPack Compose1.pptx
 
Evaluasi KPP Program Dual Track Provinsi Kaltim
Evaluasi KPP Program Dual Track Provinsi KaltimEvaluasi KPP Program Dual Track Provinsi Kaltim
Evaluasi KPP Program Dual Track Provinsi Kaltim
 
foto tenda digital skill program dari sekolah
foto tenda digital skill program dari sekolahfoto tenda digital skill program dari sekolah
foto tenda digital skill program dari sekolah
 
Meraih Peluang di Gig Economy yang cocok bagi remaja
Meraih Peluang di Gig Economy yang cocok bagi remajaMeraih Peluang di Gig Economy yang cocok bagi remaja
Meraih Peluang di Gig Economy yang cocok bagi remaja
 
Membangun aplikasi mobile dengan Appsheet
Membangun aplikasi mobile dengan AppsheetMembangun aplikasi mobile dengan Appsheet
Membangun aplikasi mobile dengan Appsheet
 
epl1.pdf
epl1.pdfepl1.pdf
epl1.pdf
 
user.docx
user.docxuser.docx
user.docx
 
Dtmart.pptx
Dtmart.pptxDtmart.pptx
Dtmart.pptx
 
DualTrack-2023.pptx
DualTrack-2023.pptxDualTrack-2023.pptx
DualTrack-2023.pptx
 
BADGE.pptx
BADGE.pptxBADGE.pptx
BADGE.pptx
 
womenatwork.pdf
womenatwork.pdfwomenatwork.pdf
womenatwork.pdf
 
Transition education to employment.pdf
Transition education to employment.pdfTransition education to employment.pdf
Transition education to employment.pdf
 

Recently uploaded

Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...stazi3110
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...MyIntelliSource, Inc.
 
EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityNeo4j
 
DNT_Corporate presentation know about us
DNT_Corporate presentation know about usDNT_Corporate presentation know about us
DNT_Corporate presentation know about usDynamic Netsoft
 
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...soniya singh
 
What is Binary Language? Computer Number Systems
What is Binary Language?  Computer Number SystemsWhat is Binary Language?  Computer Number Systems
What is Binary Language? Computer Number SystemsJheuzeDellosa
 
Unit 1.1 Excite Part 1, class 9, cbse...
Unit 1.1 Excite Part 1, class 9, cbse...Unit 1.1 Excite Part 1, class 9, cbse...
Unit 1.1 Excite Part 1, class 9, cbse...aditisharan08
 
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝soniya singh
 
The Evolution of Karaoke From Analog to App.pdf
The Evolution of Karaoke From Analog to App.pdfThe Evolution of Karaoke From Analog to App.pdf
The Evolution of Karaoke From Analog to App.pdfPower Karaoke
 
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEBATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEOrtus Solutions, Corp
 
Engage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The UglyEngage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The UglyFrank van der Linden
 
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideChristina Lin
 
Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)OPEN KNOWLEDGE GmbH
 
chapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptchapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptkotipi9215
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVshikhaohhpro
 
Project Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanationProject Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanationkaushalgiri8080
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdfWave PLM
 
why an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfwhy an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfjoe51371421
 
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxKnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxTier1 app
 
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdfThe Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdfkalichargn70th171
 

Recently uploaded (20)

Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
 
EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered Sustainability
 
DNT_Corporate presentation know about us
DNT_Corporate presentation know about usDNT_Corporate presentation know about us
DNT_Corporate presentation know about us
 
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
 
What is Binary Language? Computer Number Systems
What is Binary Language?  Computer Number SystemsWhat is Binary Language?  Computer Number Systems
What is Binary Language? Computer Number Systems
 
Unit 1.1 Excite Part 1, class 9, cbse...
Unit 1.1 Excite Part 1, class 9, cbse...Unit 1.1 Excite Part 1, class 9, cbse...
Unit 1.1 Excite Part 1, class 9, cbse...
 
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝
 
The Evolution of Karaoke From Analog to App.pdf
The Evolution of Karaoke From Analog to App.pdfThe Evolution of Karaoke From Analog to App.pdf
The Evolution of Karaoke From Analog to App.pdf
 
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEBATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
 
Engage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The UglyEngage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The Ugly
 
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
 
Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)
 
chapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptchapter--4-software-project-planning.ppt
chapter--4-software-project-planning.ppt
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTV
 
Project Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanationProject Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanation
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf
 
why an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfwhy an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdf
 
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxKnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
 
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdfThe Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
 

Using Library Classes to Implement Advanced Functionality

  • 1. More sophisticated behavior Using library classes to implement some more advanced functionality 5.0
  • 2. 2 Main concepts to be covered • Using library classes • Reading documentation Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
  • 3. 3 The Java class library • Thousands of classes. • Tens of thousands of methods. • Many useful classes that make life much easier. • Library classes are often inter- related. • Arranged into packages. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
  • 4. 4 Working with the library • A competent Java programmer must be able to work with the libraries. • 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. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
  • 5. 5 A Technical Support System • A textual, interactive dialog system • Idea based on ‘Eliza’ by Joseph Weizenbaum (MIT, 1960s) • Explore tech-support-complete … Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
  • 6. 6 Main loop structure boolean finished = false; while(!finished) { do something if(exit condition) { finished = true; } else { do something more } } Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling A common iteration pattern.
  • 7. 7 Main loop body String input = reader.getInput(); ... String response = responder.generateResponse(); System.out.println(response); Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
  • 8. 8 The exit condition String input = reader.getInput(); if(input.startsWith("bye")) { finished = true; } • Where does ‘startsWith’ come from? • What is it? What does it do? • How can we find out? Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
  • 9. 9 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 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
  • 10. 10Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
  • 11. 11 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 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
  • 12. 12 Interface vs implementation The documentation does not include • private fields (most fields are private) • private methods • the bodies (source code) of methods the implementation of the class Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
  • 13. 13 Documentation for startsWith • startsWith – public boolean startsWith(String prefix) • Tests if this string starts with the specified prefix. • Parameters: – prefix - the prefix. • Returns: – true if the …; false otherwise Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
  • 14. 14 Methods from String • contains • endsWith • indexOf • substring • toUpperCase • trim • Beware: strings are immutable! Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
  • 15. 15 Using library classes • Classes organized into packages. • Classes from the library must be imported using an import statement (except classes from the java.lang package). • They can then be used like classes from the current project. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
  • 16. 16 Packages and import • Single classes may be imported: import java.util.ArrayList; • Whole packages can be imported: import java.util.*; • Importation does not involve source code insertion. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
  • 17. 17 Using Random • The library class Random can be used to generate random numbers Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling import java.util.Random; ... Random rand = new Random(); ... int num = rand.nextInt(); int value = 1 + rand.nextInt(100); int index = rand.nextInt(list.size());
  • 18. 18 Selecting random responses Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling public Responder() { randomGenerator = new Random(); responses = new ArrayList<String>(); fillResponses(); } public void fillResponses() { fill responses with a selection of response strings } public String generateResponse() { int index = randomGenerator.nextInt(responses.size()); return responses.get(index); }
  • 19. 19 Parameterized classes • The documentation includes provision for a type parameter: – ArrayList<E> • These type names reappear in the parameters and return types: – E get(int index) – boolean add(E e) Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
  • 20. 20 Parameterized classes • The types in the documentation are placeholders for the types we use in practice: – An ArrayList<TicketMachine> actually has methods: – TicketMachine get(int index) – boolean add(TicketMachine e) Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
  • 21. 21 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 (its interface). • Some classes are parameterized with additional types. • Parameterized classes are also known as generic classes or generic types. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
  • 22. More sophisticated behavior Using library classes to implement some more advanced functionality
  • 23. 23 Main concepts to be covered • Further library classes • Set • Map • Writing documentation • javadoc Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
  • 24. 24 Using sets import java.util.HashSet; ... HashSet<String> mySet = new HashSet<String>(); mySet.add("one"); mySet.add("two"); mySet.add("three"); for(String element : mySet) { do something with element } Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Compare with code for an ArrayList!
  • 25. 25 Tokenising Strings public HashSet<String> getInput() { System.out.print("> "); String inputLine = reader.nextLine().trim().toLowerCase(); String[] wordArray = inputLine.split(" "); HashSet<String> words = new HashSet<String>(); for(String word : wordArray) { words.add(word); } return words; } Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
  • 26. 26 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. • Example: a telephone book. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
  • 27. 27 Using maps • A map with strings as keys and values Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling "Charles Nguyen" :HashMap "(531) 9392 4587" "Lisa Jones" "(402) 4536 4674" "William H. Smith" "(998) 5488 0123"
  • 28. 28 Using maps HashMap <String, String> phoneBook = new HashMap<String, String>(); phoneBook.put("Charles Nguyen", "(531) 9392 4587"); phoneBook.put("Lisa Jones", "(402) 4536 4674"); phoneBook.put("William H. Smith", "(998) 5488 0123"); String phoneNumber = phoneBook.get("Lisa Jones"); System.out.println(phoneNumber); Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
  • 29. 29 List, Map and Set • Alternative ways to group objects. • Varying implementations available: – ArrayList, LinkedList – HashSet, TreeSet • But HashMap is unrelated to HashSet, despite similar names. • The second word reveals organizational relatedness. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
  • 30. 30 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 potential 'library class'! Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
  • 31. 31 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 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
  • 32. 32 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 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
  • 33. 33 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 (2011.07.31) */ Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
  • 34. 34 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<String> getInput(String prompt) { ... } Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
  • 35. 35 Public vs private • Public elements are accessible to objects of other classes: • Fields, constructors and methods • Fields should not be public. • Private elements are accessible only to objects of the same class. • Only methods that are intended for other classes should be public. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
  • 36. 36 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. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
  • 37. 37 Code completion • The BlueJ editor supports lookup of methods. • Use Ctrl-space after a method-call dot to bring up a list of available methods. • Use Return to select a highlighted method. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
  • 38. 38 Code completion in BlueJ Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
  • 39. 39 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). Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
  • 40. Class and constant variables Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
  • 41. 41 Class variables • A class variable is shared between all instances of the class. • In fact, it belongs to the class and exists independent of any instances. • Designated by the static keyword. • Public static variables are accessed via the class name; e.g.: – Thermometer.boilingPoint Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
  • 42. 42 Class variables Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
  • 43. 43 Constants • A variable, once set, can have its value fixed. • Designated by the final keyword. – final int max = list.size(); • Final fields must be set in their declaration or the constructor. • Combing static and final is common. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
  • 44. 44 Class constants • static: class variable • final: constant private static final int gravity = 3; • Public visibility is less of an issue with final fields. • Upper-case names often used for class constants: public static final int BOILING_POINT = 100; Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling