CS100 Spring 2000 (version 0.1) I. Programming A. Definition -- automated problem solving B. Process -- tell computer what to do EXPLICITLY C. Languages -- communicate with the computer 1. Mid level -- Java 2. High level -- Matlab, Maple 3. Philosophies a. Java -- "everything is an object" b. Matlab -- "everything is an array" c. Maple -- "everyone should use Maple" ;-) 4. The rules 5. The future??? II. How to program A. Understand task B. Break problem down (baby steps!) C. Develop algorithms D. Choose language E. Program small "chunks" (methods, classes) F. Test small changes, small examples G. Try to "break" code H. Consider style, conciseness, efficiency, generality I. Save code for reuse! III. Algorithms/Development A. What would a human do? B. Figure it out C. Think in terms of language's philosophy! D. Approaches 1. Top-down: high-level abstraction to nitty-gritty chunks of code a. Create "main code" b. create class and method headers c. fill in code starting from "top" of algorithm 2. Bottom-up: develop "small" routines and connect them a. build methods that do utilities b. collect utilities in classes c. develop interactions in "main code" E. ALWAYS BREAK THE PROBLEM DOWN! F. Follow writing process: brainstorm, outline, write, rewrite, polish G. Test! Try to "break" code H. Consider style, conciseness, efficiency, generality IV. Evolutionary Programming A. Show techniques B. Solve problems C. Illustrate need to remove redundancy V. Object Oriented Programming A. Model things as object B. Benefits 1. Removing redundancy 2. Reuse code 3. Clarity (?) VI. Java and OOP A. Variables 1. Strongly typed (Java checks types at compile time) 2. Case sensitive B. Assignments 1. Variables hold values 2. Values can be primitive data or references to objects C. Control of flow 1. Selection -- making choices 2. Repetition -- repeating actions, searching, summing D. Classes 1. Help remove/reduce redundancy 2. Variables, methods 3. Blueprint for objects 4. Think as a type 5. Visibility and access: private, public, protected, default, static 6. Use $new constructor(arguments)$ to create object 7. Reference variables hold ADDRESS of object, not the object E. Variables 1. MUST ASSIGN A VALUE BEFORE USING! 2. Instance variable a. variable for object b. visible throughout object c. default types: $null$, 0, 0.0, $false$ (think zero) 3. Class variable a. modify with static -- variable for class b. visible thoughout class (all objects from class) c. can access using class name or reference variable 4. Local Variable a. Born, lives, dies in method b. Not visible outside of method c. use $this$ to distinguish with instance/class variables F. Methods 1. Avoids redundancy 2. Small chunk of code for action you to perform frequently 3. Parameters a. actual -- the variable that holds the value you pass to method (the value in the code CALLING the method) b. formal -- the variable that gets the value passed to the method (the value received by the variables in the method header) c. NO actual parameter EVER changes with a method call d. Contents of object MAY be changed e. Return values 1. must match return type 2. can use $return$ and no arguments 3. can place $return$ statement anywhere in method body 4. the returned value replaces the entire chunk of code that called the method G. Objects 1. Specific instances of classes 2. Use reference variables to hold addresses 3. Aliases -- making references share the same address of an object 4. Has-a relationship -- instance variables refer to other objects 5. Is-a relationship -- inheritance -- an object is a subclass of another H. Encapsulation 1. Set and Get 2. Information hiding a. Instance variables private b. Utility methods (methods used only in class) private c. Everything else public I. Arrays 1. Arrays are objects 2. Inherit from class $Object$ 3. Elements are like instance variables 4. Have a $length$ instance variable 5. Indexed from zero 6. Arrays have length = size = number of elements 7. No mixed types! but may have array of objects 8. Default values for basic types: null, 0, 0.0, false 9. Initializer lists and anonymous arrays 10. Multidimensional arrays a. array of 1-D arrays b. row major most popular c. other "majors", like column major d. ragged arrays J. Strings and Chars 1. Strings are objects 2. Chars are primitive types 3. Use $length()$ for string length 4. Methods from API for cool things to do 5. Upper/lower case shift: 'a'-'A' 6. Can perform arithmetic on chars K. Searching and Sorting 1. Linear search -- use $for$ loop and $if$ 2. Selection sort -- swaps 3. Insert sort -- sort with previously sorted portion L. Inheritance 1. Super/sub class 2. subclass "is a" superclass 3. sub inherits variables and methods from superclass 4. sub must have its own constructor 5. the sub constructor MUST call the super's constructor a. Java will do $super()$ autmatically if not provided b. $super(args)$ must be 1st line of each sub constructor 6. encapsulation a. $private$ means not visible/accesible in subclass b. use $protected$ for "intermediate" encaps 7. polymorphism VII. Matlab VIII. Summary A. Break problems down B. Understand problem C. Develop algorithm D. Write small chunks of code E. Test chunks and build program F. Test with small examples G. Try to break code H. Polish for conciseness, style, efficiency, and generality