basic.java
Erzeugt mit JBuilder
package klasse5;

/*                 THE JAVA TEXT PROGRAM
                       28 April 1999
            Copyright (c) 1999 Per Brinch Hansen

This program defines a set of Java classes for text
processing. These text classes enable Java programs to
output text to screen and disk files and input text from
keyboard and disk files. The present version runs on
Macintosh, Unix, and Windows 95 systems.

Please follow these instructions to install the text classes
on a Unix system:

  1. Create a directory for your Java programs;

  2. Move this program text to your program directory as a
     file, named text.java;

  3. If you received the program text by email, use an
     editor to delete everything above the first line and
     everything below the last line of the program;

  4. Compile the text program into executable Java code. The
     compilation adds 11 code files to your program
     directory:

       basic.class     infile.class   input.class
       keyboard.class  outfile.class  output.class
       random.class    reader.class   screen.class
       test.class      writer.class

  5. Run a simple test of the text program by executing the
     compiled class, named test, as a self-contained Java
     program. If the compilation was successful, you will be
     asked to type your name, followed by return. When you
     have typed your name, say, Jane Doe, the program
     responds with the message: Welcome to Java, Jane Doe!

The basic class defines Unix as the default system:

   public static final int OS = Unix;

If you wish to compile the text classes on a Macintosh (or
Windows 95) system, you must first use an editor to replace
the word, Unix, by the word, Mac (or Win95).

Please leave the text file, text.java, and the compiled
classes in your program directory. Java programs that use
the text classes must be compiled and run in the same
directory.

The following example shows a user program, named typist,
that uses the text classes:

  class typist extends basic
  { public static void main(String param[]) throws Exception
    { input in = new input();
      output out = new output("results");
      while (in.more()) out.write(in.read());
      in.close(); out.close();
    }
   }

The program extends the basic class. The program copies text
from the keyboard to a new disk file, named results. The
input ends when you type a line consisting of the word, eof,
followed by return.
------------------------------------------------------------
Modifications by H.Moessenboeck, August 1999

- Class basic exports static variables in and out that
  represent the standard input and output stream.
  Methods of class input and output don't throw exceptions
  any more. Thus a hello world program becomes as simple as

 	 class Hello extends basic {
  		public static void main (String[] arg) {
  			out.writeln("hello world");
  		}
 	 }

 - All methods of class input set a boolean variable done,
   which indicates if the operation was successful. For example,
   reading a sequence of numbers becomes:

      x = in.readint();
      while (in.done) {
      	...
      	x = in.readint();
      }

- The constructor as well as the close method of class output
  set a boolean variable done indicating the success of the
  operation.

- Input and output files are always allocated and searched in
  the directory that contains the class files.

- Deprecated method DataInputStream.readLine avoided.

- Class random has a new method to set the seed, which is now
  an instance variable instead of a static variable.
------------------------------------------------------------*/

import java.io.*;
import java.lang.Math;

/* THE BASIC CLASS
=============================================================
Defines common constants and methods.
*/

class basic
{ public static final int
    Mac = 0, Unix = 1, Win95 = 2,
    OS = Win95;
  public static final String EOF = "eof";
  public static final char
  	cr = '\r',
  	eof = '\uFFFF',
    nl = '\n',
    sp = ' ';

  public static output out;
  public static input in;


  static /*initializer*/
  { try {
    	out = new output();
    	in = new input();
    } catch (Exception e) {
    	error("could not open standard input/output");
    }
  }

  public static void assume(boolean condition)
  { assume(condition, "invalid assumption"); }

  public static void assume(boolean condition, String msg)
  { if (!condition) error(msg); }

  public static void error(String msg)
  { out.writeln("*** error: " + msg); }
}

/* THE WRITER INTERFACE
=========================================================
   Defines a writer as any class that includes a write
   method for outputting a single character and a close
   method for terminating output.
*/

interface writer
{ public void close() throws Exception;
  public void write(char value);
}

/* THE SCREEN CLASS
==========================================================
   Implements a writer interface for outputting single
   characters only to a screen.
*/

class screen extends basic implements writer
{ public screen() { }

  public void close() { }

  public void write(char value)
  { System.out.print(value); }
}

/* THE OUTFILE CLASS
==========================================================
   Implements a writer interface for outputting single
   characters only to a disk file. (Uses the class,
   PrintStream, that has been deprecated, but is still
   available in JDK 1.1.5.)
*/

class outfile extends basic implements writer
{ private PrintStream out;

  public outfile(String name) throws Exception
  { out = new PrintStream(new FileOutputStream(name)); }

  public void close() throws Exception
  { if (out != null) { out.close(); out = null; } }

  public void write(char value)
  { if (value == nl)
      switch (OS)
        { case Mac: out.print(cr); break;
          case Unix: out.print(nl); break;
          case Win95: out.print(cr); out.print(nl); break;
        }
    else out.print(value);
  }
}

/* THE OUTPUT CLASS
==========================================================
   Used to output a text file to a screen or disk.
*/

class output extends basic
{ private writer out;

  public boolean done;

  public output()
  { out = new screen(); done = true; }

  public output(String name)
  { try {
  		out = new outfile(name);
  		done = true;
  	} catch (Exception e) {
  		error("could not open output file " + name);
  		done = false;
  	}
  }

  public void close()
  { try {
  		if (out != null) { out.close(); out = null; }
  		done = true;
  	} catch (Exception e) {
  		error("could not close output file");
  		done = false;
  	}
  }

  public void write(boolean value)
  { write(value, 1); }

  public void write(boolean value, int width)
  { String word = (String)(value?"true":"false");
    writespace(width - word.length());
    write(word);
  }

  public void write(char value)
  { out.write(value); }

  public void write(char value, int width)
  { write(value); writespace(width - 1); }

  public void write(double value)
  { write(value, 1); }

  public void write(double value, int width)
  { String numeral = String.valueOf(value);
    writespace(width - numeral.length());
    write(numeral);
  }

  public void write(int value)
  { write(value, 1); }

  public void write(int value, int width)
  { String numeral = String.valueOf((int)value);
    writespace(width - numeral.length());
    write(numeral);
  }

  public void write(String value)
  { write(value, 1); }

  public void write(String value, int width)
  { int length = value.length();
    for (int i = 0; i < length; i++)
      write(value.charAt(i));
    writespace(width - length);
  }

  public void writeln()
  { write(nl); }

  public void writeln(boolean value)
  { writeln(value, 1); }

  public void writeln(boolean value, int width)
  { write(value, width); writeln(); }

  public void writeln(char value)
  { writeln(value, 1); }

  public void writeln(char value, int width)
  { write(value, width); writeln(); }

  public void writeln(double value)
  { writeln(value, 1); }

  public void writeln(double value, int width)
  { write(value, width); writeln(); }

  public void writeln(int value)
  { writeln(value, 1); }

  public void writeln(int value, int width)
  { write(value, width); writeln(); }

  public void writeln(String value)
  { writeln(value, 1); }

  public void writeln(String value, int width)
  { write(value, width); writeln(); }

  public void writesame(char value, int number)
  { for (int i = 1; i <= number; i++) write(value); }

  public void writespace(int number)
  { writesame(sp, number); }
}

/* THE READER INTERFACE
===========================================================
   Defines a reader as any class that includes a read
   method for inputting a single character and a close
   method for terminating input.
*/

interface reader
{ public void close() throws Exception;
  public char read();
}

/* THE KEYBOARD CLASS
===========================================================
   Implements a reader interface for inputting single
   characters only from a keyboard. (Uses the String
   method, readLine, that has been deprecated, but is
   still available in JDK 1.1.5.)
*/

class keyboard extends basic implements reader
{ private InputStream in;
  private char[] buffer = new char[80];
  private int typed, used;

  public keyboard() throws Exception
  { in = System.in;
  	typed = 0; used = 0;
  }

  public void close() { }

  public char read()
  { char ch;
  	try {
  		if (used == typed) {
	        typed = 0; used = 0;
	      	ch = (char)in.read();
  			while (ch != eof && ch != cr && ch != nl) {
  				buffer[typed] = ch; typed++;
  				ch = (char)in.read();
  			}
  			if (OS == Win95 && ch == cr) ch = (char)in.read();
  			String s = new String(buffer);
	        if (ch == eof)
	          { buffer[typed] = eof; }
	        else
	          { buffer[typed] = nl; }
		typed++;
	      }
	    ch = buffer[used];
	    if (ch != eof) used = used + 1;
	    return ch;
	} catch (Exception e) {
  		error("problems reading System.in");
  		return eof;
	}
  }
}

/* THE INFILE CLASS
===========================================================
   Implements a reader interface for inputting single
   characters only from a disk file.
*/

class infile extends basic implements reader
{ private InputStream in;

  public infile(String name) throws Exception
  { in = new FileInputStream(name); }

  public void close() throws Exception
  { if (in != null) { in.close(); in = null; } }

  public char read()
  { try {
  		char ch = (char)in.read();
   		switch (OS) {
      		case Mac:
      			if (ch == cr) ch = nl; break;
      		case Unix:
       			/*skip*/ break;
      		case Win95:
      		    while (ch == cr) ch = (char)in.read(); break;
  		}
  		return ch;
  	} catch (Exception e) {
  		error("problems reading from input file");
  		return eof;
  	}
  }
}

/* THE INPUT CLASS
===========================================================
   Used to input a text file from a keyboard or disk.
*/

class input extends basic
{ private reader in;
  private boolean ahead;
  private char ch /*next character (if ahead)*/;

  public boolean done; /*true if previous input operation was successful*/

  public input()
  { try {
  		in = new keyboard();
  		done = true;
  	} catch (Exception e) {
  		error("could not open keyboard");
  		done = false;
  	}
  	ahead = false; }

  public input(String name)
  { try {
  		in = new infile(name);
  		done = true;
  	} catch (Exception e) {
  		error("could not open input file " + name);
  		done = false;
  	}
  	ahead = false; }

  public boolean blank()
  { getahead();
    return (ch == cr) | (ch == nl) | (ch == sp);
  }

  public void close()
  { try {
  		if (in != null) { in.close(); in = null; }
  		done = true;
  	} catch (Exception e) {
  		error("could not close input");
  		done = false;
  	}
  }

  public boolean digit()
  { getahead();
    return ('0' <= ch) & (ch <= '9');
  }

  private void getahead()
  { if (!ahead) readnext(); else done = ch != eof;}

  public boolean letter()
  { getahead();
    return (('a' <= ch) & (ch <= 'z')) |
      (('A' <= ch) & (ch <= 'Z'));
  }

  public boolean more()
  { getahead(); return ch != eof; }

  public char next()
  { getahead(); return ch; }

  public char read()
  { getahead(); ahead = false; return ch; }

  public void readblanks()
  { while (blank()) readnext(); }

  public boolean readboolean()
  { boolean value; String symbol = readname();
    if (symbol.equals("false")) value = false;
    else if (symbol.equals("true")) value = true;
    else {value = false; done = false;}
    return value;
  }

  public double readdouble()
  { StringBuffer symbol = new StringBuffer();
    readblanks();
    if (ch == '+') readnext();
    else if (ch == '-')
      symbol.append(read());
    while (digit()) symbol.append(read());
    if (ch == '.')
      { symbol.append(read());
        while(digit()) symbol.append(read());
      }
    if ((ch == 'e') | (ch == 'E'))
      { symbol.append(read());
        symbol.append(readint());
      }
    try {
  	  Double numeral = new Double(symbol.toString());
  	  done = true;
  	  return numeral.doubleValue();
  	} catch (Exception e) {
  	  done = false; return 0;
  	}
  }

  public int readint()
  { StringBuffer symbol = new StringBuffer();
    readblanks();
    if (ch == '+') readnext();
    else if (ch == '-')
      symbol.append(read());
    while (digit()) symbol.append(read());
    try {
    	done = true;
    	return Integer.parseInt(symbol.toString(), 10);
    } catch (Exception e) {
    	done = false; return 0;
    }
  }

  public String readline()
  { StringBuffer line = new StringBuffer();
    getahead();
    while (ch != nl && ch != eof)
      { line.append(ch); readnext(); };
    ahead = false;
    done = done || line.length() > 0;
    return line.toString();
  }

  public void readln()
  { String skipped = readline(); }

  public String readname()
  { StringBuffer letters = new StringBuffer();
    readblanks();
    while (letter() | digit() | (ch == '_'))
      { letters.append(ch); readnext(); }
    done = letters.length() > 0;
    return letters.toString();
  }

  public void readnext()
  { ch = (char)in.read(); ahead = true; done = ch != eof;}

  public String readword()
  { StringBuffer word = new StringBuffer();
    readblanks();
    while (!blank())
      { word.append(ch); readnext(); }
    done = word.length() > 0;
    return word.toString();
  }
}

/* THE RANDOM CLASS
============================================================
   Used to generate random integers (or doubles) which are
   uniformly distributed in a given range.
*/

class random
{ private static final double
	a = 16807.0,
    b = 2147483647.0;
  private double seed;
  private double m, n;

  public random(int min, int max)
  { basic.assume(min <= max, "invalid random bounds");
    long time = System.currentTimeMillis();
    seed = (double)(time%1000000 + 1);
    m = (double)min; n = (double)max;
  }

  private double f()
  /* 0.0 <= f < 1.0 */
  { double temp = a*seed;
    seed = temp - b*Math.floor(temp/b);
    return seed/b;
  }

  public double readdouble()
  /* m <= readdouble < n */
  { return m + f()*(n - m); }

  public int readint()
  /* m <= readint <= n */
  { return (int)Math.floor(m + f()*(n - m + 1)); }

  public void setseed(int n)
  { seed = (double)(Math.abs(n)%1000000 + 1); }
}





basic.java
Erzeugt mit JBuilder