CS202 Site Index
- Main
- Information
- Registration
- Announcements
- Lectures
- Assignments
- Submit
- View
- Reference
- Passwords
Java logo CS202 - Spring 1999
Transition to Java

Lectures: Week 2

divider line
Mon, 1 Feb  
  • Arrays:
    - all types of arrays are Objects
    - declaration eg. int a[];, Piece board[][];
    - allocation eg. a=new int[5];, board=new Piece[15][]; for (int i=0; i<15; i++) board[i]=new Piece[15];
    - arrays have length field (read only)
    - 2D arrays do not have to be square
  • Control flow:
    - same as in C/C++: if-then, if-then-else, for, while, do-while, switch-case, methods
    - exceptions
  • Exceptions:
    - idea: try a piece of code, an error could be thrown, catch error and handle
    - try {some code} catch (Exception e) {handle error}
    - somewhere inside try and error may occur and an exception thrown
    - good because your "normal case" code is not cluttered with propagation of error cases
    - control flow goes from the throw point directly to the first matching catch on the caller stack
    - if no error, then catch is skipped
    - finally is ALWAYS executed no matter if error occurs or not, but always after error-handling
    - if no one catches thrown exception it gets propagated to VM, aborting program
    - subclasses of Exception (actually those implementing Throwable) can be thrown.
    - type of exception indicates error type; object can contain information used for error handling
    - object thrown is "caught" by catch clause
    - if exception not caught, it can be passed on to caller by declaring it in throws clause of method
    - subclasses of RunTimeException are implicit and do not need to be declared in throws clause
    - other exceptions must either be caught and handled or declared in throws clauses
  • Inner classes:
    - as of JDK 1.1 classes can be defined within classes
    - used when implementing adapters, creating helper/interface objects or GUI hooks
    - refer to the Java Language Specification inner classes addendum
    - useful when class is only needed temporarily or only within scope of current class
    - semantics can get a little tricky, so use only for simple things (read reference for more detail)
    - note that the virtual machine was not changed even though the language was!

 
Wed, 3 Feb  
  • Graphical user interfaces (GUI):
    - Abstract Windowing Toolkit (AWT)
    - Swing / Java Foundation Classes (JFC)
    - other class libraries (some portable, some not)
    - Swing built entirely using AWT
    - AWT included in standard class library
    - Swing included in standard class as of JDK 1.2
  • Swing/JFC:
    - Window: JFrame
    - Layout: Border, GridBag, Grid, absolute positioning
    - Containers: JPanel
    - Components: JButton, JList, JLabel...
    - add components to containers
    - containers are components themselves, so can nest
    - can create new components
  • Data/view model:
    - Data is an java.util.Observable object
    - View is an java.util.Observer object
    - Views add themselves to observer list of data
    - Data notifies observers to update upon change
  • Event driven programming:
    - program has state (combined state of objects)
    - reacts to user events
    - user interface (components) generate events
    - Listener objects are attached to components
    - listeners respond to events with apropriate behaviour on state
  • Reading:
    - Swing tutorial
    - Swing examples

 
Fri, 5 Feb  

divider line
Copyright 1999, Rimon Barr, Cornell University RBcs202-sp99: l.2.html (17600)