Software Design (CSC-223 97F)
Outline of Class 8: Input, Packages, and Protection
- As some of you know, I'm setting up a small multimedia
laboratory. I'd appreciate feedback on equipment. You can find a
rough draft of the equipment list at
- I still haven't graded the first homework, partially because of my time
constraints, partially because not all of you have turned it in,
but I'll try to get it graded by Thursday.
- Just a reminder that I won't have office hours tomorrow.
- It has come to my attention that some of you don't know how to
get manual pages. Simply type
- Chapter 7 of Flanagan has more information on events.
- A few of you have asked about the Friday "case studies". The intent
is to study some piece of software, its design, and the implications
of that design. While we've generally studied "things that failed",
successes are also a reasonable case study.
- Program input in Java is somewhat more complicated than output.
- There is a
System.in object for the standard input
to a program, corresponding to the
for standard output.
System.in is a member of
- Unfortunately, you cannot directly access
- Why? Because there are typically a number of things that you want
to do with input.
- You may want to buffer the input (group a sequence
of characters into a larger array). This generally speeds up
the program and, more importantly, lets you "unread" elements
- You may want to specify the encoding used in the input.
- In Java 1.0, you must construct a
the input stream.
- This can help generalize your code, as you can quickly
change a program from reading standard input to reading
a file by building a
the file, rather than standard input.
DataInputStream provides much more useful and
usable methods than
InputStream. However, these
methods are generally only appropriate for non-character data
and assume that you've written the data using a
- The safest thing to use is
but that will require you to do a lot of work converting to
- These methods all throw IOExceptions, so you may need to
- There is no explicit end-of-file test. Rather, an
EOFException is thrown when you try to read
beyond the end of the file.
DataInputStream and its associated exceptions are not
automatically part of your program. You must import them. It is
easiest to do so with
- Java 1.1 introduced
Reader (and, more importantly,
- To get an object from which you can read lines, you would
new BufferedReader(new InputStreamReader(System.in));
readLine() method does not seem to throw
- While classes provide a way to group data and routines, most programs
need somewhat higher-level grouping.
- Often, you'll want to group related classes and interfaces
together into a coherent whole.
- This "coherent whole" is called a package.
- Why create packages?
- Because the classes interrelate and you want to provide them as
a single "product".
- To add a layer of protection (it is possible to give access permission
only to other classes in the same package).
- To help differentiate similarly named objects.
- How do you create a package?
- You put all the classes in the same file.
- You put a
package packagename directive at the
start of your code. It must be the first statement in your
- You put the compiled code in a path the corresponds to the package
name (e.g., if the package name is
package would be in the
foo/bar/baz relative to some
place on your
- What should you name your package?
- Make sure it says something about the purpose of your package.
- Try to make sure that it doesn't overlap with anyone else's
- Use a hierarchical naming system, whenever possible.
- One possibility is is something like
- You might consider using
- Modern programming practice encourages the concept of
information hiding: particular details of implementations
should be hidden from those who don't need to know those details
(e.g., the clients of a class).
- Java permits you to specify different accessibility levels for your
classes, fields, and methods.
- There are four different protection levels (the "four P's")
- package (the only one that doesn't need to be explicitly specified)
- There's a good table on page 72 of Flanagan detailing their uses.
- In general, your fields should be non-public (your design will suggest
which alternative you'll need).