CS/ENGRD 2110 FALL 2015 Lecture 5: Local vars; Inside-out rule; constructors 1.
-
Upload
baldwin-henry -
Category
Documents
-
view
215 -
download
0
Transcript of CS/ENGRD 2110 FALL 2015 Lecture 5: Local vars; Inside-out rule; constructors 1.
![Page 1: CS/ENGRD 2110 FALL 2015 Lecture 5: Local vars; Inside-out rule; constructors 1.](https://reader035.fdocuments.net/reader035/viewer/2022062806/5697bf7d1a28abf838c84729/html5/thumbnails/1.jpg)
1
CS/ENGRD 2110FALL 2015Lecture 5: Local vars; Inside-out rule; constructors
http://courses.cs.cornell.edu/cs2110
![Page 2: CS/ENGRD 2110 FALL 2015 Lecture 5: Local vars; Inside-out rule; constructors 1.](https://reader035.fdocuments.net/reader035/viewer/2022062806/5697bf7d1a28abf838c84729/html5/thumbnails/2.jpg)
2
References to text and JavaSummary.pptx
Local variable: variable declared in a method body
B.10–B.11 slide 45 Inside-out rule, bottom-up/overriding rule C.15
slide 31-32and consequences thereof slide 45
Use of this B.10 slide 23-24 and super C.15 slide 28, 33
Constructors in a subclass C.9–C.10 slide 24-29 First statement of a constructor body must be a
call on another constructor —if not Java puts in super(); C.10 slide 29
![Page 3: CS/ENGRD 2110 FALL 2015 Lecture 5: Local vars; Inside-out rule; constructors 1.](https://reader035.fdocuments.net/reader035/viewer/2022062806/5697bf7d1a28abf838c84729/html5/thumbnails/3.jpg)
3
Homework
Visit course website, click on Resources and then on Code Style Guidelines. Study
4.2 Keep methods short
4.3 Use statement-comments …
4.4 Use returns to simplify method structure
4.6 Declare local variables close to first use …
![Page 4: CS/ENGRD 2110 FALL 2015 Lecture 5: Local vars; Inside-out rule; constructors 1.](https://reader035.fdocuments.net/reader035/viewer/2022062806/5697bf7d1a28abf838c84729/html5/thumbnails/4.jpg)
4
Local variables
/** Return middle value of b, c, d (no ordering assumed) */public static int middle(int b, int c, int d) { if (b > c) { int temp= b; b= c; c= temp; } // { b <= c } if (d <= b) { return b; } // { b < d and b <= c } return Math.min(c, d); }
Parameter: variable declared in () of
method header
middle(8, 6, 7)
b 8 d 7c 6
Local variable: variable
declared in method body
temp ?
All parameters and local variables are created when a call is executed, before the method body is executed. They are destroyed when method body terminates.
![Page 5: CS/ENGRD 2110 FALL 2015 Lecture 5: Local vars; Inside-out rule; constructors 1.](https://reader035.fdocuments.net/reader035/viewer/2022062806/5697bf7d1a28abf838c84729/html5/thumbnails/5.jpg)
5
Scope of local variables
/** Return middle value of b, c, d (no ordering assumed) */public static int middle(int b, int c, int d) { if (b > c) { int temp= b; b= c; c= temp; } // { b <= c } if (d <= b) { return b; } // { b < d and b <= c } return Math.min(c, d); }
Scope of local variable (where it can be used): from its declaration to the end of the block in which it is declared.
block
![Page 6: CS/ENGRD 2110 FALL 2015 Lecture 5: Local vars; Inside-out rule; constructors 1.](https://reader035.fdocuments.net/reader035/viewer/2022062806/5697bf7d1a28abf838c84729/html5/thumbnails/6.jpg)
6
Principle: declaration placement
/** Return middle value of b, c, d (no ordering assumed) */public static int middle(int b, int c, int d) { int temp; if (b > c) { temp= b; b= c; c= temp; } // { b <= c } if (d <= b) { return b; } // { b < d and b <= c } return Math.min(c, d); }
Principle: Declare a local variable as close to its first use as possible.
Not good! No need for reader to know about temp except when reading the then-part of the if- statement
![Page 7: CS/ENGRD 2110 FALL 2015 Lecture 5: Local vars; Inside-out rule; constructors 1.](https://reader035.fdocuments.net/reader035/viewer/2022062806/5697bf7d1a28abf838c84729/html5/thumbnails/7.jpg)
7
Assertions promote understanding
/** Return middle value of b, c, d (no ordering assumed) */public static int middle(int b, int c, int d) { if (b > c) { int temp= b; b= c; c= temp; } // { b <= c } if (d <= b) { return b; } // { b < d and b <= c } return Math.min(c, d); }
Assertion: Asserting that b <= c at this point. Helps reader understand code below.
![Page 8: CS/ENGRD 2110 FALL 2015 Lecture 5: Local vars; Inside-out rule; constructors 1.](https://reader035.fdocuments.net/reader035/viewer/2022062806/5697bf7d1a28abf838c84729/html5/thumbnails/8.jpg)
8
Bottom-up/overriding rule
toString() { … }
Object
PhD@20
PhD
toString()
name “Beaut”
c PhD@20Which method toString() is called by
c.toString() ?
Overriding rule orbottom-up rule:To find out which is used, start at the bottom of the object and search upward until a matching one is found.
![Page 9: CS/ENGRD 2110 FALL 2015 Lecture 5: Local vars; Inside-out rule; constructors 1.](https://reader035.fdocuments.net/reader035/viewer/2022062806/5697bf7d1a28abf838c84729/html5/thumbnails/9.jpg)
9
Inside-out rule
Inside-out rule: Code in a construct can reference names declared in that construct, as well as names that appear in enclosing constructs. (If name is declared twice, the closer one prevails.)
Person
Person@a0
n
getNAndPop() { return n + PersonPop;}
Person
Person@a1
n
getNAndPop() { return n + PersonPop;}
Person’s objects and static components
PersonPop
![Page 10: CS/ENGRD 2110 FALL 2015 Lecture 5: Local vars; Inside-out rule; constructors 1.](https://reader035.fdocuments.net/reader035/viewer/2022062806/5697bf7d1a28abf838c84729/html5/thumbnails/10.jpg)
Parameters participate in inside-out rule
10
setN(String name) {
n= name;}
Person
Person@a0
n
setN(String n) { n= n;}
Person
Person@a0
Parameter n “blocks” reference to field n.(n is a “shadowed” variable)
Doesn’t work right
n
![Page 11: CS/ENGRD 2110 FALL 2015 Lecture 5: Local vars; Inside-out rule; constructors 1.](https://reader035.fdocuments.net/reader035/viewer/2022062806/5697bf7d1a28abf838c84729/html5/thumbnails/11.jpg)
Static items participate in inside-out rule
11
m(int y) { int x; if (…) { int z; … v … w … … x … y … z };}
C
C@a0
v
Box for class Cw
Variables: static w field v parameter y local variables x and z
To see what declaration each reference v, w, x, y, z refers to, look in inside-out fashion: 1. then-block 2. method body 3. parameter list 4. fields 5. static variables
![Page 12: CS/ENGRD 2110 FALL 2015 Lecture 5: Local vars; Inside-out rule; constructors 1.](https://reader035.fdocuments.net/reader035/viewer/2022062806/5697bf7d1a28abf838c84729/html5/thumbnails/12.jpg)
A solution: use this12
setN(String n) {
this.n= n;}
Person
Person@a0
n
setN(String n) { this.n= n;}
Person
Person@a1
n
Memorize: Within an object, this evaluates to the name of the object.
In object Person@a0, this evaluates to Person@a0
In object Person@a1, this evaluates to Person@a1
[email protected] is this variable
![Page 13: CS/ENGRD 2110 FALL 2015 Lecture 5: Local vars; Inside-out rule; constructors 1.](https://reader035.fdocuments.net/reader035/viewer/2022062806/5697bf7d1a28abf838c84729/html5/thumbnails/13.jpg)
13
About super
Within a subclass object, super refers to the partition above the one that contains super.
toString() { … }
ObjectName() { return super.toString();}
Object
PhD@20
PhD
toString()
Because of the key-word super, this calls toString in the Object partition.
![Page 14: CS/ENGRD 2110 FALL 2015 Lecture 5: Local vars; Inside-out rule; constructors 1.](https://reader035.fdocuments.net/reader035/viewer/2022062806/5697bf7d1a28abf838c84729/html5/thumbnails/14.jpg)
14
Calling a constructor from a constructor
Time@fa8
Timehr 9 min 5
… Time(int, int) Time (int)
public class Time private int hr; //hour of day, 0..23 private int min; // minute of hour, 0..59
/** Constructor: instance with h hours and m minutes */ public Time(int h, int m) { …}
/** Constructor: instance with m minutes … */ public Time(int m) { hr = m / 60; min = m % 60; } …}
Want to change body to call first constructor
![Page 15: CS/ENGRD 2110 FALL 2015 Lecture 5: Local vars; Inside-out rule; constructors 1.](https://reader035.fdocuments.net/reader035/viewer/2022062806/5697bf7d1a28abf838c84729/html5/thumbnails/15.jpg)
15
Calling a constructor from a constructor
Time@fa8
Timehr 9 min 5
… Time(int, int) Time (int)
public class Time private int hr; //hour of day, 0..23 private int min; // minute of hour, 0..59
/** Constructor: instance with h hours and m minutes … */ public Time(int h, int m) { …}
/** Constructor: instance with m minutes … */ public Time(int m) { this(m / 60, m % 60); } …}Use this (Instead of Time) to call another constructor in the class.Must be first statement in constructor body!
![Page 16: CS/ENGRD 2110 FALL 2015 Lecture 5: Local vars; Inside-out rule; constructors 1.](https://reader035.fdocuments.net/reader035/viewer/2022062806/5697bf7d1a28abf838c84729/html5/thumbnails/16.jpg)
Principle: Initialize superclass fields first16
Executive@a0Object
name “G” start 1969
salary
10,000
Employee(String, int)toString() getCompensation()
toString() …
Employee
Executivebonus
getBonus() getCompensation() toString()
50,000
Class Employee contains info thatis common to all employees —name, start date, salary, etc.
getCompensation gives the salary
Executives also get a bonus. getCompensation is overridden to take this into account
Could have other subclasses for part-timers, temporary workers, consultants, etc., each with a different getCompensation
![Page 17: CS/ENGRD 2110 FALL 2015 Lecture 5: Local vars; Inside-out rule; constructors 1.](https://reader035.fdocuments.net/reader035/viewer/2022062806/5697bf7d1a28abf838c84729/html5/thumbnails/17.jpg)
17
Without OO …
Without OO, you would write a long involved method:
public double getCompensation(…) {
if (worker is an executive)
{ … }
else if (worker is part time)
{ … }
else if (worker is temporary)
{ … }
else …
OO eliminates need for many of these long, convoluted methods, which are hard to maintain.
Instead, each subclass has its own getCompensation.
End up with many more methods, which are usually very short
![Page 18: CS/ENGRD 2110 FALL 2015 Lecture 5: Local vars; Inside-out rule; constructors 1.](https://reader035.fdocuments.net/reader035/viewer/2022062806/5697bf7d1a28abf838c84729/html5/thumbnails/18.jpg)
/** Constructor: employee with name n, year hired d, salary s */public Employee(String n, int d, double s) { name= n; start= d; salary= s;}
Principle: initialize superclass fields first18
Executive@a0Object
name “G”start 1969
salary
Employee(String, int, double)
toString() …
Employee
10,000Executive
bonus
Executive(String, int, double)
50,000
![Page 19: CS/ENGRD 2110 FALL 2015 Lecture 5: Local vars; Inside-out rule; constructors 1.](https://reader035.fdocuments.net/reader035/viewer/2022062806/5697bf7d1a28abf838c84729/html5/thumbnails/19.jpg)
Principle: initialize superclass fields first19
Executive@a0
name start
salary
Employee(String, int, double)
Employee
Executivebonus
Executive(String, int, double)
/** Constructor: employee with name n, year hired d, salary s */public Employee(String n, int d, double s)
/** Constructor: executive with name n, year hired d, salary of $50,000, bonus b */public Executive(String n, int d, double b)
Principle: In subclass constructor, fill in the superclass fields first
How to do that if they are private?
Call constructor in superclass
![Page 20: CS/ENGRD 2110 FALL 2015 Lecture 5: Local vars; Inside-out rule; constructors 1.](https://reader035.fdocuments.net/reader035/viewer/2022062806/5697bf7d1a28abf838c84729/html5/thumbnails/20.jpg)
Principle: initialize superclass fields first20
Executive@a0
name start
salary
Employee(String, int, double)
Employee
Executivebonus
Executive(String, int, double)
/** Constructor: employee with name n, year hired d, salary s */public Employee(String n, int d, double s)
/** Constructor: executive with name n, year hired d, salary of $50,000, bonus b */public Executive(String n, int d, double b) {
Employee(n, d, 50000); bonus= b;}
super
To call a superclass constructor, use super( … )
![Page 21: CS/ENGRD 2110 FALL 2015 Lecture 5: Local vars; Inside-out rule; constructors 1.](https://reader035.fdocuments.net/reader035/viewer/2022062806/5697bf7d1a28abf838c84729/html5/thumbnails/21.jpg)
/** Constructor: an instance with …*/public C (…) {
S0; S1; …}
Principle: initialize superclass fields first
21
C@a0
C1( … )C1
C( … ) C
Java syntax: First statement of any constructor you write must be a call on another constructor this( … ); or super( … );
ObjectObject( … )
…
If you don’t put one in, Java silently inserts this one:
super();
super();