CS/ENGRD 2110 Spring 2014 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.
CS 1110 Prelim I: Review Session. Exam Info Prelim 1: 7:30–9:00PM, Thursday, 6 October, Statler Auditorium Look at the previous Prelims Arrive early!
CS 1110 Prelim I: Review Session. Introduction My name: Bruno Abrahao – We have four TA’s in the room to help you individually Shuang Zhao Nam Nguyen.
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.
Inheritance and Polymorphism Daniel Liang, Introduction to Java Programming.
CS/ENGRD 2110 FALL 2014 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.
CS1110 lecture 5 8 Feb 2010 Testing; class Object; toString; static variables/methods Reading for this lecture: Testing with JUnit (Appendix I.2.4 & pp.
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.
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.
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.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
Object Inheritance Lecturer: Kalamullah Ramli Electrical Engineering Dept. University of Indonesia Session-4.
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.
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.
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.
Review for Test2. Scope 8 problems, 60 points. 1 Bonus problem (5 points) Coverage: – Test 1 coverage – Exception Handling, Switch Statement – Array of.
CS/ENGRD 2110 FALL 2016 Lecture 3: Fields, getters and setters, constructors, testing 1.
2-Oct-16 Basic Object-Oriented Concepts. 2 Concept: An object has behaviors In old style programming, you had: data, which was completely passive functions,
CS/ENGRD 2110 Fall 2017 Lecture 2: Objects and classes in Java
CS1110 Classes, stepwise refinement 23 Sep 2009
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 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
CS/ENGRD 2110 Spring 2017 Lecture 5: Local vars; Inside-out rule; constructors
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
CS/ENGRD 2110 Fall 2018 The fattest knight at King Arthur's round table was Sir Cumference. He acquired his size from too much pi. Lecture 6: Consequence.
Sit next to someone. Today, we do some work in pairs.
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
Sit next to someone. Today, we do some work in pairs.
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
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 2014 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 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 Local variable: variable declared in method body b 8 d 7 c 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 variable /** 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); } 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 about placement of declaration /** 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); } 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 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.

Bottom-up/overriding rule Which method toString() is called by c.toString() ? c Butterfly@20 Butterfly@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() Butterfly “Beaut” name toString() { … }

Inside-out rule Inside-out rule: Code in a construct can reference any names declared in that construct, as well as names that appear in enclos-ing constructs. (If name is declared twice, the closer one prevails.) PersonPop Person Person@a0 n getNAndPop() { return n + PersonPop; } Person Person@a1 n getNAndPop() { return n + PersonPop; } Person’s objects and static components

Parameters participate in inside-out rule setN(String n) { n= n; } Person Person@a0 Parameter n “blocks” reference to field n. Doesn’t work right n setN(String name) { n= name; } Person Person@a0 n

A solution: use this Person@a0.n is this variable setN(String 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 Person@a0.n is this variable setN(String n) { this.n= n; } Person Person@a0 setN(String n) { this.n= n; } Person Person@a1 n n

About super Butterfly@20 Within a subclass object, super refers to the partition above the one that contains super. Object toString() Butterfly toString() { … } ObjectName() { return super.toString(); } Because of the key-word super, this calls toString in the Object partition.

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) { …} /** Constructor: instance with m minutes … */ public Time(int m) { hr= m / 60; min= m % 60; } … Time@fa8 Time hr 9 min 5 … Time(int, int) Time (int) 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) { …} /** Constructor: instance with m minutes … */ public Time(int m) { this(m / 60, m % 60); } … Time@fa8 Time hr 9 min 5 … Time(int, int) Time (int) Use this (Instead of Time) to call another constructor in the class. Must be first statement in constructor body!

Initialize superclass fields first Class Employee contains info that is common to all employees —name, start date, salary, etc. getCompensation gives the salary Executive@a0 Object name “G” start 1969 salary 10,000 Employee(String, int) toString() getCompensation() toString() … Employee Executive bonus getBonus() getCompensation() toString() 50,000 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

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

Principle: initialize superclass fields first /** Constructor: employee with name n, year hired d, salary s */ public Employee(String n, int d, double s) { name= n; start= d; salary= s; } Executive@a0 Object name “G” start 1969 salary Employee(String, int, double) toString() … Employee 10,000 Executive bonus Executive(String, int, double) 50,000

Principle: initialize superclass fields first /** 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 s, double b) Executive@a0 name start salary Employee(String, int, double) Employee Executive bonus Executive(String, int, double) Principle: In subclass constructor, fill in the superclass fields first How to do that if they are private? Call constructor in superclass

Principle: initialize superclass fields first /** 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 s, double b) { Employee(n, d, 50000); bonus= b; } Executive@a0 name start salary Employee(String, int, double) Employee Executive bonus Executive(String, int, double) super To call a superclass constructor, use super( … )

Principle: initialize superclass fields first /** Constructor: an instance with …*/ public C (…) { S0; S1; … } super(); 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( … ); Object Object( … ) … If you don’t put one in, Java inserts this one: super();