cs231 Lecture Notes Week 1, Wednesday Big picture of CS231 Things to get out of this class, from abstract to concrete 1) practice with programming, and more Java features (new features as we need them, for the most part) 2) exposure and understanding of programming paradigms functional, procedural, object-oriented 3) algorithm design, problem-solving 4) exposure to famous/useful data structures applicable to all languages/paradigms 5) exposure to famous/useful algorithms 4&5 sometimes go hand in hand ALGORITHMS + DATA STRUCTURES = PROGRAMS 6) performance analysis 7) understanding of abstract data types interfaces and implementations Syllabus me! And now... Java review! Chapter 1 --------- Syntax and semantics Three kinds of errors 1) compile time: parsing 2) run time (exceptions) 3) logical or semantic Chapter 2 --------- Variables primitive types: int char double boolean (String) values have types variables have types (declare before use) declaration assignment initialization expressions have types Chapter 3 --------- Floating-point versus integer division void method definitions invoking void methods What are methods good for? 1) provide name 2) facilitate reuse 3) encapsulate in order to reduce cognitive load Chapter 4 --------- Conditionals: simple, alternate, chained, nested. New thing! switch command: alternative form for chained conditional switch (char) { case 'a': // do something with 'a' break; case 'c': // do something with 'b' break; default: // handle the error? break; } The switch value has to be a char or int. The cases do not have to be exhaustive. The default case is optional, but almost always a good idea. Don't forget the breaks! Type casting (remember!) int x = (int) Math.log (17.0); int y = Math.round (Math.log (17.0)); Chapter 5 --------- Fruitful methods. All return statements have to provide an expression with the promised type. Overloading: you can have more than one method with the same name as long as they take different numbers or types of arguments. Fun with overloading and automatic type conversion... public class Test { public static double add (double x, int y) { System.out.println ("Version 1"); return x + y; } public static double add (int x, double y) { System.out.println ("Version 2"); return x + y; } public static void main (String[] args) { System.out.println (add (3.0, 4)); System.out.println (add (5, 6.0)); // System.out.println (add (7, 8)); } } Boolean operators and methods. Recursion and stack diagrams: 1) when you invoke a method you get an instance of the method that contains its parameters and local variables. 2) these instances go on a stack. 3) there had better be a base case, or the stack gets really big and causes a StackOverflowException. Example: factorial. public static int factorial (int n) { if (n == 0) { return 1; } else { int recurse = factorial (n-1); int result = n * recurse; return result; } }