ISE 582: Web Technology for Industrial Engineering University of Southern California DJE Dept of Industrial and Systems Engineering Lecture 7 JAVA Cup 7: It’s all in the Packaging
17 October 2002Web Technology for IE2 Handouts Lecture 7 slides READ Winston & Narasimhan : –Chapters (pp )
17 October 2002Web Technology for IE3 JAVA Cup 7 Characters and Strings –Codestrings and delimiters How to Read/Write Objects How to Modularize Programs Creating a Window
17 October 2002Web Technology for IE4 Specifying Delimiters You can use codestrings to specify interesting information in textfile
17 October 2002Web Technology for IE5 Specifying Delimiters To advise the tokens tokenizer to use double quotation marks to delimit strings: –tokens.quoteChar((int) ‘”’) To tell the tokenizer to recognize carriage returns: –tokens.eolIsSignificant(true);
17 October 2002Web Technology for IE6 Working with O/p File Streams To connect to output file –FileOutputStream stream = new FileOutputStream(“output.data”); To write more than 1-byte-at-a-time –PrintWriter writer = new PrintWriter(stream); It’s good to flush out buffered characters –writer.flush() Close the file –stream.close()
17 October 2002Web Technology for IE7 Reading / Writing Objects Serialized input-output operations –.writeObject( ) –.readObject() Indicate intentions to use operations by –implements Serializable –throws IOException, ClassNotFoundException Each instance must belong a class that implements the Serializable Interface
17 October 2002Web Technology for IE8 Review streamreaderbuffer streamwriter streamreadertokens
17 October 2002Web Technology for IE9 Reading Objects from File FileInputStream fileInputStream = new FileInputStream(“in.data”); ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream); Vector = (Vector) objectInputStream.readObject();
17 October 2002Web Technology for IE10 To Write Objects to File FileOutputStream fileOutputStream = new FileOutputStream(“out.data”); ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream); objectOutputStream.writeObject( ); objectOutputStream.close();
17 October 2002Web Technology for IE11 Modularing Your Program Compilation Units Packages Setting CLASSPATH Calling Methods from a Module Access to Var/Methods in Package
17 October 2002Web Technology for IE12 Compilation Units Define all classes in same file Only one class can be public Only one class is universally accessible Convention: each class has its own compilation unit
17 October 2002Web Technology for IE13 Packages Use single-class compilation units Indicate module using package statement at beginning of each c.u –package onto.java.entertainment; Convention: package names consist of components separated by dots
17 October 2002Web Technology for IE14 CLASSPATH Package names correspond to paths –package onto.java.entertainment; –Files stores in./onto/java Setting the path –setenv CLASSPATH : :. –setenv CLASSPATH= ; ;. UNIX PC
17 October 2002Web Technology for IE15 Using Methods from a Package First import package –import onto.java.entertainment.* –Auxiliaries.readMovieFile( “input.data” ) –(( Movie ) vectorElement).rating() Or, refer to entire name –onto.java.entertainment.Auxiliaries.readMovieFile ( “input.data” ) –(( onto.java.entertainment.Movie ) vectorElement).rating()
17 October 2002Web Technology for IE16 Access to Variables/Methods private : –available only to methods inside own class protected : –available to methods in same class, c.u. or package, subclasses No keyword : –available only to c.u. or package
17 October 2002Web Technology for IE17 How to Create Windows Nomenclature Intro to Swing classes Hierarchy of Swing classes Listener Classes Window Creation Pattern
17 October 2002Web Technology for IE18 Nomenclature GUI ( pronounced goo-ey ) : –Graphical User Interface Components : –class instances that have a graphical representation Containers : –components that allow nesting of other containers inside their boundaries Window : –a container’s graphical representation
17 October 2002Web Technology for IE19 Introduction to Swing (J) Classes API = Application Programmer’s Interface The AWT Package: java.awt.* –AWT = Abstract Window Toolkit –contains Component, Container classes The Swing Package: java.swing.* –contains JFrame, JApplet, JComponent, JPanel classes Try to restrict display work to Swing classes
17 October 2002Web Technology for IE20 Hierarchy of Swing Classes
17 October 2002Web Technology for IE21 Example: A Window import javax.swing.*; public class Demonstrate { public static void main (String argv []) { JFrame frame = new JFrame (“ I am a window, square and proud ”); frame.setSize(200,200); frame.show(); }
17 October 2002Web Technology for IE22 Listener Classes Event as a state change –Mouse clicks and key presses –Variable-value changes Event as instance of EventObject class –Describes state change Java deals with events through Listener Classes Listener classes are defined by –Extending existing listener classes –Implementing listener interfaces
17 October 2002Web Technology for IE23 When mouse clicks: –a WindowEvent instance is created –the windowClosing method is called with connected listener as target and WindowEvent instance as arg Example: Link listener to frame
17 October 2002Web Technology for IE24 Adapter Classes Adapter classes implement all interface-required methods as do- nothing methods. You can define shadowing methods in your subclass for whatever subset of methods you need
17 October 2002Web Technology for IE25 Nesting Class Definitions Advantage of nesting classes: –Method definition (windowClosing) appears in proximity to window definition –Can reuse names of private classes such as LocalWindowListener
17 October 2002Web Technology for IE26 Window Creation Pattern Define a subclass of JFrame class Main method calls constructor Constructor: –assigns title, size, and displays a window –creates required class instances –connects class instances