DOC PREVIEW
UMD CMSC 131 - Lecture 29: Packages

This preview shows page 1-2-3-4-5-6 out of 19 pages.

Save
View full document
Premium Document
Do you want full access? Go Premium and unlock all 19 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Lecture 29 Packages Last time 1 2 dimensional arrays Today 1 Project 6 due 2 2 dimensional array example 3 Packages 11 6 2006 CMSC 131 Fall 2006 Rance Cleaveland 2006 Univeristy of Maryland Project 6 Assigned Project due Sunday 11 12 at 11 pm Project is closed You must complete the project by yourself Assistance can only be provided by teaching assistants TAs and instructors You must not look at other students code Start now Read entire assignment from beginning to end before starting to code Check out assignment now from CVS Follow the instructions exactly as much of grading is automated CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 1 2 d Arrays Calendar Example Goal classes implementing month at aglance appointment calendar Approach Implement month using 2 d array of day objects Each row is a week Day object contains list of appointments CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 2 CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 3 Calendar Code Month java Day java Driver java Generates random appointments to put in month CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 4 Java Program Organization Java program 1 or more Java source files Source file 1 or more class and or interface declarations If a class interface is public the source file must use the same base name So only one public class interface per source file Can have non public classes We will discuss later Packages When a program is large its classes can be organized hierarchically into packages CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 5 Packages Package A collection of related classes and or interfaces Examples The Java API java lang Essential classes for the Java language java text Facilities for formatting text output java util Special utilities e g Scanner java net Network communication Packages can be divided into subpackages java awt java awt font java awt geom Classes for GUIs and graphics Classes and interface for fonts Classes for 2 dimensional objects CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 6 Access to Package Members Review Use fully qualified name java util Scanner sc new java util Scanner System in Can use import to tell Java where to look for things defined outside your program import java util Scanner Scanner sc new Scanner System in Using imports all classes in package import java util Scanner sc new Scanner System in Be careful with you may overwrite definitions Multiple import statements allowed java lang automatically imported by every Java program CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 7 User Defined Packages Why Packages enable a programmer organize the code into smaller logically related units A large program may consists of hundreds of classes 800 in one current project with NASA Every class is part of some package If you do not specify a package a class becomes part of the default package Access Classes defined within the same package can access one another more easily no need for imports fully qualified names Some classes object fields only accessible to classes in same package CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 8 Defining Packages To define add package statement to specify package containing the classes of a file package mypackage public class myClass myClass is part of mypackage Must be first non comment statement in file Packages organized into subpackages using the notation foo subpackage package mypackage mysubpackage public class myClass2 myClass2 is part of mysubpackage which is within mypackage Packages in Eclipse Select File New Package Enter the full name of the package CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 9 Class Access and Packages Class access within a package Classes within a package can refer to each other without full qualification If a class or member within a class is not declared public it can only be accessed by other classes within the package Class access across packages A public class can be accessed from other packages Its name is fully qualified or it must be is imported to achieve this The public classes of a package can be seen as the interface of the package with the outside world Importing a package does not automatically import subpackages E g import java awt does not import java awt font CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 10 Example Packages graphics graphics shapes Files Classes Files Driver java Driver Files Circle java Rectangle java OtherShape java Circle Rectangle OtherShape graphics otherstuff Files PublicClass1 java PublicClass2 java PublicClass1 NonPublicClass1 PublicClass2 CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 11 Example graphics shapes package graphics shapes public class Circle private double radius public String toString return I m a circle File Circle java File Rectangle java 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 OtherShape java Note Classes of this package can be accessed without the need for import or using graphics shapes Circle CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 12 Example graphics otherstuff package graphics otherstuff File PublicClass1 java public class PublicClass1 Public class Accessible everywhere public String toString return This is a PublicClass NonPublicClass1 message Nonpublic class Only accessible in this package class NonPublicClass1 static public String message return I m a nonpublic class File PublicClass2 java package graphics otherstuff public class PublicClass2 Compiler error We have no direct access to classes private Driver d outside this package Import or use qualified name private Circle c1 private graphics shapes Circle c2 Okay using the fully qualified name public String toString return This is a PublicClass2 NonPublicClass1 message Okay Can access a nonpublic classes within this package CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 13 Example graphics Package package graphics import graphics shapes Circle File Driver java Note importing Circle but nothing else public class Driver public static void main String args testShapes testOtherStuff public static void testShapes Okay Circle is accessible Circle c new Circle System out println c toString Compiler


View Full Document

UMD CMSC 131 - Lecture 29: Packages

Documents in this Course
Set #3

Set #3

7 pages

Exam #1

Exam #1

6 pages

Exam #1

Exam #1

6 pages

Notes

Notes

124 pages

Notes

Notes

124 pages

Load more
Download Lecture 29: Packages
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Lecture 29: Packages and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Lecture 29: Packages and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?