Sit next to someone. Today, we do some work in pairs.

Slides:



Advertisements
Similar presentations
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
Advertisements

CIT 590 Intro to Programming Java lecture 4. Agenda Types Collections – Arrays, ArrayLists, HashMaps Variable scoping Access modifiers – public, private,
1 CS100J 1 February Customizing a class & testing Quote for the day: There is no reason anyone would want a computer in their home. - -Ken Olson,
Road Map Introduction to object oriented programming. Classes
Access to Names Namespaces, Scopes, Access privileges.
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.
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!
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 Executing Method Calls This lecture tells you precisely how method calls are executed (a few details will have to wait until we get to classes and objects).
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.
CS 117 Spring 2002 Review for Exam 2 March 6, 2002 open book, 1 page of notes.
1 CS100J 05 February 2005 Today’s topic: Customizing a class (continued) Quiz 1 is today Quiz 2 is next Tuesday Quote for the day: There is no reason anyone.
CS1110 lecture 5 14 Sept 2010 Testing; the class Object; toString; static variables & methods Reading for this lecture: Testing with JUnit (Appendix I.2.4.
ECE122 Feb. 22, Any question on Vehicle sample code?
Java development environment and Review of Java. Eclipse TM Intergrated Development Environment (IDE) Running Eclipse: Warning: Never check the “Use this.
CSC1401 Classes - 2. Learning Goals Computing concepts Adding a method To show the pictures in the slide show Creating accessors and modifiers That protect.
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.
1 CS1110 lecture 4 9 Sept. Customizing a class & testing Classes: fields; getter & setter methods. Secs (p. 45) & 3.1 (pp. 105–110 only) Constructors.
Lecture 101 CS110 Lecture 10 Thursday, February Announcements –hw4 due tonight –Exam next Tuesday (sample posted) Agenda –questions –what’s on.
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.
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.
Principled approach to teaching OO first David Gries Computer Science, Cornell 1.
1 CS100J 08 September 2005 Today’s topic: Customizing a class (continued) Quote for the day: There is no reason anyone would want a computer in their home.
1 CS September 2008 Discussion of Methods: Executing method calls.If-statements. The return statement in a function. Local variables. For this and.
1 CS1110 Thursday, 10 Feb 2011 Discussion of Methods: Executing method calls. If-statements. The return statement in a function. Local variables. For this.
1 For more info: CS February 2009 In 1968, the Defense Department hired Bolt Beranek and Newman (BBN) of Boston.
CSCI 3328 Object Oriented Programming in C# Chapter 4: C# Control Statement – Part I – Exercises 1 Xiang Lian The University of Texas Rio Grande Valley.
CS100Lecture 61 Announcements Homework P1 due on Thursday Homework P2 handed out.
Java 5 Class Anatomy. User Defined Classes To this point we’ve been using classes that have been defined in the Java standard class library. Creating.
1. Understanding the execution of local variable declaration (in a method body) new expression ( 3 steps ) method call (method frames, call stack) examples.
Chapter 6 CS 3370 – C++ Functions.
Java Primer 1: Types, Classes and Operators
CS/ENGRD 2110 Spring 2018 Lecture 2: Objects and classes in Java
CS 302 Week 11 Jim Williams, PhD.
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.
CS 1110 Final Exam: Review Session 1 Drawing frames for calls, executing method calls Biggest issue!!! You can’t do questions on this topic correctly.
Class Structure 28-Nov-18.
6 Chapter Functions.
CSCI 3328 Object Oriented Programming in C# Chapter 4: C# Control Statement – Part I – Exercises UTPA – Fall 2012 This set of slides is revised from lecture.
CS/ENGRD 2110 Spring 2016 Lecture 5: Local vars; Inside-out rule; constructors
CHAPTER 6 GENERAL-PURPOSE METHODS
Unit 3 - The while Loop - Extending the Vic class - Examples
Class Structure 7-Dec-18.
The University of Texas – Pan American
CS100A Lecture 15, 17 22, 29 October 1998 Sorting
Class Structure 2-Jan-19.
CS/ENGRD 2110 Fall 2018 Lecture 5: Local vars; Inside-out rule; constructors
CS139 October 11, 2004.
CS2011 Introduction to Programming I Methods (II)
Sit next to someone. Today, we do some work in pairs.
CS 1110 Final Exam: Review Session 1 Drawing frames for calls, executing method calls Biggest issue!!! You can’t do questions on this topic correctly.
Recap Week 2 and 3.
Lecture 5: For Loops Building Java Programs: A Back to Basics Approach
Namespaces, Scopes, Access privileges
Class Structure 25-Feb-19.
CS360 Client/Server Programming Using Java
CS/ENGRD 2110 Spring 2019 Lecture 5: Local vars; Inside-out rule; constructors
Based on slides created by Bjarne Stroustrup & Tony Gaddis
CS 1110 Prelim I: Review Session Spring 2011
CS100A Lecture 15, 17 22, 29 October 1998 Sorting
CS/ENGRD 2110 Spring 2019 Lecture 2: Objects and classes in Java
CS 1054: Lecture 2, Chapter 1 Objects and Classes.
Loops and Iteration CS 21a: Introduction to Computing I
Review for Midterm 3.
Corresponds with Chapter 5
Lecture 20 – Practice Exercises 4
Previous Lecture: Today’s Lecture: Reading (JV):
Presentation transcript:

Sit next to someone. Today, we do some work in pairs. CS1110 Thursday, 16 February 2010 Congratulations!! You now know the basics of OO (object-orientation). Discussion of Methods: Executing method calls. If-statements. The return statement in a function. Local variables. For this and next lecture: Read chapter 2, but NOT 2.3.8!!!! Do the self-review exercises in 2.3.4 Sit next to someone. Today, we do some work in pairs. Get out a blank sheet of paper. The last slide concerns local variables –variables declared within a method body. We don’t have time to discuss them. You are responsible for knowing about local variables. Read pp. 76-78 (sec. 2.3.7). Take advantage: see videos of the 11:15 lecture for CS1110 on www.VideoNote.com. Log in with your Cornell netid

/** An instance keeps information about a book chapter */ public class Chapter { // class invariant: meanings of fields and constraints on them private int number; // the chapter number, in range 0..100 private String title; // chapter title private Chapter prev; // instance for the previous chapter // (null if no previous chapter) … }

declaration of parameter t We write programs in order to do things. Methods are the key “doers”. /** Constructor: a chapter with title t, number n, and previous chapter null.*/ public Chapter(String t, int n) { title= t; number= n; previous= null; } Within the body (between { }) is the sequence of statements to execute when the method is called. (“Follow the recipe”.) parameters: t and n declaration of parameter t Memorize: a parameter is a variable that is declared within the parentheses of a method header. But how is a method call executed? How do parameters and arguments work?

The frame (the box) for a method call Remember: Every method is in a folder (object) or in a file-drawer. method name: instruction counter scope box local variables (don’t deal with these now) parameters Draw the parameters as variables. number of the statement of method body to execute next. Start with 1. Helps you keep track of what statement to execute next. scope box contains the name of entity that contains the method —a file drawer or object. Put example here. The scope box contains: For an instance method, the name of the object in which it resides For a static method, the name of the class in which it is defined

To execute the call x.setAmt(50); 1. Draw a frame for the call. 2. Assign the value of the argument to the parameter (in the frame). 3. Execute the method body. (Look for variables in the frame; if not there, look in the place given by the scope box.) public void setAmt(int newAmt) { amt= newAmt; } 4. Erase the frame for the call. a0 Account amt setAmt(int newAmt) {amt= newAmt;} getAmt() {…} 15 x a0 Account

To execute the call cash= y.getAmt(); 1. Draw frame for call. 2. Assign value of argument to parameters (in the frame). public int getAmt() { return amt; } 3. Execute method body. (Look for variables in the frame; if not there, look in the place given by the scope box.) 4. Erase the frame for call; use value of return-statement expression as function-call value. a1 Account amt setAmt(int newAmt) {…} getAmt() {return amt;} 25 a1 y a1 Account cash int

1. Draw a frame for the call. Chapter(String t, int n) { String d; new Chapter(“Intro”, 1) 1. Draw a frame for the call. Chapter(String t, int n) { String d; 1: d= t; 2: title= d; 3: number= n; 4: previous= null; } 2. Assign arg values to pars. 3. Execute the method body. 4. Erase the frame for the call. a1 Chapter Chapter(String t, int n) { … } a8 null title previous number Note local variable d declared within method body. It should be drawn in frame for call.

/* swap x, y to put larger in y */ if (x > y) { int t; t= x; x= y; y= t; } /* Put smaller of x, y in z */ if (x < y) { z= x; } else { z= y; } if statement if-else statement Syntax: if (<boolean expression>) <statement1> else <statement2> Execution: if the boolean expression is true, then execute <statement1>; otherwise, execute <statement2> Syntax: if (<boolean expression>) <statement> Execution: if the <boolean expression> is true, then execute the <statement>

Idiom: if statements and multiple return staements /** = smallest of b, c, d */ public static int smallest(int b, int, c, int d) { } Execution of statement return <expr> ; terminates execution of the procedure body and yields the value of <expr> as result of function call if (b <= c && b <= d) { return b; } Assertion // { The smallest is either c or d } if (c <= d) { return c; } // { the smallest is d } return d; Execution of function body must end by executing a return statement.

Syntax of procedure/function/constructor and calls public <result type> <name> ( <parameter declarations> ) { … } public void <name> ( <parameter declarations> ) { … } public <class-name> ( <parameter declarations> ) { … } function procedure constructor Exec. of a function body must terminate by executing a statement “return <exp> ;”, where the <exp> has the <result type>. Exec. of a proc body may terminate by executing statement “return ;” Exec. of a constructor body initializes fields of a new object in order to make the class invariant true. <name> ( <arguments> ) <name> ( <arguments> ) ; new <class-name> ( <arguments> ) function call procedure call constructor call <arguments>: <expression>, <expression>, …, <expression>

public static int max(int x, int y) { Scope of local variable is the places where it can be used. The scope is the sequence of statements following it within the containing “block”. /** = the max of x and y */ public static int max(int x, int y) { // Swap x and y to put the max in x if (x < y) { int temp; temp= x; x= y; y= temp; } return x; scope of temp You can’t use temp down here This is an error.