Computer Science 15-100 (APEA Section E), Summer 2008
Class Notes:  Writing Classes


Logistics

  1. Schedule
    1. Friday:  hw7 + quiz7
    2. Monday:  hw8 + quiz8
    3. Wednesday:  Final Exam
    4. Next Friday:  Term Projects + All Bonus Projects
  2. Speaking of Bonus Projects...
  3. Reading:
    1. Ch 4:  Writing Classes
    2. Ch 7.5: Varargs  (Variable-Length Parameter Lists)

Topic Outline:

  1. Varargs:  Variable-length arguments as arrays
      public static void main(String[] args) throws Exception {
        System.out.println(max(1,3,5,7,6,4,2));
      }
     
      public static int max(int... a) {
        if (a.length == 0) return 0;
        int max = a[0];
        for (int i=1; i<a.length; i++) max = Math.max(max,a[i]);
        return max;
      }
     
  2. Visibility Modifiers
    1. public:  visible to everyone
    2. protected:  only visible within the given package or within subclasses
    3. default:  only visible within the given package
    4. private:  only visible within the given class
       
  3. Instance Variables
    1. Non-static
    2. Require some object reference to access (hence, "instance" variables)
    3. Should be private (maybe default?)
    4. Can be initialized outside of a constructor
      1. Initialized before constructor is called
    5. Scope
      1. Visible from all instance methods (but not static methods!)
      2. Shadowing
      3. Using this.foo to escape shadowing
         
  4. Accessors and Mutators
    1. Accessor:  getFoo
    2. Mutator:   setFoo, clearFoo, incrementFoo, etc...
    3. For booleans:  isProperty, setProperty
       
  5. Constructors
    1. Default Constructor:  No args, get this for free unless you implement a constructor
    2. Invoking "super" constructor
    3. Invoking "this" constructor
       
  6. The toString method
    1. Every class gets a default toString method
    2. Can "override" this with your own
      1. Must have exactly this signature, visibility, and return type:
        public String toString()
         
  7. A Class's API (Application Programming Interface)
    1. All your public methods, and only your public methods
    2. What methods should be public in the Rational class?
       
  8. Encapsulation, Data Hiding, Self-Governing:
          Control access to, and changing of, object properties
    which is roughly equivalent to:
          Expose public interface without reference to private implementation

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