Computer Science 15-100, Fall 2008
Class Notes:  Changes to Course Notes (Additions, Deletions, Errata)


Note:  These changes either have been made or will soon be made to the course notes for this semester.  Most have already been discussed in class and/or in email.


  1. Do not throw exceptions in equals methods
    Wrong:
       class Foo {
         public boolean equals(Object object) {
           Foo that = (Foo) object;  // crashes if object is not of type Foo
           ... compare "this" and "that" ...
         }
       }
    Right:
       class Foo {
         public boolean equals(Object object) {
           if ((object == null) || (! (object instanceof Foo)))
             return false;
           Foo that = (Foo) object; // can safely cast to Foo now!
           ... compare "this" and "that" ...
         }
       }

     
  2. Using statically-allocated arrays in expressions
    The problem:
       int[] a = { 2, 3, 4 }; // ok
       a = { 1, 2, 3 };       // not ok -- will not compile!

    The solution:
       int[] a = { 2, 3, 4 };
       a = new int[]{ 1, 2, 3 }; // works!

     
  3. Null arrays
    Small point, but the notes do not indicate that you can assign "null" to an array reference, and use equality tests for null, just as you can to any other object reference:
       int[] a = null;
       if (a == null)
         System.out.println("yes!");
     
  4. Proper use of paint
    1. Using helper methods in JComponentWithEvents
      The key is to pass the Graphics2D parameter.  Nearly all of you did this in your Tetris hw (which is good).  But the notes should show an example of it.
       
  5. Improper use of paint
    1. Do not call paint methods (Swing calls paint for you)
    2. Do not store the Graphics2D parameter in an instance variable
    3. Do not modify any instance variables inside any paint methods
       
  6. Proper use of Random
    1. Place the singleton Random instance in a static variable
    2. Use SecureRandom (slower but "more random")
         public static final Random random = new java.security.SecureRandom();
       
  7. Simple accessors and mutators
    You all know that for every private instance variable "foo", you should have a public accessor "getFoo" and, when appropriate, a public mutator "setFoo", but the notes did not explicitly spell this out.  [ As an aside, this is a somewhat simplistic treatment of private/public, but it is the standard for intro CS courses. ]
     
  8. Instance methods accessing private instance variables of other instances (that.foo)
    Methods in a class have access to all fields (private or otherwise) of ANY INSTANCE of that class.  So this works:
    class Foo {
      private int x;
      public int compareTo(Object object) {
        Foo that = (Foo) object;
        return this.x - that.x;
      }
    }

     
  9. insertionsort with a while loop
    We presented insertionsort with a for loop, but given that the inner loop counts down rather than up and also that it has a mildly complicated termination test, some people prefer to write that inner loop using a while loop:
      public static void insertionsort(int[] a) {
        int n = a.length;
        for (int i=0; i<n; i++) {
          int j=i;
          while (j>0 && a[j] < a[j-1]) {
            swap(a, j, j-1);
            j--;
          }
        }
      }

    We'll include both approaches in the notes.
     
  10. Useful web sites
    1. Flag design
      This site is so useful for flag design hw's -- it gives detailed scale and layout information and specific RGB values for the flags.  For example, check out:  http://www.vexilla-mundi.com/bahamas_flag.html
      We'll include this reference in future flag-based hw problems.
       
    2. Animated sorting algorithms:
      http://vision.bc.edu/~dmartin/teaching/sorting/anim-html/all.html
       
    3. Others?
      If you have other suggestions, we'd love to hear them!  Thanks!
       
  11. Pending Additions:
    1. Using Monte Carlo Methods
      Rather than explain this in hw4, make an explicit note that explains it, including how to count the # of trials, the # of successes, and compute the expected value as the ratio of these.
       
    2. Writing 2D Board Games
      Prior to the Connect4 (or similar) assignment, present a framework for a 2D board game, showing how to store the board in a 2d array, how to do the traditional nested for loops to traverse the board, how to display a board using
       
    3. Using Swing
      Perhaps add a note describing some basics of Swing (buttons, menus, textfields, layouts, etc).
       
    4. Deployment (jars, resources, applications, applets)
      This has been much-requested.  We will add a note that demonstrates a simple program that reads in a text file, an image file (a jpeg), and music files (midi and wav files), running in 3 modes:  "dev" mode (where it is unpackaged, but still has access to its resources), packaged as an application jar file, and packaged as a web-based applet jar file.  This note may note be completed until after the final exam this semester, but we will post it to the course mailing list when it is available.
       
  12. Pending Deletions:
    1. Reduce/eliminate chapter 1 topics
       
  13. Under Review
    1. JCF with or without generics  (ArrayList  vs  ArrayList<String>)?
    2. Term project?
       
  14. Pending Update to JComponentWithEvents
    We will release the next version of JComponentWithEvents within a week or two.  We will send an email to the class list at that time (that will be the final email to the class list, in fact).  The changes will include:
    1. javadoc html documentation
    2. version checking in background (for fast launching)
    3. no version or field-modification-in-paint checking in applet mode
    4. more key constants
    5. improved exception handling
      (better handling of file+reflection security exceptions in applet mode)
    6. modified launch method (less "magical")
    7. resources (images and music files)
    8. drawing images
    9. adding buttons, textFields, and other Swing objects
    10. 2d board game support?
    11. Other changes?
      Are there other features you would like to see in JComponentWithEvents?  Please let us know!  Thanks!
       
  15. Other changes?
    We are always seeking ways to improve this course -- both in terms of how effectively we meet the learning objectives of the course, as well as how well we meet your individual needs and interests.  To that end, we very much welcome any suggestions you might have on how to modify the course, from smaller changes to individual notes to larger changes regarding our entire approach.  Please let us know your thoughts!  Thanks!!!

carpe diem   -   carpe diem   -   carpe diem   -   carpe diem   -   carpe diem   -   carpe diem   -   carpe diem   -   carpe diem   -   carpe diem