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

Slides:



Advertisements
Similar presentations
1 CS100J Classes, stepwise refinement 25 February 2007 Rsrecah on spleilng Aoccdrnig to a rscheearch at Cmabirgde Uinervtisy, it deosn't mttaer in waht.
Advertisements

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.
Vocabulary Key Terms polymorphism - Selecting a method among many methods that have the same name. subclass - A class that inherits variables and methods.
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.
COMP More About Classes Yi Hong May 22, 2015.
1 Inheritance and Polymorphism Chapter 9. 2 Polymorphism, Dynamic Binding and Generic Programming public class Test { public static void main(String[]
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.
Recitation 2 Main Method, API & Packages, Java Basics.
4.1 Instance Variables, Constructors, and Methods.
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.
Inheritance and Polymorphism Daniel Liang, Introduction to Java Programming.
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.
RECITATION 4. Classes public class Student { } Data Members public class Student { private String name; public String id; }
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.
Inheritance (Part 5) Odds and ends 1. Static Methods and Inheritance  there is a significant difference between calling a static method and calling a.
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.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 9 Inheritance and.
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
CIT 590 Intro to Programming Lecture 13. Some Eclipse shortcuts CTRL + SHIFT + F – format file (proper indentation etc). Please do this before you submit.
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.
1 CS September 2008 Discussion of Methods: Executing method calls.If-statements. The return statement in a function. Local variables. For this and.
CS1101 Group1 Discussion 6 Lek Hsiang Hui comp.nus.edu.sg
CS/ENGRD 2110 SPRING 2016 Lecture 2: Objects and classes in Java 1.
CS100A, Fall Lecture 5 1 CS100A, Fall 1997 Lecture, Tuesday, 16 September. This lecture continues the discussion of classes. The important new concept.
 Static  Example for Static Field  Example for Static Method  Math class methods  Casting  Scope of Declaration  Method Overloading  Constructor.
OOP Basics Classes & Methods (c) IDMS/SQL News
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 FALL 2016 Lecture 3: Fields, getters and setters, constructors, testing 1.
Defining Your Own Classes II
CS/ENGRD 2110 Fall 2017 Lecture 2: Objects and classes in Java
Topic: Classes and Objects
Recitation 2 Exception handling.
Class Structure 15-Jun-18.
Some Eclipse shortcuts
CS1110 Classes, stepwise refinement 23 Sep 2009
CS/ENGRD 2110 Spring 2014 Lecture 5: Local vars; Inside-out rule; constructors
CS100J Final Class on Classes 22 September 2005
CS/ENGRD 2110 Fall 2017 Lecture 6: Consequence of type, casting; function equals
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
Chapter 9 Inheritance and Polymorphism
CS/ENGRD 2110 Spring 2017 Lecture 5: Local vars; Inside-out rule; constructors
Class Structure 16-Nov-18.
Overloading and Overriding
CS/ENGRD 2110 Spring 2016 Lecture 5: Local vars; Inside-out rule; constructors
Class Structure 7-Dec-18.
Class Structure 2-Jan-19.
CS/ENGRD 2110 Fall 2018 Lecture 2: Objects and classes in Java
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
Classes CS 21a: Introduction to Computing I
Chapter 11 Inheritance and Polymorphism Part 1
Local variables and how to recognize them
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
SPL – PS3 C++ Classes.
Presentation transcript:

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

Announcements Writing tests to check that the code works when the precondition is satisfied is not optional. Writing assertions to verify the precondition is satisfied is not optional, and if you do so incorrectly you will lose points. Writing tests to verify that you have done (2) correctly is optional. Look at JavaHyperText entry for JUnit testing, to see how to test whether an assert statement is correct.

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 …

Assignment 1 Due on September 6 (tomorrow!). Form a group before submitting (or lose points). One partner has to invite the other on CMS, and the other has to accept. Finish early!

References to JavaHyperText local variable scope this shadowing a variable inside-out rule super constructor; constructor call; constructor, default; constructor call, default

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. Every pair of curly braces defines a block.

Scope In General: 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.) /** A useless class to illustrate scopes*/ public class C{ private int field; public void method(int parameter) {      if (field > parameter) {          int temp= parameter;      } } class method block

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 Demo after this slide: show moving the point where temp was declared. Principle: Declare a local variable as close to its first use as possible.

Poll time! What 3 numbers are printed? public class ScopeQuiz {   private int a;      public ScopeQuiz(int b) {     System.out.println(a);     int a= b + 1;     this.a= a;     a= a + 1;   }   public static void main(String[] args) {     int a= 5;     ScopeQuiz s= new ScopeQuiz(a);     System.out.println(s.a); } A: 5, 6, 6 B: 0, 6, 6 C: 6, 6, 6 D: 0, 6, 0

Bottom-up/overriding rule Which method toString() is called by turing.toString() ? turing Person@20 Person@20 Object The overriding rule, a.k.a. the bottom-up rule: To find out which method 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() { … }

Constructing with a Superclass /** Constructor: person “f n” */ public Person(String f, String l) { first= n; last= l; } /** Constructor: PhD with a year. */ public PhD(String f, String l, int y) { super(f, l); gradYear= y; new PhD("David", "Gries", 1966); PhD@a0 Object first last toString() Person PhD gradYear getName() null Use super (not Person) to call superclass constructor. "David" "Gries" Before next slide, make these changes, then override toString in PhD and use super.toString(). Must be first statement in constructor body! 1966

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 gradYear getName() "David" "Gries" 1966 Because of keyword super, the call toString here refers to the Person partition. "super" refers to the partition in the object that's directly above the current one. getName() { … super.getName() … }

Bottom-Up and Inside-Out Person PhD@a0 Object first last toString() Person PhD gradYear getName() "David" "Gries" 1966 sep ' ' super getName()

Without OO … Without OO, you would write a long involved method: public double getName(Person p) { if (p is a PhD) { … } else if (p is a GradStudent) 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