COMP-202 Final Review
Melanie Lyman-Abramovitch
April 13, 2015
COMP-202 Final Review
Admin
I
There will be office hours and tutorials held during exam
period. They will likely not be at the usual times or places.
Check myCourses for updates.
I
There will be a giant Q & A session the day before the exam
on April 28 from 6-9pm. At least two TA’s will be there.
(Location: TR 3120)
I
This is the last class.
COMP-202 Final Review
Today’s Topics
I
Binary
I
Objects and classes
I
Arrays, array lists
I
Reference vs primitive types
I
Drawing shapes
I
Search
I
Sort
COMP-202 Final Review
Base 10 - Decimal
I
5 = 5 × 100
COMP-202 Final Review
Base 10 - Decimal
I
5 = 5 × 100
I
25 = 2 × 101 + 5 × 100
COMP-202 Final Review
Base 10 - Decimal
I
5 = 5 × 100
I
25 = 2 × 101 + 5 × 100
I
425 = 4 × 102 + 2 × 101 + 5 × 100
COMP-202 Final Review
Base 10 - Decimal
I
5 = 5 × 100
I
25 = 2 × 101 + 5 × 100
I
425 = 4 × 102 + 2 × 101 + 5 × 100
I
3425 = 3 × 103 + 4 × 102 + 2 × 101 + 5 × 100
COMP-202 Final Review
Base 2 - Binary
I
0 = 0 × 20
COMP-202 Final Review
Base 2 - Binary
I
0 = 0 × 20
I
10 = 1 × 21 + 0 × 20
COMP-202 Final Review
Base 2 - Binary
I
0 = 0 × 20
I
10 = 1 × 21 + 0 × 20
I
110 = 1 × 22 + 1 × 21 + 0 × 20
COMP-202 Final Review
Base 2 - Binary
I
0 = 0 × 20
I
10 = 1 × 21 + 0 × 20
I
110 = 1 × 22 + 1 × 21 + 0 × 20
I
1110 = 1 × 23 + 1 × 22 + 1 × 21 + 0 × 20
COMP-202 Final Review
Base 2 - Binary
I
0 = 0 × 20
I
10 = 1 × 21 + 0 × 20
I
110 = 1 × 22 + 1 × 21 + 0 × 20
I
1110 = 1 × 23 + 1 × 22 + 1 × 21 + 0 × 20
I
01110 = 0 × 24 + 1 × 23 + 1 × 22 + 1 × 21 + 0 × 20
COMP-202 Final Review
Base 2 - Binary
I
0 = 0 × 20
I
10 = 1 × 21 + 0 × 20
I
110 = 1 × 22 + 1 × 21 + 0 × 20
I
1110 = 1 × 23 + 1 × 22 + 1 × 21 + 0 × 20
I
01110 = 0 × 24 + 1 × 23 + 1 × 22 + 1 × 21 + 0 × 20
I
101110 = 1 × 25 + 0 × 24 + 1 × 23 + 1 × 22 + 1 × 21 + 0 × 20
COMP-202 Final Review
Decimal to Binary
What is 5310 in binary notation (base 2)?
COMP-202 Final Review
Decimal to Binary
What is 5310 in binary notation (base 2)?
I 53
2
= 26R1
COMP-202 Final Review
Decimal to Binary
What is 5310 in binary notation (base 2)?
I 53
2
I 26
2
= 26R1
= 13R0
COMP-202 Final Review
Decimal to Binary
What is 5310 in binary notation (base 2)?
I 53
2
I 26
2
I 13
2
= 26R1
= 13R0
= 6R1
COMP-202 Final Review
Decimal to Binary
What is 5310 in binary notation (base 2)?
I 53
2
I 26
2
I 13
2
I 6
2
= 26R1
= 13R0
= 6R1
= 3R0
COMP-202 Final Review
Decimal to Binary
What is 5310 in binary notation (base 2)?
I 53
2
I 26
2
I 13
2
I 6
2
I 3
2
= 26R1
= 13R0
= 6R1
= 3R0
= 1R1
COMP-202 Final Review
Decimal to Binary
What is 5310 in binary notation (base 2)?
I 53
2
I 26
2
I 13
2
I 6
2
I 3
2
I 1
2
= 26R1
= 13R0
= 6R1
= 3R0
= 1R1
= 0R1
COMP-202 Final Review
Decimal to Binary
What is 5310 in binary notation (base 2)?
I First check to see if 53 is odd or even.
I 53 = 26R 1
2
Note that any binary number ending
I 26 = 13R 0
in 0 is even and any number ending in
2
13
1 is odd. 12 = 20 = 110 .
I
= 6R 1
2
6
I
2 = 3R 0
I 3 = 1R 1
2
I 1 = 0R 1
2
I 5310 = 110101
COMP-202 Final Review
I
Now check to see if 26 ( 53
2 ) is evenly
divisible by 2.
I
Continue until the quotient is 0
I
The solution is the remainders from
bottom to top
The this keyword
Refers to the current object instance.
Test class:
Vector v1 = new Vector(1,2);
Vector v2 = new Vector(3,4);
Vector v3 = v1.add(v2);
Vector class:
public Vector add(Vector v) {
Vector u = new Vector(this.x + v.x, this.y+v.y);
return u;
}
In the example, this refers to the Vector that called the method,
v1.
COMP-202 Final Review
The static keyword
The keyword static refers to a property (attribute, method) that
belongs to the class and not a particular object. Is the same for
every instantiated object you create of that class.
COMP-202 Final Review
Data Structures
I
Arrays
I
ArrayLists
COMP-202 Final Review
Data Structures - Arrays
Arrays are an ordered collection of items of the same data type.
Once the size of the array is chosen, it cannot be changed. Arrays
are reference types. Array indices start counting at zero.
COMP-202 Final Review
Data Structures - ArrayLists
ArrayLists are a data structure in Java. They are a hybrid of arrays
and linked lists. The create an ordered set and can be re-sized as
your code progresses.
Anything you can do in an array or linked list, you can do with an
ArrayList
COMP-202 Final Review
Primitive types vs. reference types
The value of a reference type is an address. The value of a
primitive type is not.
I
Primitive types: int, double, long, char, etc
I
Reference types: Object, array, ArrayList, (String), etc
COMP-202 Final Review
Pass-by-value
public static int method(int a) {
a = a + 10;
return a;
}
public static void main(String[] args){
int a = 25;
int b = method(a);
System.out.println(a + ” ” + b);
/ / What prints?
}
COMP-202 Final Review
Pass-by-reference
public static void method(int[] a) {
a[0] = a[0] + 10;
a[1] = a[1] + 2;
}
public static void main(String[] args){
int[] vals = 4,3,2;
method(vals);
System.out.println(Arrays.toString(vals));
/ / What prints?
}
COMP-202 Final Review
Drawing Shapes - Square
Write a method that draws a coloured-in square of side length x.
For example, if x=4, it should draw:
*
*
*
*
COMP-202 Final Review
*
*
*
*
*
*
*
*
*
*
*
*
Drawing Shapes - Square
Modify the method made previously so that it draws only the two
horizontal bars from the outline. For example, if x is 4
****
****
COMP-202 Final Review
Drawing Shapes - Square
Finally, modify the code so that it draws just the outline of the
square.
***
*
*
***
COMP-202 Final Review
*
*
*
*
Search
I
Linear Search
I
Binary Search
COMP-202 Final Review
Search
I
Linear Search
I
I
I
The list does not have to be sorted.
Start from one end, and compare (in order) each element with
the target.
Binary Search
COMP-202 Final Review
Search
I
Linear Search
I
I
I
The list does not have to be sorted.
Start from one end, and compare (in order) each element with
the target.
Binary Search
I
I
The list must already be sorted.
Start in the middle and compare. Use this information (< or
>) to eliminate half of the list. Go to the middle of the
possible half, and repeat.
COMP-202 Final Review
Sorting
I
Bubble Sort
I
Cocktail Sort
I
Comb Sort
COMP-202 Final Review
Sorting
I
Bubble Sort
I
From start to end, compare adjacent elements. Swap if out of
order. Repeat until no swaps are made.
I
Cocktail Sort
I
Comb Sort
COMP-202 Final Review
Sorting
I
Bubble Sort
I
I
Cocktail Sort
I
I
From start to end, compare adjacent elements. Swap if out of
order. Repeat until no swaps are made.
Bi-directional bubble sort.
Comb Sort
COMP-202 Final Review
Sorting
I
Bubble Sort
I
I
Cocktail Sort
I
I
From start to end, compare adjacent elements. Swap if out of
order. Repeat until no swaps are made.
Bi-directional bubble sort.
Comb Sort
I
From start to end, compare elements that are gap distance
apart. Swap if out of order. Decrease gap by a shrink value
after each pass. When gap get to 1, it acts like bubble sort.
COMP-202 Final Review
Questions
Any questions?
COMP-202 Final Review
Linear Search
Figure : Returns the first index of target element, or -1 if the element is
not found. The input does not have to be sorted.
COMP-202 Final Review
Binary Search
Figure : Returns an index of target element, or -1 if the element is not
found. The input must be sorted.
COMP-202 Final Review
Bubble Sort
Figure : Sorts an array from smallest to largest
COMP-202 Final Review
© Copyright 2026 Paperzz