Prelim 3 Topics CS100J Spring 2001 Overall: + All topics from Prelims 1 and 2 and material up to, and including, 4/10 + all Java + closed book + about 4 problems + fill-in-the blank, fill-in-the box +-----------------------------------------------------------------------------+ Arrays: + declaring: arrays are objects! + initializer lists and anonymous arrays + use [] for array indexing: declare, store, extract + $.length$ to find length of current array + initial values are "zeros" (same rules as instance variables) + 1-D, 2-D, multi-D arrays + arrays must have the type inside elements + arrays of primitive values contain those values + arrays of references to objects contain references: primitive values get stored at the "furthest" dimension + use arrays of arrays to simulate multidimensional arrays + ragged arrays + to create 2-D array, typically use row major, but could use any arrangement (up to programmer) + > 2D arrays + see Project 5, Exercise 7-9 Searching: + see TicTacToe on Project 5 (basic linear search) Sorting: + select sort (Savitch), insert sort (Project 5) + recognize and/or program the algorithm + sort from left to right and vice versa + sort ascending and descending Characters and Strings + characters are primitive types + converting characters and integers with casts + using 'a'-'A' to convert a character's case + Strings are objects + how to use String literals + use string1.equals(string2) to compare equality of string1 and string2 + making Strings from arrays of characters + $charAt$, $indexOf$, $String$ as a constructor + see string_methods.java example, E9, and P5 (TicTacToe) Inheritance: + code reuse and is-a relationships + using $extends$ to forge sub/superclass relationship + polymorphism: treat a superclass object as if it were a subclass object - a reference to a superclass object can refer to a subclass object - can also say, a subclass object can be a superclass type + arrows drawn upwards to superclass :-) + subclass objects "use" code from superclass + constructors to NOT inherit - first line of constructor must call either constructor of current class with $this(...)$ or constructor of super class with $super(...)$ - if you do not supply a $super(...)$, Java will call $super()$ automatically + order of construction: - all instance/class variables set to defaults - each constructor is invoked, starting from the "lowest" subclass - once reaching the "highest" class, that class's variables are initialized, THEN the constructor is executed - the "lower" class's variables and constructors are executed next, working "downwards" + public members inherit + methods: - methods are invoked using the class of the actual object (by both an external and internal call!) (dynamic lookup/late binding) - methods call variables that are written in the same class as the invoked methods (if no variable is written in the class, Java uses an inherited variable) - methods can be overriden (a method with same header is written in the subclass -- that method prevents the subclass from automatically using the superclass version) - method overriding is NOT overloading + variables: - variables accessed from the type of the reference variable (shows up with polymorphism) - variables not rewritten (shadowed) in the subclass are inherited - methods use variables from the class in which the method is implemented - for shadowed variables, + an inherited method uses the variable from the class in which the method was inherited (the inherited method uses the variable in the same class in which the method is written) + an overriden method uses the variable in the same class as the overriden method + $super.$: access a superclass member (variable, method)