lecture tuesday, 4/18 [note: sorry about the awful accoustics at the 11:15 lecture. i'll look into why the mic didn't work....] intro inheritance -- a powerful tool for code reuse to reduce redundancy example: class Face to draw a face -- outline, eyes, mouth. now, suppose wish to make modifications: frown, colored eyes, etc. goal: be able to make changes while reusing old, unchanged code + define new *subclass* that $extends$ old *superclass* + by default, *inherit* all of the old code unchanged from superclass + *override* old methods to change old behavior + add new methods and variables to add new functionality application: + make class Face "expose decisions" in the form of methods that can be overridden + define subclass Frown to override mouth method note: complete code will go to online examples class Face { void paint(Graphics g) { drawOutline(g); drawEyes(g); drawMouth(g); } void drawOutline(Graphics g) { ... } void drawEyes(Graphics g) { ... } void drawMouth(Graphics g) { ... } } class Frown extends Face { void drawMouth(Graphics g) { ... } // new implementation } class ColorFrown extends Face { Color c; // constructor ... void drawEyes(Graphics g) { Color save = g.getColor(); g.setColor(c); super.drawEyes(); // use original implementation g.setColor(save); } } partial critique: + if have 3 choices of mouths, 4 choices of eyes, 2 choices of outline, then this approach needs 3*4*2 = 24 classes, with *lots* of repeated code! + solution: Face = 1 Outline object + 2 Eye objects + 1 Mouth object; subclass Outline, Eye, Mouth -- therefore, have only about 3+4+2=7 classes. next lecture: making inheritance more precise, e.g. box/scope diagrams to explain scope. _______________________________________________________________________________ matlab need password -- don't distribute, but *do* try it out and let us know of any problems. username: cs100a (not case-sensitive) password: ***censored*** (case-sensitive) <-- given out at prelim 3 exercise 7, due thursday 4/20: + redo Project 1, Part 2 in matlab + hand in matlab session with both your code and resulting (interleaved) output why matlab? + cs100 = "intro to programming, not intro to java " -- to see that you learned general, universal programming principles, need to see another language + very useful for matrix and processing scientific data, e.g. linear algebra and plotting/graphics + matlab is slightly different way of thinking, but not too different, so not too hard to learn + matlab has pretty good online (built-in, not web) help + matlab can be much more concise than java, but not so much that it is unreadable (people joke that APL is "WORN", "write once, read never") + matlab is interactive: you can choose code as you see answers _______________________________________________________________________________ matlab notes as we go through examples: + $%$ is like $//$ in java: comment out rest of the line + no declarations -- just initialize and use variables + basic type is an array, which *is* a value -- there are no references + matrix is 2-D array (careful: 1-D *is* "sometimes" special case of 2-D) + vector is 1-D array; row vector versus column vector + scalar (number) is 1-by-1 array + matlab tends to operate on entire arrays + matlab prints out results unless you end the statement with $;$ + $rand$ returns random scalar; $rand(height,width)$ random matrix + WARNING: use $length$ for only vectors, not 2-D arrays + WARNING: matlab uses 1-relative indexing (java uses 0-relative) + $sum$: sum of row or col vector; column sums of matrix <-- argh, 1-d/2-d?! + since matlab uses $%$ for comments, it uses $rem$ for remainder + matlab uses 0 for false, 1 for true, so $sum$ counts number of $true$s + can easily extract out a whole sub-array, e.g. $a(1:n-1)$ + matlab uses $~=$ instead of $!=$ _______________________________________________________________________________ % create 1-d array a with 50 integers from -100..100 a = floor(rand(1,50) * 201) - 100 % plot a plot(a) % T1.Q1: RMS of 1-d array a sqrt( sum(a .^ 2) / length(a) ) % project 2.2: count multiples of 5 in 1-d array a sum(rem(a,5) == 0) % project 4.1: roll pair of 6-sided dice 1000 times; count 7s sum(7 == sum(1+floor(rand(2,1000) * 6))) % T1.Q2: number of days in random $month$ month = 1 + floor(rand * 12) % 1 2 3 4 5 6 7 8 9 10 11 31 days = [31 28 31 30 31 30 31 31 30 31 30 12]; days(month) % T1.Q3: max pair sum in 1-d array a n = length(a); max(a(1:n-1) + a(2:n)) % project 2.3: count sign changes in 1-d array a n = length(a); % $;$ supresses display of the result signs = sign(a); % sign of each element of a sum(signs(1:n-1) ~= signs(2:n)) % note: matlab uses 1-relative indices % project 2.4: find elevations with steepest gradient in 1-d array a [steepest,where] = max(abs(a(2:n) - a(1:n-1))); fprintf('steepest gradient %d between elevations %d and %d\n', ... steepest, a(where), a(where)+1) disp('steepest gradient'), disp(steepest) % etc.