Here are some notes to help simplify what we have learned so far. (Thanks to UTM instructor Faye Baron for getting them started.) These notes do not represent everything that you must know for the midterm. For the midterm, you are responsible for all of the material that we have covered in class, lab, prelab, and assignment 1, except for while statements. ================================================================== An expression is a combination of one or more variables or values that are evaluated to produce a single value: 5 produces a number a produces a number (if 'a' has been declared as a number type - int, double, etc) s produces a String (if 's' has been declared as a String and initialized) 5 + 6 produces the number 11 a + 5 produces a number a++ produces a number (a + b)/c produces a number a % b produces a number s + t produces a String (if 't' is also a String) a == b produces a Boolean value (true or false) true produces a Boolean value (true) d produces an object reference (if it was declared as some class type and initialized) method calls are also expressions: they produce the value that is returned by the method ================================================================== There are 4 basic statement types: 1. Declaration which declares a variable that is either a primitive type or can reference some object: int x; JFrame y; /* note that y does not refer to a JFrame until you create and assign one. */ 2. Assignments which assign the value of the expression on the right hand side of the '=' to the variable on its left hand side: x = 5; y = new JFrame(); /* we use new to create a new object, the value returned is assigned to y */ 3. Method calls: y.show(); /* method called with object dot method signature */ System.out.println("test"); /* static method called with class name */ 4. 'return' which return a single value to the method calling statement, for all methods that are not void: return true; return 8 + y * 2; return x; ================================================================== More complex combinations - statements which direct execution flow: if statements permit execution of a body of statements only if the prestated condition is met: if (condition) { statements to be performed if condition is true; } if (b1) { statements to be performed if condition b1 is true; } else { statements to be performed if condition b1 is not true; } if (b1) { statements to be performed if condition b1 is true; } else if (b2) { statements to be performed if condition b2 is true; } while statements execute the body of statements repeatedly as long as the condition remains satisfied: while (condition) { statements to be repeatedly executed. } ================================================================== Other Observations and questions '==' versus 'equals': What is the difference between '==' and '.equals(object o)'? '==' can be used with numbers and char because they are primitives. All other non-primitive objects must be compared using the 'equals' method. If you use == to compare two non-primitive objects, then you are only verifying whether they have the same address in memory. Every class that we write is a customization of the Object class. Casts: Given: double n; (int) n casts n from a double to an int. Given: int x; (double) x casts x from an int to a double. How do you change a String 's' to an int (or to a double)? Integer.parseInt(s) returns the integer represented by 's' Double.parseDouble(s) returns the double represented by 's' What operators are used to construct Boolean expressions? comparisons and logical operators('&&','||', '!') How do you define a class? a method? We've been doing this all term - look at your notes... When do you use static? Use a static variable if you only need one for the whole class and not one for each object in the class. Use a static method if the method is independent of any perticular object.