20
Public-Service Announcement “Cal Actuarial League will be hosting their First General Meet- ing on Tuesday, September 5th at 8 pm in [TBD] and Actuarial Career Panel on Thursday, September 7th at 8 pm in [TBD]. See Facebook for location updates. Free food and refreshments will be provided. An actuary conducts mathematical and statistical analysis along- side data science techniques to estimate financial risks. The ac- tuarial career is consistently ranked as one of the best jobs. For those of you who are looking for a challenging and rewarding ca- reer with a remarkable social reputation, becoming an actuary would be a great choice. A panel of professionals at our Actu- arial Career Panel will share their experiences and answer your questions.” Recreation Prove that (2 + 3) n is odd for all integer n 0. [Source: D. O. Shklarsky, N. N. Chentzov, I. M. Yaglom, The USSR Olympiad Problem Book, Dover ed. (1993), from the W. H. Freeman edition, 1962.] Last modified: Tue Aug 29 16:20:03 2017 CS61B: Lecture #4 1

Public-Service Announcementcs61b/fa17/materials/lectures/lect4.pdf · Public-Service Announcement “Cal Actuarial League will be hosting their First General Meet-ing on Tuesday,

Embed Size (px)

Citation preview

Page 1: Public-Service Announcementcs61b/fa17/materials/lectures/lect4.pdf · Public-Service Announcement “Cal Actuarial League will be hosting their First General Meet-ing on Tuesday,

Public-Service Announcement

“Cal Actuarial League will be hosting their First General Meet-ing on Tuesday, September 5th at 8 pm in [TBD] and ActuarialCareer Panel on Thursday, September 7th at 8 pm in [TBD]. SeeFacebook for location updates. Free food and refreshments willbe provided.An actuary conducts mathematical and statistical analysis along-side data science techniques to estimate financial risks. The ac-tuarial career is consistently ranked as one of the best jobs. Forthose of you who are looking for a challenging and rewarding ca-reer with a remarkable social reputation, becoming an actuarywould be a great choice. A panel of professionals at our Actu-arial Career Panel will share their experiences and answer yourquestions.”

Recreation

Prove that ⌊(2 +√3)n⌋ is odd for all integer n ≥ 0.

[Source: D. O. Shklarsky, N. N. Chentzov, I. M. Yaglom, The USSR Olympiad Problem

Book, Dover ed. (1993), from the W. H. Freeman edition, 1962.]

Last modified: Tue Aug 29 16:20:03 2017 CS61B: Lecture #4 1

Page 2: Public-Service Announcementcs61b/fa17/materials/lectures/lect4.pdf · Public-Service Announcement “Cal Actuarial League will be hosting their First General Meet-ing on Tuesday,

CS61B Lecture #4: Values and Containers

• Labs are normally due at midnight Friday.

• Today. Simple classes. Scheme-like lists. Destructive vs. non-destructive operations. Models of memory.

Last modified: Tue Aug 29 16:20:03 2017 CS61B: Lecture #4 2

Page 3: Public-Service Announcementcs61b/fa17/materials/lectures/lect4.pdf · Public-Service Announcement “Cal Actuarial League will be hosting their First General Meet-ing on Tuesday,

Values and Containers

• Values are numbers, booleans, and pointers. Values never change.

3 ’a’ true

• Simple containers contain values:

3x: L: p:

Examples: variables, fields, individual array elements, parameters.

Last modified: Tue Aug 29 16:20:03 2017 CS61B: Lecture #4 3

Page 4: Public-Service Announcementcs61b/fa17/materials/lectures/lect4.pdf · Public-Service Announcement “Cal Actuarial League will be hosting their First General Meet-ing on Tuesday,

Structured Containers

Structured containers contain (0 or more) other containers:

3

h t

3h:

t:

42

0

171

92

420

171

92

Class Object Array Object Empty Object

AlternativeNotation

Last modified: Tue Aug 29 16:20:03 2017 CS61B: Lecture #4 4

Page 5: Public-Service Announcementcs61b/fa17/materials/lectures/lect4.pdf · Public-Service Announcement “Cal Actuarial League will be hosting their First General Meet-ing on Tuesday,

Pointers

• Pointers (or references) are values that reference (point to) con-tainers.

• One particular pointer, called null, points to nothing.

• In Java, structured containers contain only simple containers, butpointers allow us to build arbitrarily big or complex structures any-way.

0 1

30

91

170

Last modified: Tue Aug 29 16:20:03 2017 CS61B: Lecture #4 5

Page 6: Public-Service Announcementcs61b/fa17/materials/lectures/lect4.pdf · Public-Service Announcement “Cal Actuarial League will be hosting their First General Meet-ing on Tuesday,

Containers in Java

• Containers may be named or anonymous.

• In Java, all simple containers are named, all structured contain-ers are anonymous, and pointers point only to structured containers.(Therefore, structured containers contain only simple containers).

p: 3

h t

7

h t

simple container(local variable)

structured containers(anonymous)

named simple containers (fields)within structured containers

• In Java, assignment copies values into simple containers.

• Exactly like Scheme and Python!

• (Python also has slice assignment, as in x[3:7]=..., which is short-hand for something else entirely.)

Last modified: Tue Aug 29 16:20:03 2017 CS61B: Lecture #4 6

Page 7: Public-Service Announcementcs61b/fa17/materials/lectures/lect4.pdf · Public-Service Announcement “Cal Actuarial League will be hosting their First General Meet-ing on Tuesday,

Defining New Types of Object

• Class declarations introduce new types of objects.

• Example: list of integers:

public class IntList {// Constructor function (used to initialize new object)

/** List cell containing (HEAD, TAIL). */

public IntList(int head, IntList tail) {this.head = head; this.tail = tail;

}

// Names of simple containers (fields)// WARNING: public instance variables usually bad style!

public int head;

public IntList tail;

}

Last modified: Tue Aug 29 16:20:03 2017 CS61B: Lecture #4 7

Page 8: Public-Service Announcementcs61b/fa17/materials/lectures/lect4.pdf · Public-Service Announcement “Cal Actuarial League will be hosting their First General Meet-ing on Tuesday,

Primitive Operations

IntList Q, L;L:

Q:

L = new IntList(3, null);

Q = L;

L:

Q:

3

Q = new IntList(42, null);

L.tail = Q;

L:

Q:

3 42

L.tail.head += 1;

// Now Q.head == 43

// and L.tail.head == 43

L:

Q:

3 43

Last modified: Tue Aug 29 16:20:03 2017 CS61B: Lecture #4 8

Page 9: Public-Service Announcementcs61b/fa17/materials/lectures/lect4.pdf · Public-Service Announcement “Cal Actuarial League will be hosting their First General Meet-ing on Tuesday,

Side Excursion: Another Way to View Pointers

• Some folks find the idea of “copying an arrow” somewhat odd.

• Alternative view: think of a pointer as a label , like a street address.

• Each object has a permanent label on it, like the address plaque ona house.

• Then a variable containing a pointer is like a scrap of paper with astreet address written on it.

• One view:

last:

result: 5 45

• Alternative view:

#7last:

#7result: 5 #37

453

Last modified: Tue Aug 29 16:20:03 2017 CS61B: Lecture #4 9

Page 10: Public-Service Announcementcs61b/fa17/materials/lectures/lect4.pdf · Public-Service Announcement “Cal Actuarial League will be hosting their First General Meet-ing on Tuesday,

Another Way to View Pointers (II)

• Assigning a pointer to a variable looks just like assigning an integerto a variable.

• So, after executing “last = last.tail;” we have

last:

result: 5 45

• Alternative view:

#3last:

#7result: 5 #37

453

• Under alternative view, you might be less inclined to think that as-signment would change object #7 itself, rather than just “last”.

• BEWARE! Internally, pointers really are just numbers, but Javatreats them as more than that: they have types, and you can’t justchange integers into pointers.

Last modified: Tue Aug 29 16:20:03 2017 CS61B: Lecture #4 10

Page 11: Public-Service Announcementcs61b/fa17/materials/lectures/lect4.pdf · Public-Service Announcement “Cal Actuarial League will be hosting their First General Meet-ing on Tuesday,

Destructive vs. Non-destructive

Problem: Given a (pointer to a) list of integers, L, and an integer in-crement n, return a list created by incrementing all elements of the listby n.

/** List of all items in P incremented by n. Does not modify

* existing IntLists. */

static IntList incrList(IntList P, int n) {return /*( P, with each element incremented by n )*/

}We say incrList is non-destructive, because it leaves the input objectsunchanged, as shown on the left. A destructive method may modify theinput objects, so that the original data is no longer available, as shownon the right:

L:

Q:

3 43

5 45

After Q = incrList(L, 2):

L:

Q:

5 45

After Q = dincrList(L, 2) (destructive):

Last modified: Tue Aug 29 16:20:03 2017 CS61B: Lecture #4 11

Page 12: Public-Service Announcementcs61b/fa17/materials/lectures/lect4.pdf · Public-Service Announcement “Cal Actuarial League will be hosting their First General Meet-ing on Tuesday,

Nondestructive IncrList: Recursive

/** List of all items in P incremented by n. */

static IntList incrList(IntList P, int n) {if (P == null)

return null;

else return new IntList(P.head+n, incrList(P.tail, n));

}• Why does incrList have to return its result, rather than just set-ting P?

• In the call incrList(P, 2), where P contains 3 and 43, which IntListobject gets created first?

Last modified: Tue Aug 29 16:20:03 2017 CS61B: Lecture #4 12

Page 13: Public-Service Announcementcs61b/fa17/materials/lectures/lect4.pdf · Public-Service Announcement “Cal Actuarial League will be hosting their First General Meet-ing on Tuesday,

An Iterative Version

An iterative incrList is tricky, because it is not tail recursive.Easier to build things first-to-last, unlike recursive version:

static IntList incrList(IntList P, int n) {if (P == null) <<<return null;

IntList result, last;

result = last

= new IntList(P.head+n, null);

while (P.tail != null) {P = P.tail;

last.tail

= new IntList(P.head+n, null);

last = last.tail;

}return result;

}

+

+P: 3 43 56

Last modified: Tue Aug 29 16:20:03 2017 CS61B: Lecture #4 13

Page 14: Public-Service Announcementcs61b/fa17/materials/lectures/lect4.pdf · Public-Service Announcement “Cal Actuarial League will be hosting their First General Meet-ing on Tuesday,

An Iterative Version

An iterative incrList is tricky, because it is not tail recursive.Easier to build things first-to-last, unlike recursive version:

static IntList incrList(IntList P, int n) {if (P == null)

return null;

IntList result, last;

result = last <<<= new IntList(P.head+n, null);

while (P.tail != null) {P = P.tail;

last.tail

= new IntList(P.head+n, null);

last = last.tail;

}return result;

}

+

+P: 3 43 56

last:

result: 5

Last modified: Tue Aug 29 16:20:03 2017 CS61B: Lecture #4 14

Page 15: Public-Service Announcementcs61b/fa17/materials/lectures/lect4.pdf · Public-Service Announcement “Cal Actuarial League will be hosting their First General Meet-ing on Tuesday,

An Iterative Version

An iterative incrList is tricky, because it is not tail recursive.Easier to build things first-to-last, unlike recursive version:

static IntList incrList(IntList P, int n) {if (P == null)

return null;

IntList result, last;

result = last

= new IntList(P.head+n, null);

while (P.tail != null) {P = P.tail; <<<last.tail

= new IntList(P.head+n, null);

last = last.tail;

}return result;

}

+

+P: 3 43 56

last:

result: 5

Last modified: Tue Aug 29 16:20:03 2017 CS61B: Lecture #4 15

Page 16: Public-Service Announcementcs61b/fa17/materials/lectures/lect4.pdf · Public-Service Announcement “Cal Actuarial League will be hosting their First General Meet-ing on Tuesday,

An Iterative Version

An iterative incrList is tricky, because it is not tail recursive.Easier to build things first-to-last, unlike recursive version:

static IntList incrList(IntList P, int n) {if (P == null)

return null;

IntList result, last;

result = last

= new IntList(P.head+n, null);

while (P.tail != null) {P = P.tail;

last.tail <<<= new IntList(P.head+n, null);

last = last.tail;

}return result;

}

+

+P: 3 43 56

last:

result: 5 45

Last modified: Tue Aug 29 16:20:03 2017 CS61B: Lecture #4 16

Page 17: Public-Service Announcementcs61b/fa17/materials/lectures/lect4.pdf · Public-Service Announcement “Cal Actuarial League will be hosting their First General Meet-ing on Tuesday,

An Iterative Version

An iterative incrList is tricky, because it is not tail recursive.Easier to build things first-to-last, unlike recursive version:

static IntList incrList(IntList P, int n) {if (P == null)

return null;

IntList result, last;

result = last

= new IntList(P.head+n, null);

while (P.tail != null) {P = P.tail;

last.tail

= new IntList(P.head+n, null);

last = last.tail; <<<}return result;

}

+

+P: 3 43 56

last:

result: 5 45

Last modified: Tue Aug 29 16:20:03 2017 CS61B: Lecture #4 17

Page 18: Public-Service Announcementcs61b/fa17/materials/lectures/lect4.pdf · Public-Service Announcement “Cal Actuarial League will be hosting their First General Meet-ing on Tuesday,

An Iterative Version

An iterative incrList is tricky, because it is not tail recursive.Easier to build things first-to-last, unlike recursive version:

static IntList incrList(IntList P, int n) {if (P == null)

return null;

IntList result, last;

result = last

= new IntList(P.head+n, null);

while (P.tail != null) {P = P.tail; <<<last.tail

= new IntList(P.head+n, null);

last = last.tail;

}return result;

}

+

+P: 3 43 56

last:

result: 5 45

Last modified: Tue Aug 29 16:20:03 2017 CS61B: Lecture #4 18

Page 19: Public-Service Announcementcs61b/fa17/materials/lectures/lect4.pdf · Public-Service Announcement “Cal Actuarial League will be hosting their First General Meet-ing on Tuesday,

An Iterative Version

An iterative incrList is tricky, because it is not tail recursive.Easier to build things first-to-last, unlike recursive version:

static IntList incrList(IntList P, int n) {if (P == null)

return null;

IntList result, last;

result = last

= new IntList(P.head+n, null);

while (P.tail != null) {P = P.tail;

last.tail <<<= new IntList(P.head+n, null);

last = last.tail;

}return result;

}

+

+P: 3 43 56

last:

result: 5 45 58

Last modified: Tue Aug 29 16:20:03 2017 CS61B: Lecture #4 19

Page 20: Public-Service Announcementcs61b/fa17/materials/lectures/lect4.pdf · Public-Service Announcement “Cal Actuarial League will be hosting their First General Meet-ing on Tuesday,

An Iterative Version

An iterative incrList is tricky, because it is not tail recursive.Easier to build things first-to-last, unlike recursive version:

static IntList incrList(IntList P, int n) {if (P == null)

return null;

IntList result, last;

result = last

= new IntList(P.head+n, null);

while (P.tail != null) {P = P.tail;

last.tail

= new IntList(P.head+n, null);

last = last.tail; <<<}return result;

}

+

+P: 3 43 56

last:

result: 5 45 58

Last modified: Tue Aug 29 16:20:03 2017 CS61B: Lecture #4 20