CS61B Midterm Review

23
CS61B Midterm Review Winston Liaw and Amir Kamil

description

CS61B Midterm Review. Winston Liaw and Amir Kamil. Agenda. Static vs. Dynamic Box and pointer diagrams Code Questions and examples Bit representation Algorithmic Analysis Access rights Quickie questions. Static vs. Dynamic. What happens? Assume we have defined: Homer h = new Homer(); - PowerPoint PPT Presentation

Transcript of CS61B Midterm Review

Page 1: CS61B Midterm Review

CS61B Midterm Review

Winston Liaw and Amir Kamil

Page 2: CS61B Midterm Review

Midterm Review 2

Agenda1. Static vs. Dynamic2. Box and pointer diagrams3. Code Questions and examples4. Bit representation5. Algorithmic Analysis6. Access rights7. Quickie questions

Page 3: CS61B Midterm Review

Midterm Review 3

Static vs. Dynamic What happens? Assume we have

defined:Homer h = new Homer();Bart b = new Bart();

Homer h1 = b;h1.talk2();Answer: Homer, Bart: dude

Page 4: CS61B Midterm Review

Midterm Review 4

Static vs. Dynamic cont. What happens? Assume the same

definitions

Bart b1 = b;b1.talk2();

Cartoon c1 = h;((Homer)c1).talk4();

Answer: Homer, Bart: dude

Answer: Homer, Homer: doh!

Page 5: CS61B Midterm Review

Midterm Review 5

Static vs. Dynamic cont. What happens?

Cartoon c2 = b;((Bart)c2).whoa();

Lumpy l2 = b;((Homer)l2).talk4();

Answer: dude

Answer: Bart: dude

Page 6: CS61B Midterm Review

Midterm Review 6

Static vs. Dynamic cont. For calls with an object of interest

(i.e. h.f()), static methods are called based on static type, non-static methods are based on dynamic type

For calls involving “this”, things get a little trickier. Static calls “stay” in the same class, dynamic calls are based on the dynamic set.

Page 7: CS61B Midterm Review

Midterm Review 7

Box and Pointer diagrams Draw the diagrams that result from

this code:

int [] x = new int[3];int [] y = new int[0];int [][] z = new int[3][2];

Page 8: CS61B Midterm Review

Midterm Review 8

Box and Pointer diagrams Draw the diagrams that result from this code:

int [] x = new int[3];int [] y = new int[0];int [][] z = new int[3][2];

0 0 00 0

0 0

0 0x

y

z

Page 9: CS61B Midterm Review

Midterm Review 9

Box and Pointer diagrams cont. Modify the box and pointer

diagram according to this code:

z[1] = z[2];

Page 10: CS61B Midterm Review

Midterm Review 10

Box and Pointer diagrams cont. Modify the box and pointer diagram according to this code:

z[1] = z[2];

0 0

0 0

0 0

z

Page 11: CS61B Midterm Review

Midterm Review 11

Coding Question Finish this method:

/* Given an IntList, it will reverse it destructively and return the new list */

public IntList reverse(IntList l) {…

}

Page 12: CS61B Midterm Review

Midterm Review 12

Coding Question Solutionpublic IntList reverse(IntList l) {

IntList prev = null; IntList next = l.tail; while (l != null) { next = l.tail; l.tail = prev; prev = l; l = next;} return prev; //once we are done reversing all the pointers //we need to set l's head to the new head

}

Page 13: CS61B Midterm Review

Midterm Review 13

Bit Representation What is the bit representation for:byte b = 15;

What is this value as a char?10110111

What about as a byte?

Answer: 00001111

Answer: 183

Answer: -73

Page 14: CS61B Midterm Review

Midterm Review 14

Bit Representation Two’s complement

If the Most Significant Bit (MSB) is 0, then treat the remaining bits as normal (as a positive number).

If the MSB is 1, flip the remaining bits, add 1, and that is your negative value.

Remember, two’s complement only applies to signed values. For an unsigned integer, for instance, treat it as “normal.”

Page 15: CS61B Midterm Review

Midterm Review 15

Modular Arithmetic For modular arithmetic:

Find out how many times your divisor can divide into your dividend. Remember, the remainder must be positive

If the remainder is greater than the range of your values (byte can have values btw –128 and 127 for instance) then loop value around

Page 16: CS61B Midterm Review

Midterm Review 16

Another Coding Question Finish these methods:

IntList pqueue;

/* removes the node with smallest value */ public int remove() {

… }

/* inserts the value into pqueue */public void insert(int k) {

…}

Page 17: CS61B Midterm Review

Midterm Review 17

Another Coding Question Solution

public int remove() {

int x = pqueue.head;pqueue = pqueue.tail;return x;

}

Page 18: CS61B Midterm Review

Midterm Review 18

Another Coding Question Solution cont.

public void insert(int k) {IntList temp = pqueue;IntList prev = null;while (temp != null) {if (k < temp.head) { if (prev != null) { prev.tail = new IntList(k, temp);

} else { pqueue = new IntList(k, pqueue); } return;

} else { prev = temp; temp = temp.tail;}

}

Page 19: CS61B Midterm Review

Midterm Review 19

Algorithmic Analysis Definition of Big-Oh

O(g(n)) = {f(n) : there exist positive constants c and n0 such that 0 <= f(n) <= cg(n) for all n >= n0

Definition of Big-Omega (g(n)) = {f(n) : there exist positive

constants c and n0 such that 0 <= cg(n) <= f(n) for all n >= n0

Page 20: CS61B Midterm Review

Midterm Review 20

Algorithmic Analysis One method takes O(n2), while

another takes O(n lg(n)). The O(n lg(n)) method is always preferred.

True or false?

Page 21: CS61B Midterm Review

Midterm Review 21

Algorithmic Analysis What are the running times (Big-Oh,

Big-Omega, Big-Theta) for this code?

for (int i = k; i < z; i++) { for (int j = 0; j < z; j++) {

//some lg (n) code here}

} Answer: all are (z-k)(z)lg(n)

Page 22: CS61B Midterm Review

Midterm Review 22

Access rights If you override a method in a child class,

Java allows you to change the access rights to be less restrictive

Ex. – Parent’s method is protectedChild’s method is public

Refer to page 113 in Programming into Java for more details

Page 23: CS61B Midterm Review

Midterm Review 23

Quickies What class has no superclass?

Why would you want to pick an array over a list?