Packages - Object Oriented Programming I - Lecture Slides | CMSC 131, Study Guides, Projects, Research of Computer Science

Material Type: Project; Professor: Dorr; Class: OBJECT-ORIENTED PROG I; Subject: Computer Science; University: University of Maryland; Term: Spring 2007;

Typology: Study Guides, Projects, Research

Pre 2010

Uploaded on 07/30/2009

koofers-user-29u
koofers-user-29u 🇺🇸

10 documents

1 / 17

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CMSC 131 Spring 2007
Bonnie Dorr (adapted from Rance Cleaveland)
Lecture 29: Packages
Last time:
1. 2-dimensional arrays
Today:
1. Packages
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff

Partial preview of the text

Download Packages - Object Oriented Programming I - Lecture Slides | CMSC 131 and more Study Guides, Projects, Research Computer Science in PDF only on Docsity!

CMSC 131 Spring 2007 Bonnie Dorr (adapted from Rance Cleaveland)

Lecture 29: Packages^ Last time:^ 1.^

2-dimensional arrays

Today: 1.^

Packages

CMSC 131 Spring 2007 Bonnie Dorr (adapted by Rance Cleaveland)

Project #6 Assigned! z^ Project due Monday, 4/16 at 11 pm z^ Project is closed^ z^

You must complete the project by yourself z Assistance can only be provided by teaching assistants(TAs) and instructors z You must not look at other students' code

z^ Start now!^ z^

Read entire assignment from beginning to end beforestarting to code z Check out assignment now from CVS z Follow the instructions

exactly

, as much of grading is

automated

CMSC 131 Spring 2007 Bonnie Dorr (adapted by Rance Cleaveland)

Packages z^ Package

: a collection of

related

classes and/or interfaces.

z^ Examples

: The Java API

javax.swing

: classes dealing with the development of GUIs.

java.lang

: essential classes required by the Java language.

java.text

: facilities for formatting text output.

java.util

: classes for storing/accessing collections of objects.

java.net

: for network communication.

z^ Hierarchical

: Packages can be

divided

into subpackages.

java.awt

: classes for basic GUI elements and graphics.

java.awt.font
: classes and interface relating to fonts.
java.awt.geom
: classes for defining 2-dimensional objects.

There is no limit to the nesting depth.

CMSC 131 Spring 2007 Bonnie Dorr (adapted by Rance Cleaveland)

Access to Package Members z^ Review of Package Basics

Accessing Package Members

Fully qualified name

: E.g.,

javax.swing.JOptionPane

Importing a single class

:

import javax.swing.JOptionPane;…JOptionPane.showMessageDialog( … );Importing all the classes

:

import javax.swing.*;…JOptionPane.showMessageDialog( … ); Import semantics

: import does not “

insert

” the Java files (as C/C++ do

with “include” files). Instead, it tells the compiler

where to look

to find

classes that the program refers to. Multiple import statements

: You can have as many as you like. They go

at the top of your .java

file (before any classes or interfaces).

java.lang

: is^ automatically imported

into every program.

CMSC 131 Spring 2007 Bonnie Dorr (adapted by Rance Cleaveland)

Defining your own package z Defining a package

: Add a “

package

” statement to specify the package

containing the classes of this file.^ package mypackage;…public class myClass { … }

// myClass is part of mypackage

z^

This must be the

first statement

of your file. (Other than comments.)

z^ Subpackages

: Packages organized into subpackages. This is specified using the notation “

main.subpackage

”. Example:

package mypackage.mysubpackage;…public class myClass2 { … }

// myClass2 is part of mysubpackage// … which is within mypackage

z^ Packages in Eclipse

:^ File→

New→

Package

. Enter the full name of the

package (e.g. “

mypackage

” or “mypackage.mysubpackage

z^ Without Eclipse

: Just insert this yourself (“

package mypackage

CMSC 131 Spring 2007 Bonnie Dorr (adapted by Rance Cleaveland)

Class Access and Packages z^ Class access within a package

z^ Classes within a package can refer to each other

without full

qualification

z^ If a class is

not^ declared

public

, it can

only be accessed by other

classes

within the package

z^ Class access across packages

z^ A^

public class

can be accessed from

other packages

z^ When this is done, either its name is

fully qualified

or is^

imported

z^ We can view

public classes of a package

as the “

interface

” of the

package with the outside world. (This is analogous to public methods ofa class forming its interface.)

z^ Subpackages are not automatically imported

z^ When you import a package (e.g.

import java.awt.*

) it^ does not

import

the subpackages (e.g.

java.awt.font

must be

explicitly

imported).

CMSC 131 Spring 2007 Bonnie Dorr (adapted by Rance Cleaveland)

Example z To illustrate these points, let’s consider an example of a simple hierarchicalpackage, which we will create.

Files:Driver.java

Driver

Files:Circle.javaRectangle.javaOtherShape.java Files:PublicClass1.javaPublicClass2.java

Circle Rectangle OtherShape^ PublicClass1^ NonPublicClass1^ PublicClass

graphics Packages:^ graphics.shapes^ graphics.otherstuff

Files:

Classes:

CMSC 131 Spring 2007 Bonnie Dorr (adapted by Rance Cleaveland)

Example: graphics.shapespackagepackage graphics.shapes;public class Circle {private double radius;public String toString( ) { return "I'm a circle"; }} package graphics.shapes;public class Rectangle {private double height, width;public String toString( ) { return "I'm a rectangle"; }} package graphics.shapes;public class OtherShape {private Circle c;private Rectangle r;}

File: Circle.java File: Rectangle.java File: OtherShape.java

Note: Classes of this package canbe accessed without the need forimport or using graphics.shapes.Circle

CMSC 131 Spring 2007 Bonnie Dorr (adapted by Rance Cleaveland)

Example: graphics.packagepackage graphics;import graphics.shapes.Circle;public class Driver {public static void main( String[ ] args ) {

testShapes( );testOtherStuff( );} public static void testShapes( ) {Circle c = new Circle( );System.out.println( c.toString( ) );Rectangle r = new Rectangle( );} public static void testOtherStuff( ) {PublicClass1 x = new PublicClass1( );graphics.otherstuff.PublicClass1 y = new graphics.otherstuff.PublicClass1( );System.out.println( y );graphics.otherstuff.NonPublicClass1 z;} }

File: Driver.java

Note: importing Circle (but nothing else)

Okay: Circle is accessible. Compiler error: Cannot access Rectanglewithout import or qualified name.^ Compiler error: Cannot access PublicClass1without import or qualified name.^ Compiler error: Non-public classes arenever accessible outside the class.

CMSC 131 Spring 2007 Bonnie Dorr (adapted by Rance Cleaveland)

File Structure z^ Java organizes the package files using your system’s directory structure. graphics:Driver.class

Driver.java otherstuff/shapes/graphics/otherstuff:NonPublicClass1.classPublicClass1.java

PublicClass2.java

PublicClass1.class

PublicClass2.class

graphics/shapes:Circle.class

OtherShape.class

Rectangle.class

Circle.java

OtherShape.java

Rectangle.java

When you create newpackages in Eclipse, thisis done automatically.^ Note that nonpublic classesgenerate their own .class file,even though there is no .javafile.

CMSC 131 Spring 2007 Bonnie Dorr (adapted by Rance Cleaveland)

Packages and Classpath z^ You may have many different packages on your system (e.g. theJava runtime library, cmsc131PictureLib, cmsc131PuzzleLib) inmany different locations.

How does Java know where to look

for

them? z ClassPath

: is a system

environment variable

that gives a list of

directories and jar files where Java should look up its classes. z Windows Example

: Suppose we want to use

z^ graphics package

: stored in directory C:\MyJavaPackages\graphics.

z^ cmsc131PictureLib.jar

: stored in C:\MyJars\cmsc131PictureLib.jar

z^ classes compiled in the

current working directory

: The current

directory is denoted by “.” (period) on most systems. z^ C:>set CLASSPATH=.;C:\MyJavaPackages;C:\MyJars\cmsc131PictureLib.jar

The list is separated by semicolons “;”

Always include “.”

jar files must belisted explicitly

CMSC 131 Spring 2007 Bonnie Dorr (adapted by Rance Cleaveland)

Packages and Classpath z^ In Eclipse

: The ClassPath is already set with important default directories (e.g. the Java runtime library). Tomodify the ClassPath: z^ In the

Package Explorer

, right click on the project name

z^ Select “

Properties

→^ Java Build Path

→^ Libraries

z^ To add Jars

: Select: “

Add External JARs…

” and browse for the

file name. z To add a directory to the ClassPath

: Select:

Add Variable…

→^ Configure Variables

→^ New

and add the name of the new directory.