CS/ENGRD 2110 Spring 2016 Lecture 5: Local vars; Inside-out rule; constructors http://courses.cs.cornell.edu/cs2110.

Slides:



Advertisements
Similar presentations
CS/ENGRD 2110 SPRING 2015 Lecture 4: The class hierarchy; static components 1.
Advertisements

1 CS100J Classes, stepwise refinement 25 February 2007 Rsrecah on spleilng Aoccdrnig to a rscheearch at Cmabirgde Uinervtisy, it deosn't mttaer in waht.
1 Review of classes and subclasses M fast through this material, since by now all have seen it in CS100 or the Java bootcamp First packages Then classes.
Java boot camp1 Subclasses Concepts: The subclass and inheritance: subclass B of class A inherits fields and methods from A. A is a superclass of B. Keyword.
1 CS100J 18 September 2007 More on Methods. Developing methods. Also: The inside-out rule; and the use of this and super. Read sec. 2.5 on stepwise refinement.
1 CS100J 20 September 2005 More on Methods. Developing methods Also: About the use of this and super Read section 2.5 on stepwise refinement Listen to.
CS 1110 Final Exam: Review Session 2 Part 1 : Inheriting classes 1. Inheritance Facts 2. Constructors in Subclasses BREAK : 10 sec. Part 2 : Working with.
CS/ENGRD 2110 FALL 2014 Lecture 3: Fields, getters and setters, constructors, testing 1.
CS/ENGRD 2110 SPRING 2015 Lecture 3: Fields, getters and setters, constructors, testing 1.
CS/ENGRD 2110 FALL 2013 Lecture 5: Local vars; Inside-out rule; constructors 1.
1 Biggest issue!!! You can’t do questions on this topic correctly unless you draw variables, draw objects when they are created, and draw frames for method.
CS/ENGRD 2110 SPRING 2015 Lecture 6: Consequence of type, casting; function equals 1.
1 CS Sept 2010 Inside-out rule; use of this, super Developing methods (using Strings). Read sec. 2.5, stepwise refinement Listen to Plive, 2.5.1–2.5.4.
CS/ENGRD 2110 SPRING 2012 Lecture 2: Objects and classes in Java 1.
Inheritance Chapter 10 Programs built from objects/instances of classes An O.O. approach – build on earlier work. Use classes in library and ones you have.
CS/ENGRD 2110 FALL 2013 Lecture 4: The class hierarchy; static components 1.
1 CS100J Classes, stepwise refinement 14 Feb 2007 Rsrecah on spleilng Aoccdrnig to a rscheearch at Cmabirgde Uinervtisy, it deosn't mttaer in waht oredr.
Introduction to Java Chapter 7 - Classes & Object-oriented Programming1 Chapter 7 Classes and Object-Oriented Programming.
Chapter 5 Classes and Methods II Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas, E.
This recitation 1 An interesting point about A3: Using previous methods to avoid work in programming and debugging. How much time did you spend writing.
CS100A, Fall 1998 Key Concepts 1 These notes contain short definitions of the basic entities that make up a Java program, along with a description of the.
Inheritance CSI 1101 Nour El Kadri. OOP  We have seen that object-oriented programming (OOP) helps organizing and maintaining large software systems.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
1 CS1110 Stepwise refinement, more on classes 24 Sep 2009 Application of String processing and stepwise refinement. Miscellaneous points about classes.
1 CS1110 Classes, stepwise refinement 17 Feb 2009 Miscellaneous points about classes. More on stepwise refinement. Next: wrapper classes. Section 5.1 of.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
CS/ENGRD 2110 SPRING 2016 Lecture 2: Objects and classes in Java 1.
1 CS1110 Classes, stepwise refinement 12 Feb 2009 Rsrecah on spleilng Aoccdrnig to a rscheearch at Cmabirgde Uinervtisy, it deosn't mttaer in waht oredr.
CS100A, Fall Lecture 5 1 CS100A, Fall 1997 Lecture, Tuesday, 16 September. This lecture continues the discussion of classes. The important new concept.
CS/ENGRD 2110 FALL 2015 Lecture 6: Consequence of type, casting; function equals 1.
 Static  Example for Static Field  Example for Static Method  Math class methods  Casting  Scope of Declaration  Method Overloading  Constructor.
CS/ENGRD 2110 FALL 2013 Lecture 3: Fields, getters and setters, constructors, testing 1.
1 CS Sep 2011 Miscellaneous points about classes. More on stepwise refinement. Next: wrapper classes. Section 5.1 of class text Need Help? Make.
1 CS February 2009 Inside-out rule; use of this and super Developing methods (using String ops). Read sec. 2.5 on stepwise refinement Listen to.
CS100Lecture 61 Announcements Homework P1 due on Thursday Homework P2 handed out.
CS/ENGRD 2110 SPRING 2016 Lecture 6: Consequence of type, casting; function equals 1.
1 CS1110 Classes, wrapper classes, Vectors. 10 Feb 2012 Miscellaneous points about classes. Discussion of wrapper classes and class Vector Use the text.
CS/ENGRD 2110 FALL 2016 Lecture 3: Fields, getters and setters, constructors, testing 1.
CS/ENGRD 2110 Fall 2017 Lecture 2: Objects and classes in Java
Inheritance ITI1121 Nour El Kadri.
Some Eclipse shortcuts
CS1110 Classes, stepwise refinement 23 Sep 2009
CS/ENGRD 2110 Spring 2014 Lecture 5: Local vars; Inside-out rule; constructors
Review Session.
Sorry these slides weren’t available last evening!
Lecture 10: Inheritance Subclasses and superclasses
CS100J Final Class on Classes 22 September 2005
CS/ENGRD 2110 Fall 2017 Lecture 6: Consequence of type, casting; function equals
The fattest knight at King Arthur's round table was Sir Cumference
CS/ENGRD 2110 Spring 2018 Lecture 2: Objects and classes in Java
CS/ENGRD 2110 Spring 2018 Lecture 5: Local vars; Inside-out rule; constructors
CS/ENGRD 2110 Spring 2017 Lecture 5: Local vars; Inside-out rule; constructors
Class Structure 16-Nov-18.
Can perform actions and provide communication
Overloading and Overriding
CS/ENGRD 2110 Fall 2017 Lecture 5: Local vars; Inside-out rule; constructors
Class Structure 7-Dec-18.
Class Structure 2-Jan-19.
Can perform actions and provide communication
CS/ENGRD 2110 Fall 2018 Lecture 2: Objects and classes in Java
Chapter 11 Inheritance and Polymorphism
CS/ENGRD 2110 Fall 2018 Lecture 5: Local vars; Inside-out rule; constructors
CS/ENGRD 2110 Spring 2019 Lecture 2: Objects and classes in Java
Class Structure 25-Feb-19.
CS/ENGRD 2110 Spring 2019 Lecture 5: Local vars; Inside-out rule; constructors
Chapter 11 Inheritance and Polymorphism Part 1
CS/ENGRD 2110 Spring 2019 Lecture 2: Objects and classes in Java
CS100J Classes, stepwise refinement 21 September 2005
CS100J Final Class on Classes 10 February 2005
Presentation transcript:

CS/ENGRD 2110 Spring 2016 Lecture 5: Local vars; Inside-out rule; constructors http://courses.cs.cornell.edu/cs2110

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-32 and 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

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 …

Local variables middle(8, 6, 7) /** Return middle value of a, b, c (no ordering assumed) */ public static int middle(int a, int b, int c) { if (b > c) { int temp= b; b= c; c= temp; } if (a <= b) { return b; return Math.min(a, c); } Parameter: variable declared in () of method header Local variable: variable declared in method body a 8 c 7 b 6 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.

Scope of local variables /** Return middle value of a, b, c (no ordering assumed) */ public static int middle(int a, int b, int c) { if (b > c) { int temp= b; b= c; c= temp; } if (a <= b) { return b; return Math.min(a, c); } block Scope of local variable (where it can be used): from its declaration to the end of the block in which it is declared.

Principle: declaration placement /** Return middle value of a, b, c (no ordering assumed) */ public static int middle(int a, int b, int c) { int temp; if (b > c) { temp= b; b= c; c= temp; } if (a <= b) { return b; return Math.min(a, c); } Not good! No need for reader to know about temp except when reading the then-part of the if- statement Principle: Declare a local variable as close to its first use as possible.

Assertions promote understanding /** Return middle value of a, b, c (no ordering assumed) */ public static int middle(int a, int b, int c) {  if (b > c) {         int temp= b; b= c; c= temp;     }     // b <= c     if (a <= b) {         return b;     // a and c are both greater than b     return Math.min(a, c); } /** Return middle value of a, b, c (no ordering assumed) */ public static int middle(int a, int b, int c) { if (b > c) { int temp= b; b= c; c= temp; } if (a <= b) { return b; return Math.min(a, c); } Assertion: Asserting that b <= c at this point. Helps reader understand code below.

Bottom-up/overriding rule Which method toString() is called by turing.toString() ? turing Person@20 Person@20 Object Overriding rule or bottom-up rule: To find out which is used, start at the bottom of the object and search upward until a matching one is found. toString() Person Before next slide, demo constructor that splits a name into first and last; “Turing” name toString() { … }

Calling a constructor from a constructor 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) { hr = h; min = m; assert …; } /** Constructor: instance with m minutes … */ public Time(int m) { hr = m / 60; min = m % 60; } … Want to change body to call first constructor

Calling a constructor from a constructor 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) { hr = h; min = m; assert …; } /** Constructor: instance with m minutes … */ public Time(int m) { this(m / 60, m % 60); } … Before next slide, demo pulling out ‘ ‘ into a static final field called separator. Use this (not Time) to call another constructor in the class. Must be first statement in constructor body!

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 (static) sep Person Person@a0 name getNameWithSep() { return name + sep; } Person Person@a1 name getNameWithSep () { return name + sep; } Person’s objects and static components

Constructing with a Superclass /** Constructor: person “f n” */ public Person(String f, String l) { first= n; last= l; } /** Constructor: PhD “Dr. f m. l”*/ public PhD(String f, char m, String l) { super(f, l); middle= m; new PhD(“Ross”, ‘E’, “Tate”); PhD@a0 Object first last toString() Person PhD middle getName() null ‘\0’ Use super (not Person) to call superclass constructor. “Ross” “Tate” Before next slide, make these changes, then override toString in PhD and use super.toString(). Must be first statement in constructor body! ‘E’

About super Within a subclass object, super refers to the partition above the one that contains super. PhD@a0 Object first last toString() Person PhD middle getName() “Ross” “Tate” ‘E’ Because of the keyword super, the call toString here refers to the Person partition. toString() { … super.toString() … }

Bottom-Up and Inside-Out Person (static) PhD (static) PhD@a0 Object first last toString() Person PhD middle getName() “Ross” “Tate” ‘E’ sep ‘ ‘ title “Dr.” Refactor “Dr.” into a static variable. Then change toString to use separator instead of ‘ ‘. Then override getName to use middleInitial. super getName() toString()

Without OO … Without OO, you would write a long involved method: public double getName(Person p) { if (p is a PhD) { … } else if (p hates formality) else if (p prefers anonymity) else … } OO eliminates need for many of these long, convoluted methods, which are hard to maintain. Instead, each subclass has its own getName. Results in many overriding method implementations, each of which is usually very short