LECTURE NOTES CHAPTER 2 ASSIGNMENTS - today - exam conflicts/McBurnie - survey - 2/2 CodeLab - 2/4 Assignment 4 ADMINISTRIVIA - must start assignments early if want help from consultants - textbooks should be available next Wednesday - CodeLab comes bundled w/ book - lab day Monday - CSS 1370 - must have UW student id # or previous cs id & pwd 2.5 NUMBER TYPES - every value has a type - type limits how can use value in program - distinction between - integers - whole numbers only - int type - floating-point (real) numbers: can have fractional parts - never use commas to type numbers - double type - use for floating point numbers - can use scientific notation, e.g. avogadro = 6.022e23 - why integers? - take less space - faster to process - no rounding errors - operators + - * - precedence - parenthesis 2.1 VARIABLES - variable - storage location - type, name, contents - use in place of the object stored - declaring a variable - name something so that you can refer to it later - "uninitialized variable error": must declare a variable before you can use it - specify type - intended use determines - specify name - describe the purpose of the variable - must be an identifier - identifier: name of a Java something - rules - letters, digits, underscore - cannot start with a digit - no reserved words - variable conventions - begin with a lowercase letter - if multiple words, used "mixed case", e.g. bankAccount 2.2 ASSIGNMENT - assignment operator = - sets the value associated with a variable - evaluated from right to left - "gets" not "equals" - type declaration errors - literals have implicit types - if the type of the value and the variable don't match, compiler error - initialization - giving a variable its first value - error to use a variable before initializing it 2.3 OBJECTS, CLASSES, AND METHODS - object - entity that you can manipulate in your program via methods - don't need to know internal organization - well-defined behavior - method - sequence of intructions that access internal data - to use, must only know purpose, not how done - e.g. length - calling a method - telling an object to do something - dot syntax - class - defines the characteristics (data) and behavior (methods) of a particular kind of object - every object belongs to exactly one - public interface: what you can do with the object - private implementation: how these actions are carried out - a variable can have a class as a type - primitive types vs. classes - primitives: double, int - primitivies have no methods 2.4 METHOD PARAMETERS AND RETURN VALUES - parameter - input to a method - specify in more detail what the method does - e.g. PrintStream.println() - implicit: the object on which the method is invoked - explicit - the ones in the parenthesis - every method has zero or more - return value - method output that can be used by your program - not all methods have it - when reading code, think of the return value replacing the method - method definition - occurs in class - specifies - type of returned value - type and number of explicit parameters - implicit parameter: object of the class where the method is defined - how does the compiler find the code? - first, find the class of the implicit parameters - then, match the name - overloaded methods: multiple methods of the same name in the same class - number-type of parameters must differ - compiler finally looks for match in number and ordered types of parameters 2.6 CONSTRUCTING OBJECTS - construction 1. new operator makes an object, given a class name 2. construction parameters initialize the object's data 3. object is returned - e.g. color(int r, int g, int b) // 0-255 - some classes let objects be constructed in multiple ways: color(int r, int g, int b, int a) 2.7 ACCESSOR AND MUTATOR METHODS - accessor method - returns some info about an obj w/out changing it - e.g. color.getRed() - mutator method - changes something about an obj - e.g. color.redder(int howMuch) // fictitious 2.8 IMPLEMENTING A TEST PROGRAM 1. Provide a new class 2. Supply a main method 3. Inside the main method, construct one or more objects 4. Apply methods to the objects 5. Display the results of the method calls - package: collection of related classes - import statement - when using classes defined elsewhere, must tell compiler where to find their code - syntax - e.g. import java.awt.Rectangle - everything in java.lang is imported automatically 2.9 THE API DOCUMENTATION - javadocs: http://java.sun.com/j2se/1.5.0/docs/api/ 2.10 OBJECT REFERENCES - variable whose type is a class - holds the memory location of the object, not the object itself - is called an object reference - sharing - primitives actually contain their values - example - to save space, b/c objects can get large - only significant when multiple variables refer to "same thing"