CS100B Week 2 Section Answers to some common questions: Q: Want to use JDK (Java Development Kit)? A: See Appendix L for help. Q: What's the difference between main(String[] args) and main(String args[])? A: The [] indicate an array. For now, don't worry about it. We'll discuss more about Strings and arrays later. See page 53 for more info. Q: Why bother with TokenReader? Why not use the BufferedReader stuff? A: This is long. See below: Background ********** Review the following keyterms: * programming statement - instruction carried out by a program - using Java, ends with a semicolon (;) * class - collections of methods and data - what's a method? see below * method - group of programming statements with a given name - performs a task - similar to concept of a function - example) main is a method that starts processing in a Java program * invoke or call - to execute a method * class library - set of classes that support program development - usually predefined/preprogrammed methods accessible by, but not actually part of, language API *** * definition - The Java Application Programming Interface (API) is a collection of preexisting software available to a Java program. - The API is a class library. * contents - The API contains packages, each of which containing related methods. - See pg. 62 (Fig. 2.11) for examples of different packages. java.lang ********* * contents - Belongs to Java API. - The java.lang package contains "basic" and "common" methods that many programs would require. * to import or not to import? - No, importing by import java.lang.*; not necessary. - Automatically loaded by Java. - Also, see pg. 806. * so, what's "basic?" - Standard I/O - see java.io for another package that handles more I/O - What's I/O? (see below) I/O *** * definition - I = input - O = output * uses - bringing data into program - printing data from program - how? Java uses streams for I/O * stream - any source of data for input - any destination for data for output Standard Streams **************** * types - System.in - read input - System.out - write output - System.err - write error message * see API for other kinds Buffers ******* * When reading/writing I/O, Java stores data in buffer. A buffer is essentially computer memory holding the data. * Input - data held in input buffer until the Enter or Return key is pressed. - lets you use Backspace and arrow keys to edit data before the input buffer is truly entered * Output - data held in output buffer until flushed - flushing sends data to output device, like computer screen Input ***** * The "standard" way: - Read a string - Use java.io package // import java.io.* methods // We're using the BufferedReader class contained in java.io import java.io.*; // do your public static etc "thing" public class something { public static void main(String args[]) { // set up text input BufferedReader stdin = new BufferedReader (new InputStreamReader(System.in)); // declare variable for input String mesg; // String is the type, mesg is the variable // read a string as input and assign the input to // variable mesg mesg = stdin.readLine(); // continue with program // you can now use mesg as a string statements; } } * Numeric Input: - basically, a mess when using "standard" Java - do BufferedReader thing again - For integers: int k; k = Integer.parseInt(mesg); - For floats and doubles, - convoluted approach - use Double.valueOf to convert * A "cleaner" way: - TokenReader designed here at Cornell - Saves hassles with converting strings - Does type checking on input - See CUCS Application stationery // set input using TokenReader class TokenReader in = new TokenReader(System.in); // Integer input int k = in.readInt(); // Double (floating-point) input double d = in.readDouble(); Output ****** - System.out.println belongs to java.lang package - println automatically flushes output buffer - If use System.out.print, you might have to use the code System.out.flush(); to flush the buffer - Will discuss formatted output another time.