Prelim 3 Topics CS100 Fall 2000 Overall Prelim 3 + 100% Java + closed book + about 4 problems + short answer, fill-in-the blank, fill-in-the box Language Elements + keywords + primitive types (int, double) + identifiers (rules, naming convention) + Issues: - Java is OOP - Java is strongly typed - Everything is an object Flow Control + block statements + condition: $if$, $if$-$else$, $if$-$else if$-$else$ + repetition: $while$,$for$,$do$-$while$ Methods + syntax + components of a header - return type - name - formal parameters (must have types) + how to access (from an object or within an object) + returning values - return value has a type - $void$ does not return a value but you may still use $return$ w/o a value + returning a value and printing are DIFFERENT! + all variables pass by value, including references + local variables and scope + overloading Classes + class is blueprint; object is instance of class + INSTANTIATION: creating an object using the class + structure of class - instance variable(s) (usually private) - constructor(s) - method(s) - setters (set instance vars -> make public) - getters (get instance vars -> make public - utility (methods used only in class) + $this$ uses: - this.varname when a method has a local variable with same name as varname - refers to current object - may also use as a constructor (not required for exam) + methods inside a class can all "see" eachother + instance vars inside a class can all "see" eachother + $toString()$ - builtin method to print a description of an object - activates when code "prints the object" - user must define in class to make "pretty" output for "printing the object" Objects + create with $new$ operator: 1) new Classname(arguments); 2) Classname refvar; refvar = new Classname(arguments); 3) Classname refvar = new Classname(arguments); + the code $new Classname(arguments)$ returns a REFERENCE to a newly created object + a reference is an address of a newly created object + alias: setting a refvar to another refvar -- they both point to the same object + passing the reference to a method means passing the address of an object to a local variable -- the local variable becomes as alias of the object + objects that have no reference variable connection become "unreachable" Constructors + use rules for methods but, no return type and have same name as class! + may be overloaded + if you do not provide a constructor, Java gives you empty constructor by default + if you give at least one, Java does NOT automatically give you a default constructor $static$ + CLASS VARIABLES: variables in class that are labeled as static + CLASS METHODS: methods in class that are labeled as static + all objects in class can see the same static things + changing a static variable changes that var's value for all objects in the same class + when to use? when all objects in the class need/use the same thing encapsulation + "spread out" code + make methods for getting and setting instance variables + hide information with $private$ when possible + objects "passed inside" another class can directly access members even if the fields are labelled as $private$ $final$ + modifier for variable to make it constant (unassignable) + write constants in all UPPERCASE strings + Strings are objects + use string1.equals(string2) to compare equality of string1 and string2 Box Diagrams + see online file for entire set of rules