V CS 231: Intro to Programming—Final Exam Review
V About the exam
* Tuesday 6 May, from 2-5 pm in the usual lecture room (Collins 408)
* resources: textbook, lecture notes, class hand-outs, previous exam reviews, lab assignments
* exam will be three hours, closed book/notes and cumulative
> Topics from the first exam—see previous review sheet!
V Arrays
V An array is a linearly-ordered collection (set) of Objects or values
* arrays allow us to maintain a large number of related values (called the elements) together
* the number of values may be set at run-time (when the array is created)
* although any given array is always of a fixed length, we can set an array variable to point to a newly-created, larger array
V we can use loops to run through all the elements of an array, using the .length field of the array as a loop limit
* if an array is not filled up all the way, we may want to use a separate variable to tell how full it is
V Arrays and initialization
V an array variable itself may be uninitialized (it will be null)
* int[] myArray;
V an array variable may be initialized (it points to the array) but the elements may be uninitialized (they will be null)
* int[] myArray = new int[10]
* we can inititialize all (or some) of the elements of an array in a loop
V an array can be initialized with a set of specific values without telling how many there are (braces notation
* int[] myArray = {1, 2, 3, 4, 5};
V Access to elements through indexing
* we can get (or assign) an array element by using the bracket notation myArray[i]
* array element indices always start at 0 and run up to (but not including) the length of the array
V The switch statement
* the switch statement is like a generalized if-else statement: it allows handling multiple cases in separate clauses
V the syntax is:
* switch (expression) {
case c1: statements
...
case cn : statements
default : statements }
* each "c" must be a constant of type int or char
V the semantics is:
* evaluate the expression after the switch, then choose a clause based on the value and execute all the statements in that clause; continue on to the next clause unless there is a break statement
* the default label is used if no other constant matches the value of the expression
* note: very important! remember that you must use a break statement at the end of each clause, or execution will fall through into the next case. This is a common cause of errors.
V Sub-classes and super-classes
V a (sub) class may be declared to extend an existing (super) class; an object or instance of a sub-class contains all parts of the original super-class
* syntax: class A extends class B { ... (normal stuff here) ... }
V objects of the sub-class have all the variables and methods of the super-class
* note carefully! this means that the sub-class properties are a superset of the super-class ones!!
* a method (or variable) definition which is available in a sub-class in this way is said to be inherited from the super-class
* if a method is defined in the super-class and re-defined in the sub-class, the sub-class definition is given preference: this is called an over-ride of the super-class definition
* one class may extend another, which extends another, etc.
* however, a given class can extend only one other one directly
* this leads to a class hierarchy of classes, their sub-classes, sub-sub-classes, etc., like an organization chart in a business (one at the top, branching out down below)
* when we look for a variable or method for some object, we look first in its own class, then in any super-class, then any super-super-class, etc., stopping and using the first definition we find
V Exceptions
* see the Java website for details
* exceptions are a program structuring mechanism which allow us to ignore potential errors for the duration of longer blocks of code
* exceptions allow us to concentrate on what is likely to happen, and to restrict attention to errors to specific locations in the code
* exceptions are objects of (a sub-class of) the Exception class in Java
V exceptions are thrown (either by the programmer or the system) when something goes wrong
* (as a programmer, we write "throw new Exception()", i.e., we create a new exception object and throw it)
V exceptions can be caught using a try-catch block
* try {
... <body statements> ...
}
catch (Exception e) {
... <error statements> ...
}
* the body statements are executed and, if an exception occurs, it is bound to the name e and the error statements are executed: control continues to the statement after the try-catch bock
* methods which might throw an exception without catching it must be marked with a clause "throws Exception" in their header (even if the throw comes indirectly from a method they call)
* some exceptions (array index out-of-bounds, division by zero and null reference access) are so common that they do not need to be explicitly marked with throws clauses: these are called unchecked exceptions
* when an event is thrown, it propagates up the chain of called methods, eliminating them until a catch block is found
V Programming with references
* a variable whose type is a class holds a reference to an object—by contrast, a variable of primitive type holds just an actual value of that type
> having a reference to an object, via a variable, allows us to call methods on that object
V when we make changes to a referenced object, the object itself changes, and all references to it can "observe" those changes
* (this does not work the same way with values of primitive type: when they are used as arguments to methods, a copy of the value is passed in; changes to the original just over-write the original, and copies do not change
V if we use a reference to some object as an argument in a method call, we can pass a reference to that object to some other object: this is like giving someone a calling card or business card, allowing them to contact someone we know directly
* example: customer.tell(subcontractor)