exam will be three hours, closed book/notes and cumulative
Topics from the first exam—see previous review sheet!
Programming and programming languages
Computers, programs and languages; the programming process; low- and high-level languages
Java programming
Object-Oriented programming; Java and/versus the NetBeans development environment
hierarchy of Java syntax: packages; class definitions; method definitions; statements; expressions; variables & constants
hierarchy of Java semantics: packages; classes and sub-classes; objects & references; method calls & run-time stack; variables & values
Statements, expressions and variables
A statement does something; an expression has a value. Methods may do both, by performing some instructions and then returning a certain value as their result.
parameters are special variables filled in when a method is called; the calling method fills in the values (inside parentheses)
Objects and Classes
Objects hold data, run methods and communicate with each other (via methods); public and private access; classes and constructors
Types, classes, variables and references
Variables (= type, name, box and value); primitive values and data types; object references and class types
Static and non-static
variables and methods declared static are stored in the class, not in individual objects
variables and methods declared without static are stored/associated with a specific created object
Methods and parameters
The back-and-forth of method calls; parameter and return types; parameter lists (in a method definition) versus argument lists (in a method call); the (run-time) method call stack
Sub-classes and super-classes
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
If statements
The basic if statement; else clauses; issues surrounding block usage (blocks are not required, but allowed)
Loops
a for loop has a header (three parts) and a body (a single statement, typically a block statement between curly braces)
the header of the for loop has three parts (separated by semi-colons): initialization, check and update
the parts are executed as follows: initialization first, then check, then the body, then update
Special features
break: you can use a break statement anywhere in a loop; it will cause the program to skip to the end of the loop and execute the statement after the loop
continue: a continue statement is similar, but just skips back to the header of the loop, without finishing the body
Arrays
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
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
Arrays and initialization
an array variable itself may be uninitialized (it will be null)
int[] myArray;
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
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};
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
The switch statement
the switch statement is like a generalized if-else statement: it allows handling multiple cases in separate clauses
the syntax is:
switch (expression) { case c1: statements ... case cn : statements default : statements }
each "c" must be a constant of type int or char
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.
Sub-classes and super-classes
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) ... }
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
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
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
example: myMinion.doSomething();
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
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