lecture tuesday, 4/18 exercise 8: turn in to the appropriate pile [a-c, d-i, j-l, m-r, s-z] exercise 9 + your birthday or random day of the year; guess max number of duplicates + multiple choice questions on inheritance reading on inheritance: L&L 7.1-7.5 topics + matlab: logical arrays + matlab: random access + matlab: $sparse$ + java: inheritance ------------------------------------------------------------------------------- Matlab % note -- certain kinds of arrays are special and are not just numbers: % string = array of characters % logical array = array of true/false values s = 'hello', ischar(s), s+0, ischar(s+0) s == 'e', islogical(s == 'e'), islogical(s) % logical arrays are another way to index into arrays s = 'hello there, you''re not the slayer' % <-- '' means ' where = s == 'e'; char(where + '0') s (where) s (~where) s (where) = [] % delete all e's: assigning [] means "delete" vowels = s == 'a' | s == 'o' | s == 'u'; s(vowels) = upper(s(vowels)) % $FULL(SPARSE(I,J,S,M,N))$ is a useful way to count/accumulate: % think of throwing numbers into an M-by-N array that is initially 0; % if multiple numbers end up in the same box, then add them: % % if I, J, S are equal-sized vectors of length L, % then for K = 1:L, throw S(K) into the box at row I(K), column J(K) i = [2 3 2], j = [3 1 3], s = [7 8 4] full(sparse(i,j,s,3,3)) % start off: throw in throw in throw in % blank boxes are 0s i(1),j(1),s(1) i(2),j(2),s(2) i(3),j(3),s(3) % 2 3 7 3 1 8 2 3 4 % % 1 2 3 1 2 3 1 2 3 1 2 3 % +---+---+---+ +---+---+---+ +---+---+---+ +---+---+---+ % 1 | | | | 1 | | | | 1 | | | | 1 | | | | % +---+---+---+ +---+---+---+ +---+---+---+ +---+---+---+ % 2 | | | | 2 | | | 7 | 2 | | | 7 | 2 | | |11 | % +---+---+---+ +---+---+---+ +---+---+---+ +---+---+---+ % 3 | | | | 3 | | | | 3 | 8 | | | 3 | 8 | | | % +---+---+---+ +---+---+---+ +---+---+---+ +---+---+---+ % note: some of I, J, S may be scalars -- matlab will auto-extend to vectors j = [3 1 4 1 5 9 2 6 5 3 5 7 9 8 9 3 2 3 8] count = full(sparse(1,j,1, 1,9)) % it turns out COUNT contains only integers 1:4 1234 % suppose we want to "encrypt" COUNT as follows: AXYZ map = 'AXYZ' map(count) % see GETL or GETS for how to read lines of text from a file help getl help gets _______________________________________________________________________________ Java Inheritance Here are three important Java Principles: + Safe Laziness: Java will do its best to *safely* let you be lazy. + Ex: automatic casting from $int$ to $double$, but not vice versa (unsafe)! + Ex: automatic casting to $String$ for concatenation + Ex: initializer lists as shorthand for anonymous arrays in initialization + Object: Every class is a subclass of class Object. + Constructor: Every class must define a constructor. + Super-Constructor: Every constructor for a subclass must call $super(...)$. Here are two Corollaries to these Principles and resulting Pitfalls: + Constructor + Laziness: Java defines a default constructor if you don't define one. - Pitfall: *you* have to define a "nullary" constructor if you define any other constructors. ("Nullary" here means "takes no parameters" and has nothing to do with $null$.) + Super-Constructor + Laziness: Java calls $super()$ if you don't call $super(...)$. - Pitfall: often the nullary constructor $super()$ doesn't exist: + *you* should call the correct constructor from the superclass + *you* should define a nullary constructor for the superclass if you want one _______________________________________________________________________________ Scope Rules: + "specific" = (defined explicitly at) *that* precise class, not its superclass or subclass. + $public$: not hidden from anyone + $protected$: not hidden from subclasses, but hidden from everyone else + $private$: hidden from subclasses and every other class + A class sees: + Its specific static methods and variables. + The $public$ and $protected$ things its superclasses see. + An object sees: + Its specific instance methods and variables. + The $public$ and $protected$ instance methods and variables from its superclasses. + Whatever its specific class sees. + An activation record of a method sees: + Its parameters and local variables. + (Instance methods): the instance variables and methods that an instance of its specific class sees. + (Static methods): whatever its specific class sees. + To find the implemention of a method: + Start at the (most precise) class containing the object + Keep searching until an implementation is found. _______________________________________________________________________________ [see code posted for 4/18 lecture for complete code] public class Face { // Face(){super();} auto-defined by Laziness+Constructor+Super-Constructor public void drawEyes(Graphics g) { ... } public void drawMouth(Graphics g) { ... } public void paint(Graphics g) { g.drawOval(0,0,100,100); drawEyes(g); drawMouth(g); } } public class Frown extends Face { // Frown(){super();} auto-defined by Laziness+Constructor+Super-Constructor public void drawMouth(Graphics g) { ... } } public class ColoredFrown extends Frown { Color eye; ColoredFrown(Color c) { // super(); auto-called by Laziness + Super-Constructor eyeColor = c; } public void drawEyes(Graphics g) { Color save = g.getColor(); g.setColor(eye); super.drawEyes(g); g.setColor(save); } } public class Oops extends ColoredFrown { // Oops() { super(); } auto-defined, BUT ColoredFrown() DOES NOT EXIST! } notes: + Face[] f = new Face[] {new Face(), new Frown(), new ColoredFrown(Color.red)}; + can $paint$ each one box/scope diagram: + show $extends$ as special kind of arrow from subclass to superclass /---------\ ------| extends |------> \---------/