V CS 231: Introduction to Programming—Midterm Review
V About the exam
* The exam will be held on Wednesday 2 April 2008 at 12:50pm (normal lecture time); it will last for 90 minutes.
* The exam will cover material from the lectures, programming assignments and from the textbook (see below) covered in class up to Spring Break.
* Topical coverage is detailed below, along with a quick guide to finding material in the textbook. You should concentrate on understanding concepts and syntax of the Java language.
* Test questions may include true/false, multiple choice, short answer and analysis of program examples (e.g., what happens when we run this program).
You may be asked to write short pieces of code, but only about 1-3 lines, in a context which will be provided for you.
V Textbook coverage
V study the following textbook chapters:
* all of chapters 1, 2 and 3
* supplement 3G for graphics
* chapter 4, sections 4.1 and 4.2
* chapter 5, but only section 5.2
* chapter 7, sections 7.1-7.3
* chapter 8, sections 8.1-8.7
* also check lecture notes, the labs and other materials on the home page for more details, pictures, etc.
V Programming and programming languages
* programming: designing a fixed set of instructions describing some task precisely
* after sketching them out, programs are expressed in a programming language
* much time is spent re-thinking, re-designing, re-writing and re-running programs
* machine language (machine code): the native language of instructions a machine can perform, "ones and zeros"
* high-level languages: make programming conceptually closer to humans thought; e.g., Java
* compiler: translates a high-level program into a machine code program, once and for all (for us, this is part of Dr. Java)
V Java programming
V A program is broken up into parts, each nested within the other, according to the following hierarchy:
* packages: collections of classes, grouped together into a hierarchy according to related purpose. These are often imported into a java program so that libraries of code can be re-used (example: import java.awt.*)
* classes: collections of variables and methods, grouped together to represent a template for objects which can store internal data (the variables) and have certain capabilities (the methods). New "copies" or instances of the template (class) are "stamped out" by calling a constructor method (example: Circle a = new Circle(100,100,20); ). Each class should have a constructor
* methods: sequences of statements (possibly with a return at the end) which perform some action related to an object. The method is a "general recipe" which is specialized to a given situation by its use of an object's data and by passing parameters.
* statements: abstract instructions which do things to change the computer's memory, either directly (by assigning new values to variables) or indirectly (by calling other methods). Statements are abstract versions of the computer's basic instructions. So far we know assignment statements, if statements, method calls (like graphic.drawOval(…)) and return statements.
* expressions: like mathematical formulæ, these are built from constant values (specific numbers), variables (like x and y) using operators (such as + or *) and perhaps method calls (e.g., Math.sqrt()). Some expressions have boolean values rather than integer values (e.g., a.overlap(b) || a.overlap(c))
* variables: places in the computer's memory, like a slot or a box, in which values can be stored.
V 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.
* The body of a method is essentially a sequence of statements, instructions which cause things to happen at run-time in the computer's memory.
* parameters are special variables filled in when a method is called; the calling method fills in the values (inside parentheses)
* expressions are built from basic pieces (literals, variables and method calls) using operators
* operators have precedences: e.g., multiplication is done before addition (and similarly for other operators)
* relational operators compare numbers and evaluate to booleans (true/false)
* logical operators combine boolean values into new ones
V Types, classes, variables and references
* variables are boxes or slots which hold values (such as numbers, booleans (true/false) or references to objects)
* a variable has some space (the box), a name and a type
* the variable is located somewhere in the computer's memory; it must be big enough to store the information for the variables value
* the name of the variable is visible within its method, or if declared just within a class, to all methods in the class
* if a class has a certain variable (such as x) and a method in that class also has a variable of the same name (x), the method "sees" only its own x, unless we write "this.x"
* if a variable is of primitive type (lower case name, pre-defined) its value is stored in the box; if it has an object type (uppercase name, often user-defined) then a reference (e.g., arrow) is used and the object's value is thus shared
V Control flow and communication
* the program starts when the main method of the class is called (for applets, we never see this method)
* methods call other methods in order to communicate and to "delegate authority"
* control returns from each method when it is done to the place from which it was called
* this leads to a "call stack", in which the most recently-called method is on top, and other, pending methods are below it
* a method may return a value when it is done; this value can be used in an expression such as "this.distance(other) > this.radius + other.radius"
V 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
* static methods cannot access non-static data: there isn't a specific object involved, so no specific data
* non-static methods may access static data (they just access the data stored in their class)
* we usually use non-static data, because usually objects store their own (non-static) information
* static methods and data are useful if we only want one of something (like a Main class with a main() method)
* static data might also be used for a kind of "global information" (e.g., a counter for how many objects of some class have been created)
V Methods and parameters
V Rationale for methods
* methods allow us to abbreviate or consolidate common "blocks" of code
(for example, one set of graphics calls to draw a StickFigure, versus repeating the same code)
* parameters allow us to abstract out common structure relative to some systematic variation
(for example, making the StickFigure drawing consistent, but dependent on x and y parameters)
V Uses of methods
* methods may provide an interface to the outside world so an object can be used
* methods may provide internal consolidation of repeated code for purposes of convenience
V Methods and return values
* some methods are called only for their effects: these should have a void return type
* others are called to return a value (e.g., in an expression); these have non-void return types
* if a method has a non-void return type, it must execute a return statement
V If statements
V The basic if statement
* if ( expr ) statement
* parentheses are required
* expr must be type boolean
V The else clause
* if ( expr ) statement else statement
V Nested if and the dangling else problem
* when several if statements occur in a row, the else goes with the most recent if before it
* you can always use braces { } to specify the association (like parentheses for expressions)
V Repetition statements (for loops)
V The basic structure of a for loop
* a for loop has a header (three parts) and a body (a single statement, typically a block statement between curly braces)
V the header of the for loop has three parts (separated by semi-colons): initialization, check and update
* inititalization: executed only once, often used for setting up a loop counter variable
* check: a boolean expression used to decide whether to continue or stop the loop
* update: executed after the loop body, often used to increment loop counters
* any of the four parts may be empty!
V How a for loop is executed
* the parts are executed as follows: initialization first, then check, then the body, then update
* the check-body-update part is repeated until the check fails (is false)
V Array-based for loops
* Java also allows a variation on for loops for running through all elements of an array
* the header does not have three parts: just one!
* it declares a variable of the array element type and indicates (with a colon (:)) which array will be used
* in the body of the loop, the declared variable will take on the value (successively) of each element of the array
V example:
* for (Star s : stars) {
s.paint(g);
}
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