package console; /* ATTENZIONE: il package "console" risiede nel jar-file "console.jar" che deve essere aggiunto al CLASS_PATH, sia per compilare: javac -classpath console.jar Test.java sia per eseguire un programma che vi fa riferimento: java -classpath .:console.jar Test (Integrazione di Claudio Mirolo, maggio 2003) */ /* The file defines a class Console. Objects of type Console are windows that can be used for simple input/output exchanges with the user. Various routines are provided for reading and writing values of various types from the output. (This class exists mainly because System.out and System.in don't work very well, at least not for beginning programming students. The file TextIO.java defines a class that provides an easy interface to System.out and System.in. That interface includes the same put and get routines defined in class Console, except that they are static routines that use System.in and System.out for IO. An applet version of Consoles is also available. See the file ConsoleApplet.java.) This class is dependent on another class, ConsoleCanvas. To use a console in a program, write a class with the usual "public static void main(String[] arg)" main program. In that program, when you create an object of type Console, a console window will open. Let's say that the name of your variable is con. You would declare "Console con" and then set "con = new Console()". You can then interact with the user with statemens such as "con.putln(x)" and "x = con.getlnDouble()". When you are finished with the console, you should close it using the close() method: "con.close()". Unless the console is only for use in the main() routine, it should be declared outside the main() routine as a static variable. You won't be able to compile and run your program unless the Console and ConsoleCanvas classes are available to it. Console.class and ConsoleCanvas.class should be in the same directory as your program when you run it. When you compile your program, either the source files, Console.java and ConsoleCanvas.java, or the compiled class files, should be in the same directory with your source code file. (If you are using a development environment such as Visual J++ or CodeWarrior, you can add the source files to your project.) Note that when a console has the input focus, it is outlined with a bright blue border. If, in addition, the console is waiting for user input, then there will be a blinking cursor. If the console is not outlined in light blue, the user has to click on it before any input will be accepted. (This is a newer version of a Console class that I originally wrote in 1996 and updated in December 1997. The newest version has been updated to make it fully compiant with Java 1.1 and to use the "realToString()" method for output of real data types.) Written by: David Eck Department of Mathematics and Computer Science Hobart and William Smith Colleges Geneva, NY 14456 Email: eck@hws.edu WWW: http://math.hws.edu/eck/ July 17, 1998 */ public class Console // ***************************** Constructors ******************************* // default constructor just provides default window title and size public Console(); // open window with specified title and default size public Console(String title); // create and open console window with given title, width, and height public Console(String title, int width, int height); // close the window; can't use the window after doing this! public void close(); // *************************** I/O Methods ********************************* // Methods for writing the primitive types, plus type String, // to the console window, with no extra spaces. // // Note that the real-number data types, float // and double, a rounded version is output that will // use at most 10 or 11 characters. If you want to // output a real number with full accuracy, use // "con.put(String.valueOf(x))", for example. public void put(int x); // Note: also handles byte and short! public void put(long x); public void put(double x); // Also handles float. public void put(char x); public void put(boolean x); public void put(String x); // Methods for writing the primitive types, plus type String, // to the console window,followed by a carriage return, with // no extra spaces. public void putln(int x); // Note: also handles byte and short! public void putln(long x); public void putln(double x); // Also handles float. public void putln(char x); public void putln(boolean x); public void putln(String x); // Methods for writing the primitive types, plus type String, // to the console window, with a minimum field width of w, // and followed by a carriage return. // If outut value is less than w characters, it is padded // with extra spaces in front of the value. public void putln(int x, int w); // Note: also handles byte and short! public void putln(long x, int w); public void putln(double x, int w); // Also handles float. public void putln(char x, int w); public void putln(boolean x, int w); public void putln(String x, int w); // Method for outputting a carriage return public void putln() { newLine(); } // Methods for writing the primitive types, plus type String, // to the console window, with minimum field width w. public void put(int x, int w); // Note: also handles byte and short! public void put(long x, int w); public void put(double x, int w); // Also handles float. public void put(char x, int w); public void put(boolean x, int w); public void put(String x, int w); // Methods for reading in the primitive types, plus "words" and "lines". // The "getln..." methods discard any extra input, up to and including // the next carriage return. // A "word" read by getlnWord() is any sequence of non-blank characters. // A "line" read by getlnString() or getln() is everything up to next CR; // the carriage return is not part of the returned value, but it is // read and discarded. // Note that all input methods except those for char's and lines will // skip past any blanks and carriage returns to find a non-blank value. // getln() can return an empty string; getChar() and getlnChar() can // return a space or a linefeed ('\n') character. // peek() allows you to look at the next character in input, without // removing it from the input stream. (Note that using this // routine might force the user to enter a line, in order to // check what the next character.) // Acceptable boolean values are the "words": true, false, t, f, yes, // no, y, n, 0, or 1; uppercase letters are OK. // None of these can produce an error; if an error is found in input, // the user is forced to re-enter. // Available input routines are: // // getByte() getlnByte() getShort() getlnShort() // getInt() getlnInt() getLong() getlnLong() // getFloat() getlnFloat() getDouble() getlnDouble() // getChar() getlnChar() peek() getln() // getWord() getlnWord() getString() getlnString() // // (getlnString is the same as getln and is onlyprovided for consistency.) public byte getlnByte(); public short getlnShort(); public int getlnInt(); public long getlnLong(); public float getlnFloat(); public double getlnDouble(); public char getlnChar(); public boolean getlnBoolean(); public String getlnWord(); public String getlnString(); // same as getln() public String getln(); public byte getByte(); public short getShort(); public int getInt(); public long getLong(); public char getChar(); public char peek(); public float getFloat(); // can return positive or negative infinity public double getDouble(); public String getWord(); public boolean getBoolean(); } // end of class Console