Package type.lang

This package provides I/O, formatting, graphics, and assertion services for J2SE releases prior to 5.0 (aka JDK 1.5.0).

See:
          Description

Class Summary
IO The IO class provides a collection of static services to facilitate I/O.
SE The SE (Software Engineering)class provides a collection of static services to implement design-by-contract assertions.
UniPanel This class encapsulates a (JPanel) with buffered, instead of a callback, graphics.
UniReader A universal reader class that provides a consistent interface for reading text from disk files, URL's, or from Standard Input.
UniWriter A universal writer class that provides a consistent interface for writing text to disk files or to Standard Output.
 

Exception Summary
SEassertionException This exception is thrown when the condition passed to the SE.check(boolean, java.lang.String) method is false.
SEException  
SEinvariantException This exception is thrown when the condition passed to the SE.invariant(boolean, java.lang.String) method is false.
SEpostconditionException This exception is thrown when the condition passed to the SE.ensure(boolean, java.lang.String) method is false.
SEpreconditionException This exception is thrown when the condition passed to the SE.require(boolean, java.lang.String) method is false.
 

Package type.lang Description

This package provides I/O, formatting, graphics, and assertion services for J2SE releases prior to 5.0 (aka JDK 1.5.0). If you use the 5.0 release (or later) then you don't need this package.

The package is made up of five main classes: IO (console I/O, formatting, serialization, graphics, and test harness support), UniReader (file / URL input), UniWriter (file output), UniPanel (graphics), and SE (assertion). There are also exception classes used by SE to distinguish assertion violations.

I/O Choices

The package provides several ways to perform Standard I/O, and they are not equivalent. To demonstrate the different approaches, consider the following three fragments that read an int from the user and store it at x:

  1. int x = IO.readInt();
  2. UniReader reader = new UniReader();
    int x = reader.readInt();
  3. int x = IO.in.readInt();

The first relies on the static services of the IO class which are all line oriented (only one entry per line): the user is expected to enter an integer value and then press ENTER. This approach is the easiest to understand and we ask that all students use it because it keeps the program prompts and inputs in synch with the user entries and thus allows input to proceed in an orderly fashion.

The second approach relies on the services of the UniReader class which are all more general, allowing multiple entries per line: Each input starts from where the previous one left off. This approach is well-suited for files but can be quite confusing for beginners when used in an interactive setting. For example, consider the code fragment:

  IO.println("Enter the temperature");
  int temp = reader.readInt();
  IO.println("Fahrenheit or Celsius? (F/C)");
  char unit = reader.readChar();
and assume the user entered the value 10, pressed ENTER, and then entered 'F'. This will not work because unit will end up storing the new line character that was entered after the integer! In short, this type-ahead feature is a potential pitfall, especially for beginning users.

The third approach is similar to the second but uses the static class variables in and out which are pre-connected to Standard Input and Standard Output. It is shorter since you don't have to explicitly create the instance.

Although you can mix all three approaches in the same program, it is recommended that your program uses only one.

J2SE 5.0

If you are using J2SE release 5.0 or later, then you do not need to use the classes of this package. For console and file I/O, use the new Scanner and PrintStream classes. For assertions, use the assert keyword. For other services offered by this package, use the ToolBox class in type.lib.



Java by Abstraction: A Client-View Approach