cs230 Lecture Notes Week 1, Friday Before yesterday's lab you should have read the first two chapters of the book and done the exercises that are interspersed. For next Monday there is no additional reading, but you might want to review Java, possibly using the online textbook "How to Think Like a Computer Scientist", which is available online. http://www.cs.colby.edu/~downey/ost/thinkCS/java_html/index.html Also, you should work on Homework 0, which I will hand out in class, and which is also available from the web page. Homework 0 is due on Tuesday. Chapter 1: Using built-in objects --------------------------------- Structure of Java programs: program = set of class definitions + startup class one (public) class definition per file, filename same as class name startup class must have: public static void main (String[] args) To compile: > javac a.java b.java c.java Produces a.class b.class c.class To execute > java a Just name the startup class. No suffix. Vocabulary ---------- package: A collection of classes. The built-in Java classes are organized in packages. AWT: The Abstract Window Toolkit, one of the biggest and most commonly-used Java packages. instance: An example from a category. My cat is an instance of the category ``feline things.'' Every object is an instance of some class. instance variable: One of the named data items that make up an object. Each object (instance) has its own copy of the instance variables for its class. reference: A value that indicates an object. In a state diagram, a reference appears as an arrow. aliasing: The condition when two or more variables refer to the same object. garbage collection: The process of finding objects that have no references and reclaiming their storage space. state: A complete description of all the variables and objects and their values, at a given point during the execution of a program. state diagram: A snapshot of the state of a program, shown graphically. Chapter 2: Creating your own objects -- Monday --------------------------------------------------------------- Java review ----------- If there is a topic you are fuzzy about, you might want to read or print the corresponding chapter. HTTLCS Chapter 1 ---------------- Syntax and semantics Three kinds of errors 1) compile time: parsing 2) run time (exceptions) 3) logical or semantic HTTLCS 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 HTTLCS 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 HTTLCS Chapter 4 ---------------- Conditionals: simple, alternate, chained, nested. switch: 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 int x = (int) Math.log (17.0); int y = Math.round (Math.log (17.0)); HTTLCS 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; } }