DOC PREVIEW
Berkeley COMPSCI 61B - Lecture Notes

This preview shows page 1 out of 2 pages.

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

Unformatted text preview:

02/19/1416:48:21 113 CS 61B: Lecture 13 Wednesday, February 19, 2014Today’s reading: Sierra & Bates, pp. 154-160, 587-591, 667-668.JAVA PACKAGES=============In Java, a _package_ is a collection of classes and Java interfaces, andpossibly subpackages, that trust each other. Packages have three benefits.(1) Packages can contain hidden classes that are used by the package but are not visible or accessible outside the package.(2) Classes in packages can have fields and methods that are visible by all classes inside the package, but not outside.(3) Different packages can have classes with the same name. For example, java.awt.Frame and photo.Frame.Here are two examples of packages.(1) java.io is a package of I/O-related classes in the standard Java libraries.(2) Homework 4 uses "list", a package containing the classes DList and DListNode. You will be adding two additional classes to the list package.Package names are hierarchical. java.awt.image.Model refers to the class Modelinside the package image inside the package awt inside the package java.Using Packages--------------You can address any class, field, or method with a fully-qualified name.Here’s an example of all three in one. java.lang.System.out.println("My fingers are tired.");Java’s "import" command saves us from the tedium of using fully-qualified namesall the time. import java.io.File; // Can now refer to File class, not just java.io.File. import java.io.*; // Can now refer to everything in java.io.Every Java program implicitly imports java.lang.*, so you don’t have to importit explicitly to use System.out.println(). However, if you import packagesthat contain multiple classes with the same name, you’ll need to qualify theirnames explicitly throughout your code. java.awt.Frame.add(photo.Frame.canvas);Any package you create must appear in a directory of the same name. Forexample, the photo.Frame class bytecode appears in photo/Frame.class, andx.y.z.Class appears in x/y/z/Class.class. Where are the photo and xdirectories? They can appear in any of the directories on your "classpath".You can specify a classpath on the command line, as when you type javac -cp ".:˜jrs/classes:libraries.jar" *.javaThis means that Java first looks in ".", the current directory, then looks in˜jrs/classes/, then finally in the _Java_archive_ libraries.jar when it’slooking for the photo and x directories. The classpath does not include thelocation of the Java standard library packages (those beginning with java orjavax). The Java compiler knows where to find them.Building Packages-----------------The files that form a package are annotated with a "package" command, whichspecifies the name of the package, which must match the name of the directoryin which the files appear./* list/SList.java */ | /* list/SListNode.java */ |package list; | package list; |public class SList { | class SListNode { SListNode head; | Object item; int size; | SListNode next;} | }Here, the SListNode class and its fields are marked neither public, private,nor protected. Instead, they have "package" protection, which falls somewherebetween "private" and "protected". Package protection is specified not byusing the word "package", but by using no modifier at all. Variables arepackage by default unless declared public, private, or protected.A class or variable with package protection is visible to any class in the samepackage, but not to classes outside the package (i.e., files outside thedirectory). The files in a package are presumed to trust each other, and areusually implemented by the same person. Files outside the package can only seethe public classes, methods, and fields. (Subclasses outside the package cansee the protected methods and fields as well.)Before we knew about packages, we had to make the fields of SListNode public sothat SList could manipulate them. Our list package above solves this problemby giving SListNode and its fields package protection, so that the SList classmay use SListNodes freely, but outside applications cannot access them.In Homework 4, you’ll see a different approach. There, the DListNode class ispublic, so that DListNodes can be directly held by application programs, butthe "prev" and "next" fields have package protection, so an application cannotaccess these fields or corrupt the DList ADT. But an application can hopquickly from node to node because it can store DListNode references and usethem as parameters in DList method calls.Each public class must be declared in a file named after the class, but a classwith package protection can be declared in any .java file (usually foundtogether with a class that uses it). So a public SList class and a packageSListNode class can both be declared in the file list/SList.java, if you feellike it.Compiling and running files in a package is a bit tricky, because it must bedone from outside the package, using the following syntax: javac -g list/SList.java java list.SListHere’s the correspondence between declarations and their visibility. Visible: in the same package in a subclass everywhere Declaration "public" X X X "protected" X X default (package) X "private"02/19/1416:48:21 213ITERATORS=========In java.util there is a standard Java interface for iterating over sequences ofobjects. public interface Iterator { boolean hasNext(); Object next(); void remove(); // The remove() method is optional. }Part of Project 1 is to write a class RunIterator that implements an Iteratorfor your RunLengthEncoding class. Its purpose is to provide an interface bywhich other classes can read the runs in your run-length encoding, one by one.An Iterator is like a bookmark. Just as you can have many bookmarks in a book,you can have many Iterators iterating over the same data structure, each oneindependent of the others. One Iterator can advance without disturbing otherIterators that are iterating over the same data structure.The first time next() is called on a newly constructed Iterator, it returns thefirst item in the sequence. Each


View Full Document

Berkeley COMPSCI 61B - Lecture Notes

Documents in this Course
Lab

Lab

4 pages

Matrix

Matrix

3 pages

Numbers

Numbers

14 pages

Lectures

Lectures

12 pages

Project 1

Project 1

24 pages

Exam

Exam

8 pages

Load more
Download Lecture Notes
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 Notes 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 Notes 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?