Sie sind auf Seite 1von 13

Fundamentals of Software

Development
CT010-3-1
Packages

Prepared by: GTK First Prepared on:26 th July 2005 Last Modified on:15th December 2005
Quality checked by: GTK
Copyright 2005 Asia Pacific University College of Technology and Innovation

Packages

Introduction to Packages
API packages
Using import
Sample programs

CT010-3-1 Fundamentals of Software Development

Packages

Slide 2 (of 14)

Learning Outcomes
At the end of this module, YOU should be
able to:
Use and understand the Java API
packages.
Write Java programs using packages.

CT010-3-1 Fundamentals of Software Development

Packages

Slide 3 (of 14)

Packages
Definition:
A package is used to group similar and
interdependent classes together under a
single name.
Syntax:
package package_name
class class_name {
..
}

CT010-3-1 Fundamentals of Software Development

Packages

Slide 4 (of 14)

Packages
Java API packages
java.lang
- contains essential Java classes, including
numerics, strings, objects, compiler, runtime,
security, and threads.
- only package that is automatically imported
into every Java program.
java.awt
- provides an integrated set of classes to manage user
interface components such as windows, dialog boxes,
buttons, checkboxes, lists, menus, scrollbars, and text
fields. (AWT = Abstract Window Toolkit)
CT010-3-1 Fundamentals of Software Development

Packages

Slide 5 (of 14)

java.lang package
Math class
defined in the java.lang package
consists of static methods and variables used for
mathematical values and functions
eg.
Methods and arguments
abs(double value)
min(double x, double y)
max (double x, double y)
pow(double x, double y)
random()
sqrt(double value)
CT010-3-1 Fundamentals of Software Development

Packages

Slide 6 (of 14)

java.lang package
String class
defined in the java.lang package
contains methods that allows strings to be examine,
tested and manipulated
eg.
Methods and arguments
char
charAt(int index)
int
compareTo(String s)
String
concat(String s)
boolean
equals(Object o)
int
indexOf(String s)
Strings
toLowerCase()
CT010-3-1 Fundamentals of Software Development

Packages

Slide 8 (of 14)

Creating Packages
creating packages encourages reuse of software
as it makes it convenient to import many related
classes at once
provides users with compiled files .class
this prevents copying of programs and changes
cannot be made
included at the beginning of a class file
eg. package com.course.animals
indicates the compiled file should be placed in a
folder named com\course\animals
CT010-3-1 Fundamentals of Software Development

Packages

Slide 9 (of 14)

Creating Packages
To place the HelloWorld.java file below into a
package called world :
// only comment can be here
package world;
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World");
}
}

1. Specify the keyword package and name of package


2. The program above will have to be placed in the
world directory ie. C:\World
3. Set up a classpath set CLASSPATH=.;C:\;
CT010-3-1 Fundamentals of Software Development

Packages

Slide 10 (of 14)

Using Packages
Syntax:
class name in the current file

import package.class;

OR
all classes in the package is
visible

import package.*;

eg. import java.io.IOException;


import java.awt.*;

CT010-3-1 Fundamentals of Software Development

Packages

Slide 11 (of 14)

Adding Classes to Packages


To add the class HelloFriends into a
package called world add the following
statement into HelloFriends.java file:
package world;
Move the source file HelloFriends.java to
the source directory for package world
Compile the file

CT010-3-1 Fundamentals of Software Development

Packages

Slide 12 (of 14)

Summary of Main Teaching Points

Introduction to Packages
API packages
Using import
Sample programs

CT010-3-1 Fundamentals of Software Development

Packages

Slide 13 (of 14)

Next Lesson
Java API and Class Libraries

Introduction to Applets
Difference between an applet and an application
Drawing methods
Executing applets

The Abstract Window Toolkit


Creating and Using GUI Components

TextField
TextArea
Button
Choice
List
Checkbox
CheckBoxGroup
Label

CT010-3-1 Fundamentals of Software Development

Packages

Slide 14 (of 14)