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
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
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
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
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
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
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
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:

11/6/2006 CMSC 131 Fall 2006Rance Cleaveland©2006 Univeristy of MarylandLecture 29:PackagesLast time:1. 2-dimensional arraysToday1. Project #6 due 2. 2-dimensional array example3. PackagesCMSC 131 Fall 2006Rance Cleaveland©2006 University of Maryland1Project #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 automatedCMSC 131 Fall 2006Rance Cleaveland©2006 University of Maryland22-d Arrays: Calendar Example Goal: classes implementing “month-at-a-glance” appointment calendar Approach: Implement month using 2-d array of day objects Each row is a week Day object contains list of appointmentsCMSC 131 Fall 2006Rance Cleaveland©2006 University of Maryland3CMSC 131 Fall 2006Rance Cleaveland©2006 University of Maryland4Calendar Code Month.java Day.java Driver.javaGenerates random appointments to put in monthCMSC 131 Fall 2006Rance Cleaveland©2006 University of Maryland5Java Program Organization Java program1 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 PackagesWhen a program is large, its classes can be organized hierarchically into packagesCMSC 131 Fall 2006Rance Cleaveland©2006 University of Maryland6Packages Package A collection of related classes and/or interfaces Examples: The Java APIjava.lang Essential classes for the Java languagejava.text Facilities for formatting text outputjava.util Special utilities (e.g. Scanner)java.net Network communication Packages can be divided into subpackagesjava.awt Classes for GUIs and graphicsjava.awt.font Classes and interface for fontsjava.awt.geom Classes for 2-dimensional objectsCMSC 131 Fall 2006Rance Cleaveland©2006 University of Maryland7Access 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 programimport 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 programCMSC 131 Fall 2006Rance Cleaveland©2006 University of Maryland8User-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 packageIf 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 packageCMSC 131 Fall 2006Rance Cleaveland©2006 University of Maryland9Defining Packages To define: add package statement to specify package containing the classes of a filepackage mypackage;…public class myClass { … } // myClass is part of mypackageMust 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 packageCMSC 131 Fall 2006Rance Cleaveland©2006 University of Maryland10Class 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 subpackagesE.g. import java.awt.* does not import java.awt.fontCMSC 131 Fall 2006Rance Cleaveland©2006 University of Maryland11ExampleFiles:Driver.javaDriverFiles:Circle.javaRectangle.javaOtherShape.javaFiles:PublicClass1.javaPublicClass2.javaCircleRectangleOtherShapePublicClass1NonPublicClass1PublicClass2graphicsgraphics.shapesgraphics.otherstuffPackages: Files:Classes:CMSC 131 Fall 2006Rance Cleaveland©2006 University of Maryland12Example: graphics.shapespackage 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.javaFile: Rectangle.javaFile: OtherShape.javaNote: Classes of this package canbe accessed without the need forimport or using graphics.shapes.CircleCMSC 131 Fall 2006Rance Cleaveland©2006 University of Maryland13Example: graphics.otherstuffpackage graphics.otherstuff;public class PublicClass1 {public String toString( ) { return "This is a PublicClass: " + NonPublicClass1.message( ); }}class NonPublicClass1 {static public String message( ) { return "I'm a nonpublic class"; }}package graphics.otherstuff;public class PublicClass2 {private Driver d;private Circle c1;private graphics.shapes.Circle c2;public String toString( ) { return "This is a PublicClass2: " + NonPublicClass1.message( ); }}File: PublicClass1.javaFile: PublicClass2.javaPublic class: Accessible everywhereNonpublic class: Only accessible in this packageCompiler error! We have no direct access to classes outside this package. Import or use qualified name.Okay, using the fully qualified name.Okay: Can access a nonpublic classes within this package.CMSC 131 Fall 2006Rance Cleaveland©2006 University


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 2 2 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?