CS/ENGRD 2110 Spring 2017 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 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.
COMP 110 Introduction to Programming Mr. Joshua Stough October 8, 2007.
1 What’s a class People are confused with nested classes and anonymous classes. Experiments in a recitation last week revealed that the problems were probably.
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.
CSE 115 Week 4 February 4 - 8, Monday Announcements Software installation fest Tuesday & Wednesday 4-7 in Baldy 21. Software installation fest Tuesday.
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/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.
Recitation 2 Main Method, API & Packages, Java Basics.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
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.
Page: 1 การโปรแกรมเชิงวัตถุด้วยภาษา JAVA บุรินทร์ รุจจนพันธุ์.. ปรับปรุง 15 มิถุนายน 2552 Keyword & Data Type มหาวิทยาลัยเนชั่น.
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.
Introduction to Methods. Previously discussed There are similarities in make up of that can help you remember the construct of a class a class in the.
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.
 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 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.
CS1101 Group1 Discussion 6 Lek Hsiang Hui comp.nus.edu.sg
CS100A, 15 Sept Lecture 5 1 CS100A, 5 Sept This lecture continues the discussion of classes. The important new concept of this lecture is.
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.
CS/ENGRD 2110 FALL 2015 Lecture 6: Consequence of type, casting; function equals 1.
CS/ENGRD 2110 FALL 2013 Lecture 3: Fields, getters and setters, constructors, testing 1.
CS/ENGRD 2110 SPRING 2016 Lecture 4: The class hierarchy; static components 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.
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: Inner Classes Course : JAVA PROGRAMMING Paper Code: ETCS-307 Faculty : Dr. Prabhjot Kaur Reader, Dept. of IT 1.
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
Review Session.
Sorry these slides weren’t available last evening!
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 3: Fields, getters and setters, constructors, testing
CS/ENGRD 2110 Spring 2018 Lecture 5: Local vars; Inside-out rule; constructors
CS/ENGRD 2110 Spring 2017 Lecture 4: The class hierarchy; static components
Class Structure 16-Nov-18.
CS/ENGRD 2110 Fall 2017 Lecture 5: Local vars; Inside-out rule; constructors
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 3: Fields, getters and setters, constructors, testing
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 1110 Prelim I: Review Session Spring 2011
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 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. Piazza note @129 tells you how. Watch the loop invariant tutorials before next week’s recitation. They are linked from the Lecture Notes page.

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.

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 Class{ 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 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.

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); }

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!

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(“David”, ‘J’, “Gries”); PhD@a0 Object first last toString() Person PhD middle getName() null ‘\0’ 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! ‘J’

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() “David” “Gries” ‘J’ Because of keyword super, the call toString here refers to the Person partition. toString() { … super.toString() … }

Bottom-Up and Inside-Out Person PhD@a0 Object first last toString() Person PhD middle getName() “David” “Gries” ‘J’ sep ‘ ‘ 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 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