14 packages

371 views
309 views

Published on

Hariprasanna V (9843824677)

0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
371
On SlideShare
0
From Embeds
0
Number of Embeds
9
Actions
Shares
0
Downloads
5
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

14 packages

  1. 1. Packages Tonga Institute of Higher Education
  2. 2. Pre-Built Objects and Methods <ul><ul><li>Java has many pre-built objects for us to use. </li></ul></ul><ul><ul><li>Doorknob analogy </li></ul></ul><ul><ul><ul><li>We want to add a doorknob to a door. </li></ul></ul></ul><ul><ul><ul><li>We could make our own. </li></ul></ul></ul><ul><ul><ul><li>But it’s easier to use a pre-built doorknob. </li></ul></ul></ul><ul><ul><ul><li>We still need to do work to add the doorknob to our door. </li></ul></ul></ul><ul><ul><li>You can find a list of pre-built objects in the Java Developer’s Kit documentation. </li></ul></ul>System.out.println(&quot;We Miss Dave!&quot;); Method Name Parameter Package
  3. 3. Demonstration Java API Documentation
  4. 4. Organizing Classes <ul><li>Java comes with over 2,700 different classes for doing different things. </li></ul><ul><li>Sun needed a way to organize the classes into groups, they decided to use packages. </li></ul><ul><li>A package is a group of related classes. </li></ul>
  5. 5. Packages <ul><li>Java contains 140 different packages. </li></ul><ul><li>These packages are arranged hierarchically (packages inside of packages). </li></ul>java awt event geom image lang ref reflect acl security spec
  6. 6. Package Names <ul><li>The name of the package is actually a list of names, separated by periods. </li></ul>java.awt.image; java awt event geom image lang ref reflect acl security spec
  7. 7. Using Packages <ul><li>If you want to use a built-in Java class, first look it up in the Java Application Interface. (API) </li></ul><ul><li>You can find the Java API documentation on the course website page. </li></ul><ul><ul><li>http://www.tihe.org/courses/it151 </li></ul></ul>This is the API entry for the Graphics class. The package for this class is java.awt .
  8. 8. Using Packages (cont.) <ul><li>Once you know the package for a class, import it into your program. </li></ul><ul><li>To import all classes in a package, use the wild card (*). </li></ul>import java.awt.Graphics; import java.awt.*;
  9. 9. Case Study: Problem <ul><li>Michael wants to use the Socket class in his program for Networking. </li></ul><ul><li>Which package does he need to include in his program? </li></ul><ul><li>What is the line of code he needs to write? </li></ul>
  10. 10. Case Study: Answer <ul><li>Look up the Socket class in the Java API </li></ul><ul><li>Use the import command to include the package: </li></ul><ul><li>or </li></ul>import java.net.Socket; import java.net.*;
  11. 11. Defining a Package <ul><li>If you want to make your own package to be used in other programs, define it. </li></ul><ul><li>Defining packages can lead to problems compiling your program. </li></ul><ul><li>If you have multiple files in your project, make sure they are all defined in the same package. </li></ul>package myprojects.helloworld;

×