comp-202 final review - cs.mcgill.ca€¦ · comp-202 final review melanie lyman-abramovitch april...
TRANSCRIPT
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 examperiod. 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 examon 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
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 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 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 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
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
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
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
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
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
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)?
I 532 = 26R1
I 262 = 13R0
I 132 = 6R1
I 62 = 3R0
I 32 = 1R1
I 12 = 0R1
COMP-202 Final Review
Decimal to Binary
What is 5310 in binary notation (base 2)?
I 532 = 26R1
I 262 = 13R0
I 132 = 6R1
I 62 = 3R0
I 32 = 1R1
I 12 = 0R1
COMP-202 Final Review
Decimal to Binary
What is 5310 in binary notation (base 2)?
I 532 = 26R1
I 262 = 13R0
I 132 = 6R1
I 62 = 3R0
I 32 = 1R1
I 12 = 0R1
COMP-202 Final Review
Decimal to Binary
What is 5310 in binary notation (base 2)?
I 532 = 26R1
I 262 = 13R0
I 132 = 6R1
I 62 = 3R0
I 32 = 1R1
I 12 = 0R1
COMP-202 Final Review
Decimal to Binary
What is 5310 in binary notation (base 2)?
I 532 = 26R1
I 262 = 13R0
I 132 = 6R1
I 62 = 3R0
I 32 = 1R1
I 12 = 0R1
COMP-202 Final Review
Decimal to Binary
What is 5310 in binary notation (base 2)?
I 532 = 26R1
I 262 = 13R0
I 132 = 6R1
I 62 = 3R0
I 32 = 1R1
I 12 = 0R1
COMP-202 Final Review
Decimal to Binary
What is 5310 in binary notation (base 2)?
I 532 = 26R1
I 262 = 13R0
I 132 = 6R1
I 62 = 3R0
I 32 = 1R1
I 12 = 0R1
COMP-202 Final Review
Decimal to Binary
What is 5310 in binary notation (base 2)?
I 532 = 26R 1
I 262 = 13R 0
I 132 = 6R 1
I 62 = 3R 0
I 32 = 1R 1
I 12 = 0R 1
I 5310 = 110101
I First check to see if 53 is odd or even.Note that any binary number endingin 0 is even and any number ending in1 is odd. 12 = 20 = 110.
I Now check to see if 26 (532 ) is evenlydivisible by 2.
I Continue until the quotient is 0
I The solution is the remainders frombottom to top
COMP-202 Final Review
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) thatbelongs to the class and not a particular object. Is the same forevery 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. Arraysare 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 arraysand linked lists. The create an ordered set and can be re-sized asyour code progresses.Anything you can do in an array or linked list, you can do with anArrayList
COMP-202 Final Review
Primitive types vs. reference types
The value of a reference type is an address. The value of aprimitive 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 twohorizontal 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 thesquare.
* * * ** ** ** * * *
COMP-202 Final Review
Search
I Linear Search
I The list does not have to be sorted.I Start from one end, and compare (in order) each element with
the target.
I Binary Search
I The list must already be sorted.I Start in the middle and compare. Use this information (< or
>) to eliminate half of the list. Go to the middle of thepossible half, and repeat.
COMP-202 Final Review
Search
I Linear SearchI The list does not have to be sorted.I Start from one end, and compare (in order) each element with
the target.
I Binary Search
I The list must already be sorted.I Start in the middle and compare. Use this information (< or
>) to eliminate half of the list. Go to the middle of thepossible half, and repeat.
COMP-202 Final Review
Search
I Linear SearchI The list does not have to be sorted.I Start from one end, and compare (in order) each element with
the target.
I Binary SearchI The list must already be sorted.I Start in the middle and compare. Use this information (< or
>) to eliminate half of the list. Go to the middle of thepossible half, and repeat.
COMP-202 Final Review
Sorting
I Bubble Sort
I From start to end, compare adjacent elements. Swap if out oforder. Repeat until no swaps are made.
I Cocktail Sort
I Bi-directional bubble sort.
I Comb Sort
I From start to end, compare elements that are gap distanceapart. Swap if out of order. Decrease gap by a shrink valueafter each pass. When gap get to 1, it acts like bubble sort.
COMP-202 Final Review
Sorting
I Bubble SortI From start to end, compare adjacent elements. Swap if out of
order. Repeat until no swaps are made.
I Cocktail Sort
I Bi-directional bubble sort.
I Comb Sort
I From start to end, compare elements that are gap distanceapart. Swap if out of order. Decrease gap by a shrink valueafter each pass. When gap get to 1, it acts like bubble sort.
COMP-202 Final Review
Sorting
I Bubble SortI From start to end, compare adjacent elements. Swap if out of
order. Repeat until no swaps are made.
I Cocktail SortI Bi-directional bubble sort.
I Comb Sort
I From start to end, compare elements that are gap distanceapart. Swap if out of order. Decrease gap by a shrink valueafter each pass. When gap get to 1, it acts like bubble sort.
COMP-202 Final Review
Sorting
I Bubble SortI From start to end, compare adjacent elements. Swap if out of
order. Repeat until no swaps are made.
I Cocktail SortI Bi-directional bubble sort.
I Comb SortI From start to end, compare elements that are gap distance
apart. Swap if out of order. Decrease gap by a shrink valueafter 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 isnot 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 notfound. The input must be sorted.
COMP-202 Final Review
Bubble Sort
Figure : Sorts an array from smallest to largest
COMP-202 Final Review