CSE 283 Introduction to Object Oriented Design
Barbara Nostrand, Ph.D.
Your first course in software engineering probably emphasized structured programming. Your programs probably consisted of collections of functions. In Object Oriented Programming, programs consist of collections of classes. Instead of functions we have either static methods which are attached to classes or instance methods which are attached to individual objects specified by classes. Objects are not actions they are entities. Consequently, Object Oriented Design begins by identifying what objects are needded and sorting them into classes.
Large Software systems may have dozens, hundreds, or even thousands of classes. Simply distributing these classes over a collection of files in a single directory isn't enough to keep track of all of these individual class definitions. What we want is some sort of heirarchical system. In Java, packages allow us to heirarchically structure our software components. A package is a set of related classes.
You can specify the package at the top of any Java source file. The specified package name is automatically added to the front of the fully qualified name for each of the classes defined in the file. If a package name is not specified, then the default package is assumed. If you want to use a class from a package, then you can either use the fully qualified name every time you use a class from the package, or you can import either the specific classes you need from a package or the entire package.
import java.util.Scanner; // Imports the Scanner class import javax.swing.*; // imports the entire swing packageClasses belonging to the same package must be compiled and all of the source files and object files saved in the same directory or the same cluster in a database. The package name identifies either a directory in a heirarchical directdory system or a cluster in a data base system. Please consult Packages.java for a simple example.
The Java Class Library consists has dozens of predefined classes organized into a heirarchy of packages. A few commonly used pacages are as follows:
|java.io||Input and Output||PrintStream|
The Textbook and Assignments use a number of special purpose libraries as follows:
|ann.easyio||Textbook: Simplified Terminal I/O||Keyboard|
|ann.gui||Textbook: Simplified GUI I/O||CartesianPanel|
|ann.math||Textbook: Common Mathematical Objects||Matrix|
|ann.util||Textbook: Common Software Objects||Stack|
|hoj||Assignments: Assertions and Files||Assertion|
The easiest way to handle these special purpose libraries is to creaty a directory called libraries and load each of the directory hierarchies into it. Next, compile each of the Java files in the libraries Hierarchy. Then, set the Java classpath for JGRASP to include your libraries directory. Once you have done this, your Java program should be able to include these classes in the same way that you include classes from the Java Class Library.