Java Packages and Libraries M Taimoor Khan [email protected].
-
Upload
june-stephens -
Category
Documents
-
view
216 -
download
3
Transcript of Java Packages and Libraries M Taimoor Khan [email protected].
![Page 2: Java Packages and Libraries M Taimoor Khan taimoorkhan@ciit-attock.edu.pk.](https://reader035.fdocuments.us/reader035/viewer/2022070409/56649e915503460f94b96918/html5/thumbnails/2.jpg)
Content
• What is a class library?• What is a package?• Java class libraries, what do they
provide?• The root class : java.lang.object
2
![Page 3: Java Packages and Libraries M Taimoor Khan taimoorkhan@ciit-attock.edu.pk.](https://reader035.fdocuments.us/reader035/viewer/2022070409/56649e915503460f94b96918/html5/thumbnails/3.jpg)
The Java class library• Thousands of classes• Tens of thousands of methods• Many useful classes that make life much easier• A Java programmer must be able to work with the
librarieso http://java.sun.com/javase/reference/api.jsp
3
![Page 4: Java Packages and Libraries M Taimoor Khan taimoorkhan@ciit-attock.edu.pk.](https://reader035.fdocuments.us/reader035/viewer/2022070409/56649e915503460f94b96918/html5/thumbnails/4.jpg)
Working with the library
You should:• know some important classes by name• know how to find out about other classes
o http://java.sun.com/javase/6/docs/api/
Remember:• We only need to know the interface, not the
implementation
4
The public face of the class
![Page 5: Java Packages and Libraries M Taimoor Khan taimoorkhan@ciit-attock.edu.pk.](https://reader035.fdocuments.us/reader035/viewer/2022070409/56649e915503460f94b96918/html5/thumbnails/5.jpg)
The exit conditionString 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?
5
![Page 6: Java Packages and Libraries M Taimoor Khan taimoorkhan@ciit-attock.edu.pk.](https://reader035.fdocuments.us/reader035/viewer/2022070409/56649e915503460f94b96918/html5/thumbnails/6.jpg)
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
6
![Page 7: Java Packages and Libraries M Taimoor Khan taimoorkhan@ciit-attock.edu.pk.](https://reader035.fdocuments.us/reader035/viewer/2022070409/56649e915503460f94b96918/html5/thumbnails/7.jpg)
Interface vs implementation
The documentation includes• the name of the class;
7
![Page 8: Java Packages and Libraries M Taimoor Khan taimoorkhan@ciit-attock.edu.pk.](https://reader035.fdocuments.us/reader035/viewer/2022070409/56649e915503460f94b96918/html5/thumbnails/8.jpg)
Interface vs implementation
The documentation includes• a general description of the class;
8
![Page 9: Java Packages and Libraries M Taimoor Khan taimoorkhan@ciit-attock.edu.pk.](https://reader035.fdocuments.us/reader035/viewer/2022070409/56649e915503460f94b96918/html5/thumbnails/9.jpg)
Interface vs implementation
The documentation includes• a list of constructors
9
![Page 10: Java Packages and Libraries M Taimoor Khan taimoorkhan@ciit-attock.edu.pk.](https://reader035.fdocuments.us/reader035/viewer/2022070409/56649e915503460f94b96918/html5/thumbnails/10.jpg)
Interface vs implementation
The documentation includes• a list of methods
10
![Page 11: Java Packages and Libraries M Taimoor Khan taimoorkhan@ciit-attock.edu.pk.](https://reader035.fdocuments.us/reader035/viewer/2022070409/56649e915503460f94b96918/html5/thumbnails/11.jpg)
Interface vs implementation
The documentation includes• return values and parameters for constructors
and methods
11
![Page 12: Java Packages and Libraries M Taimoor Khan taimoorkhan@ciit-attock.edu.pk.](https://reader035.fdocuments.us/reader035/viewer/2022070409/56649e915503460f94b96918/html5/thumbnails/12.jpg)
Interface vs implementation
The documentation does not include
• private fields (most fields are private)• private methods• the implementation (source code) for each
method
12
![Page 13: Java Packages and Libraries M Taimoor Khan taimoorkhan@ciit-attock.edu.pk.](https://reader035.fdocuments.us/reader035/viewer/2022070409/56649e915503460f94b96918/html5/thumbnails/13.jpg)
Using library classes• Classes from the library must be imported using
an import statement (except classes from java.lang).
• They can then be used like classes from the current project.
• http://java.sun.com/docs/books/tutorial/java/package/usepkgs.html
13
“The Java compiler automatically imports” •java.lang package•the package of the current file source code
![Page 14: Java Packages and Libraries M Taimoor Khan taimoorkhan@ciit-attock.edu.pk.](https://reader035.fdocuments.us/reader035/viewer/2022070409/56649e915503460f94b96918/html5/thumbnails/14.jpg)
Packages and import• Classes are organised in packages.
• Single classes may be imported:
import java.util.ArrayList;
• Whole packages can be imported:
import java.util.*;
14
Some of the Packages listed in the API
Packages are NOT hierarchicalUsing “import java.awt.*;” does not import java.awt.color.*;
![Page 15: Java Packages and Libraries M Taimoor Khan taimoorkhan@ciit-attock.edu.pk.](https://reader035.fdocuments.us/reader035/viewer/2022070409/56649e915503460f94b96918/html5/thumbnails/15.jpg)
Using Random• The library class Random can be used to generate random numbers
15
import java.util.Random;...Random randomGenerator = new Random();...int index1 = randomGenerator.nextInt();int index2 = randomGenerator.nextInt(100);
![Page 16: Java Packages and Libraries M Taimoor Khan taimoorkhan@ciit-attock.edu.pk.](https://reader035.fdocuments.us/reader035/viewer/2022070409/56649e915503460f94b96918/html5/thumbnails/16.jpg)
Generating random responses
16
public Responder(){ randomGenerator = new Random(); responses = new ArrayList<String>(); fillResponses();}
public String generateResponse(){ int index = randomGenerator.nextInt(responses.size()); return responses.get(index);}
public void fillResponses(){ ...}
![Page 17: Java Packages and Libraries M Taimoor Khan taimoorkhan@ciit-attock.edu.pk.](https://reader035.fdocuments.us/reader035/viewer/2022070409/56649e915503460f94b96918/html5/thumbnails/17.jpg)
Java Packages• An overview can be found at this link• http://java.sun.com/javase/6/docs/api/overview-summary.ht
ml
17
![Page 18: Java Packages and Libraries M Taimoor Khan taimoorkhan@ciit-attock.edu.pk.](https://reader035.fdocuments.us/reader035/viewer/2022070409/56649e915503460f94b96918/html5/thumbnails/18.jpg)
Java Packages• Inside each package you will find
o InterfacesoClasseso Enumerationso Exceptions
18
![Page 19: Java Packages and Libraries M Taimoor Khan taimoorkhan@ciit-attock.edu.pk.](https://reader035.fdocuments.us/reader035/viewer/2022070409/56649e915503460f94b96918/html5/thumbnails/19.jpg)
java.lang• “Provides classes that are fundamental to the design of the
Java programming language.”• Boolean, Byte, Double, Integer, Long, String • Math • System • Object
19
![Page 20: Java Packages and Libraries M Taimoor Khan taimoorkhan@ciit-attock.edu.pk.](https://reader035.fdocuments.us/reader035/viewer/2022070409/56649e915503460f94b96918/html5/thumbnails/20.jpg)
java.util• “Contains the collections framework…date and
time facilities,… string tokenizer.. random-number generator..”
• ArrayList<E>, LinkedList<E>, Stack<E> • Calendar, Date • Formatter• Scanner• StringTokenizer• Random • Iterator<E>
20
![Page 21: Java Packages and Libraries M Taimoor Khan taimoorkhan@ciit-attock.edu.pk.](https://reader035.fdocuments.us/reader035/viewer/2022070409/56649e915503460f94b96918/html5/thumbnails/21.jpg)
java.io• “Provides for system input and output through
data streams, serialization and the file system.”• Console • File • FileReader • BufferedInputStream • StreamTokenizer
21
![Page 22: Java Packages and Libraries M Taimoor Khan taimoorkhan@ciit-attock.edu.pk.](https://reader035.fdocuments.us/reader035/viewer/2022070409/56649e915503460f94b96918/html5/thumbnails/22.jpg)
GUI libraries• java.awt
o java.awt.Event
• javax.imageio • javax.swing
o http://java.sun.com/docs/books/tutorial/uiswing/
22
![Page 23: Java Packages and Libraries M Taimoor Khan taimoorkhan@ciit-attock.edu.pk.](https://reader035.fdocuments.us/reader035/viewer/2022070409/56649e915503460f94b96918/html5/thumbnails/23.jpg)
All classes are Objects
23
![Page 24: Java Packages and Libraries M Taimoor Khan taimoorkhan@ciit-attock.edu.pk.](https://reader035.fdocuments.us/reader035/viewer/2022070409/56649e915503460f94b96918/html5/thumbnails/24.jpg)
Packages
24
![Page 25: Java Packages and Libraries M Taimoor Khan taimoorkhan@ciit-attock.edu.pk.](https://reader035.fdocuments.us/reader035/viewer/2022070409/56649e915503460f94b96918/html5/thumbnails/25.jpg)
25
![Page 26: Java Packages and Libraries M Taimoor Khan taimoorkhan@ciit-attock.edu.pk.](https://reader035.fdocuments.us/reader035/viewer/2022070409/56649e915503460f94b96918/html5/thumbnails/26.jpg)
Reading & Exercises• P136 – p147• Collections
o http://java.sun.com/docs/books/tutorial/collections/index.html
• Swingo http://java.sun.com/docs/books/tutorial/ui/index.html
• Classes and Objectso http://java.sun.com/docs/books/tutorial/java/javaOO/inde
x.html
• Genericso http://java.sun.com/docs/books/tutorial/java/generics/ind
ex.htm
26
![Page 27: Java Packages and Libraries M Taimoor Khan taimoorkhan@ciit-attock.edu.pk.](https://reader035.fdocuments.us/reader035/viewer/2022070409/56649e915503460f94b96918/html5/thumbnails/27.jpg)
References• http://java.sun.com/javase/reference/api.jsp• http://java.sun.com/docs/books/tutorial/java/package/mana
gingfiles.html• http://en.wikipedia.org/wiki/Classpath_(Java)• http://condor.depaul.edu/~glancast/224class/docs/lecJun16.
html
27