COMP 202 – Final Review

advertisement
COMP 202 – Final Review
Xue (Steve) Liu
Email: xueliu@cs.mcgill.ca
Final Exam
• Friday, April 20th at 9:00 AM-12:00 Noon
• Today we will give a review of Final exam
• On Thursday, we will give the review of the past
two final exams from previous semesters
• A "road map" of the exam is posted.
– rough list of the questions we will ask (very general).
– http://www.cs.mcgill.ca/~cs202/2012-01/web/finalexam/finalexamroadmap.pdf
– It lets you plan time a bit and know what to expect 
Knowledge points
Brief examples
types:
primitive types, reference types
8 primitive types in java;
others are reference types, such as String, etc.
expressions, boolean expressions
x > 5 || y < 2;
if-statement
if (x > 10) {…}
else if (x > 0 && x < 5) {…}
else {…}
loops (while/for)
while (true) {…}
for (int i = 0; i < 100; i++) {…}
Array
int[] i = new int[10];
int[] j = {1, 2, 5};
ArrayList
ArrayList<String> list = new ArrayList<String>();
OOP: class definition, constructor,
getter/setter, property, method
JetAgent class in A4
recursion
factorial, Fibonacci
exception handling
try {
} catch (Exception e) {
e.printStackTrace();
}
Types
• 8 primitive types
– byte, short, int, long, float, double, boolean, char
• Reference types
• Constructor
• MyClass myClass = new MyClass();
allocates
Variable:
myClass
contains
• Go to address: locationX
Reference type: Aliasing
• ArrayList<String> boxingMatchWith = new
ArrayList<String>();
• In Player class, a method
– void addMatchSchedule (String name) {
boxingMatchWith.add(name);
}
• Player Alice = new Player(“Alice”, boxingMatchWith);
• Player Bob = new Player(“Bob”, boxingMatchWith);
• Alice.addMatchSchedule(“Mike Tyson”);
Poor Bob…
Reference Types: Aliasing
• ArrayList<String> getMatchSchedule () {
• return boxingMatchWith;
• }
• ArrayList<String> a1 = Alice.getMatchSchedule();
• ArrayList<String> a2 = Bob.getMatchSchedule();
• a1.add(“Prof. Liu”);
• a2.remove(“Mike Tyson”);
•
Who would Alice and Bob box with?
Alias illustration (1)
Alice.addBoxSchedule
boxingMatchWith
MikeTyson
Alias illustration (2)
Alice.addBoxSchedule
boxingMatchWith
a2
MikeTyson
a1.add
Prof. Liu
Alias illustration (3)
Alice.addBoxSchedule
boxingMatchWith
a2.remove
MikeTyson
a1.add
Prof. Liu
Alias illustration (4)
Alice
boxingMatchWith
Bob
Prof. Liu
Expressions
• Use explicit parentheses when there is the
possibility of confusion
–3-2*2+1
3 – (2 * 2) + 1
• Determine the type of an expression, then
calculate value
– "" + ’F’ + ’o’ + 21
String, “Fo21”
• Boolean expression
– Be familiar with &&, ||, !, <=, >=, ==, <, >
Boolean Expression
• The character stored in variable c of type char
is a digit (Fall 2011 midterm)
– c >= ‘0’ && c <= ‘9’
• A character is a lower-case letter in the
alphabet
– c >= ‘a’ && c<= ‘z’
Boolean Expression Exercises
Requirement (Fall 2011 final exam)
boolean b1, b2; write an expression
to check whether at least one of
them is true.
Given an int[] array of size 3,
write a boolean expression
indicating whether exactly 2 values
in the array are even.
Answer
Boolean Expression Exercises
Requirement (Fall 2011 final exam)
Answer
boolean b1, b2; write an expression
to check whether at least one of
them is true.
b1 || b2
Given an int[] array of size 3,
write a boolean expression
indicating whether exactly 2 values
in the array are even.
Boolean Expression Exercises
Requirement (Fall 2011 final exam)
Answer
boolean b1, b2; write an expression
to check whether at least one of
them is true.
b1 || b2
Given an int[] array of size 3,
write a boolean expression
indicating whether exactly 2 values
in the array are even. (harder)
array[0] % 2 + array[1] % 2
+ array[2] % 2 == 1
If-statement
•
•
•
•
•
•
if (condition) { … }
if (condition) { … } else { … }
if (condition1) { … } else if (condition2) { … }
else if (conditionN) { … }
else { … }
else, else if, are optional
Loops
• Repetition statements allow us to execute a block of
code or (set of) statement(s) multiple times
– These statements are normally called loops
• Very often in loops, one will do 3 things:
• 1)Perform some initialization before the loop starts.
• 2)Check a condition before an iteration of the loop
starts.
• 3)Perform some finalization at the end of each
iteration
While wrap-up (1)
Remember this simple formality: do something for n times
int counter = 0; //initialization
while (counter < n) //condition
{
//whatever you want done
counter++; //finalization at the end of each iteration
}
While loop wrap-up (2)
A counter is not always required. A clearly defined total
number of iterations may be unknown in advance.
Remember this simple formality: do something when true
while (boolean expression) //condition
{
//whatever you want to be done;
}
For loop wrap-up
Remember this simple formality:
for (initialization; condition; finalization)
{
//loop body
}
for loop vs while loop
for (initialization; condition;
finalization)
initialization
{
{
while (condition)
System.out.println(i);
System.out.println(i);
}
finalization
}
For-loop and while-loop are syntactically different
but functionally equivalent
22
for loop vs while loop
for (initialization; condition;
finalization)
initialization
{
{
while (condition)
System.out.println(i);
System.out.println(i);
}
finalization
}
For-loop and while-loop are syntactically different
but functionally equivalent
transform
for-loop
into
while-loop into
while-loop
for-loop
23
loop example
• for (int i = 1; i < 5; i++) {
System.out.print(i*2 + ", ");
}
System.out.println("");
counter value
value of condition
expression
other value at
each step
new counter value
after finalization
i=1
true
i*2 + “, “ = “2, “
2
i=2
true
i*2 + “, “ = “4, “
3
i=3
true
i*2 + “, “ = “6, “
4
i=4
true
i*2 + “, “ = “8, “
5
i=5
false
Array
• An array must be declared and initialized with
fixed length
– int[] array; array = {1, 2, 3} OR array = new int[3];
• array.length == largestIndexInArray + 1 !!
– char[] c = new char[10]; //c.length == 10
– c[9] exists, but c[10] doesn’t !!
– index always starts from 0 to array.length – 1
• Array is frequently used with loops to iterate
through elements
– for(int i = 0; i < array.length; i++) { //do anything }
ArrayList
• ArrayList is a dynamic array of reference types
• ArrayList<Type> is a dynamic array of
reference type “Type”
array
ArrayList<Type>
define
Type[] x;
ArrayList<Type> x;
initialize
x = new Type[10];
x = new ArrayList<Type>();
get element at index i
x[i]
x.get(i)
set element at index i
x[i] = y;
x.set(i,y); //add, etc. See API
size
x.length
x.size()
for loop
for(int i=0; i<x.length;i++)
{ x[i] = … }
for(int i=0; i<x.size();i++)
{ x.set(i,y); }
Basic OOP
• public class Point {
•
private int x_axis; //property
•
private int y_axis; //property
•
public void setX (int x) { x_axis = x; }//setter
•
public void setY (int y) { y_axis = y; }//setter
•
public int getX () { return x_axis; } //getter
•
public int getY () { return y_axis; } //getter
•
public Point (int x, int y) { //constructor
•
this.x_axis = x;
•
this.y_axis = y;
•
}
• }
Recursive Method
• A method that calls itself
• What type of problem is recursion good at?
– A simple base case (or cases), and
– A set of rules which reduce all other cases toward
the base case
• About “faith”: a new way of thinking:
if the following are true then the solution is correct:
1)You correctly solved a “little” version (base case) of the problem.
–2)Every “big” version of the problem can be written in terms of a
smaller version of the problem
–
line 1: int methodA (int n) {
line 2:
if (n < 0) {
//base case
line 3:
return n;
line 4:
} else {
line 5:
return methodA(n-1); //recursive call
line 6:
}
line 7: }
Block at Line5
A(n)
Block at Line5
Block at Line5
A(n)
A(n-1)
Block at Line5
A(n)
A(n-1)
Block at Line5
Block at Line5
A(n-2)
Block at Line5
A(n)
A(n-1)
Block at Line5
Block at Line5
A(n-2)
…
Block at Line5
A(0)
Block at Line5
A(n)
A(n-1)
Block at Line5
Block at Line5
A(n-2)
…
Block at Line5
A(0)
Base case line2
A(-1)
Block at Line5
A(n)
A(n-1)
Block at Line5
Block at Line5
A(n-2)
…
Block at Line5
A(0)
A(-1)
Block at Line5
A(n)
A(n-1)
Block at Line5
Block at Line5
A(n-2)
…
A(0)
A(-1)
Block at Line5
A(n)
A(n-1)
Block at Line5
Block at Line5
A(n-2)
…
A(0)
A(-1)
Block at Line5
Block at Line5
A(n)
A(n-1)
A(n-2)
…
A(0)
A(-1)
Block at Line5
A(n)
A(n-1)
A(n-2)
…
A(0)
A(-1)
Exceptions
• Java provides a nice way to handle exceptions
• try statement should contain
– at least one catch block or a finally block (at least one
or both of them)
– may have multiple catch blocks
• try {
//your code that might generate exceptions
} catch (Exception e) {
//your code to handle exceptions
} finally {
//wrapping up
}
Throw exceptions
• Your code can throw an exception
– throw new Exception(“new exception”);
• Your method indicates what exceptions might
be thrown by the method
– void myMethod (File f) throws IOException {…}
– void myMethod (File f) throws IOException,
IndexOutOfBoundsException{…}
Thanks for all your attention
Wish you all do well in the Final!
Download