1 CS1110 18 September 2008 Discussion of Methods: Executing method calls.If-statements. The return statement in a function. Local variables. For this and.

Slides:



Advertisements
Similar presentations
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
Advertisements

1 CS100J. Lecture 2, 25 January 2007 Today’s topic: Objects and classes Reading for this lecture: Section 1.3. It’s most important that you study this.
1 CS Sept. Customizing a class & testing Quiz 2 on Tuesday 15 Sept Purpose of a constructor (slide 5) Evaluating a new expression (slide 6) Fields;
July 13 th.  If/ Else if / Else  Variable Scope  Nested if/else's  Switch statements  Conditional Operator.
CIT 590 Intro to Programming Java lecture 4. Agenda Types Collections – Arrays, ArrayLists, HashMaps Variable scoping Access modifiers – public, private,
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.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
CS102--Object Oriented Programming Lecture 5: – Arrays – Sorting: Selection Sort Copyright © 2008 Xiaoyan Li.
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).
Wednesday, 10/9/02, Slide #1 CS 106 Intro to CS 1 Wednesday, 10/9/02  QUESTIONS ??  Today:  Discuss HW #02  Discuss test question types  Review 
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.
 Wednesday, 9/25/02, Slide #1 CS106 Introduction to CS1 Wednesday, 9/25/02  QUESTIONS??  Today: More on functions  Reading: Chapter 3 through 3.7 
Lecture 11. Today’s topic Conditional statement –Relational operators –if statement –if-else statement –If-elseif statement.
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.
CS1101: Programming Methodology Aaron Tan.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational operators – Discover.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational and logical operators.
Chapter 06 (Part I) Functions and an Introduction to Recursion.
1 CS1110 Fall 2011 David Gries, Steve Marschner Reading for this lecture and previous lecture: Sections 1.1, 1.2, 1.3. Lab 1 will give you practice with.
ITBP 119 Algorithms and Problem Solving Section 2.1 Installing software Section 2.2 First Programs Section 2.3 Variables.
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.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
CMP 131 Introduction to Computer Programming Violetta Cavalli-Sforza Week 5, Lecture 1 (Monday)
CSC1401 Classes - 2. Learning Goals Computing concepts Adding a method To show the pictures in the slide show Creating accessors and modifiers That protect.
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.
CS100J 25 September 2003: Constructors We alluded to this topic several times in the past month. Now we make it all clear. Read Section 3.1.3, This.
Quiz 3 is due Friday September 18 th Lab 6 is going to be lab practical hursSept_10/exampleLabFinal/
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.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational and logical operators.
 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 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.
RECURSION, CONTINUED Lecture 8 CS2110 – Fall 2015.
1 CS1110. Lecture 2, 28 Jan Objects & classes PLive: Activities 3-3.1, 3-3.2, (not 3-3.3), 3-4.1, Summary of lectures: On course page,
CS/ENGRD 2110 SPRING 2016 Lecture 2: Objects and classes in Java 1.
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 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.
1 CS Sept Customizing a class & testing Quote for the day: There is no reason anyone would want a computer in their home. - -Ken Olson, founder.
RECURSION (CONTINUED) Lecture 9 CS2110 – Spring 2016.
1. Understanding the execution of local variable declaration (in a method body) new expression ( 3 steps ) method call (method frames, call stack) examples.
Repetition Statements
Recursion (Continued)
CS/ENGRD 2110 Fall 2017 Lecture 2: Objects and classes in Java
Introduction to Programming
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
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.
LRobot Game.
CS/ENGRD 2110 Fall 2017 Lecture 5: Local vars; Inside-out rule; constructors
Lecture 28: Types of Types
Sit next to someone. Today, we do some work in pairs.
Introduction to Programming
Recursion (Continued)
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 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.
Chapter 4: Control Structures I (Selection)
CS 1110 Prelim I: Review Session Spring 2011
Introduction to Programming
Review for Midterm 3.
Corresponds with Chapter 5
Presentation transcript:

1 CS September 2008 Discussion of Methods: Executing method calls.If-statements. The return statement in a function. Local variables. For this and next lecture: Read section 2. but NOT 2.3.8!!!! Do the self-review exercises in Oxymoron: a combination for epigrammatic effect of contradictory or incongruous words (as cruel kindness, laborious idleness ) airline foodState worker military intelligencepeace force Microsoft Works computer security sanitary landfilltight slacks religious tolerancebusiness ethics Congratulations!! You now know the basics of OO (object- orientation). There are more odds and ends, which we will be discussing, but the basics have been covered. We now turn to: Please sit next to someone. We will do some work in pairs today.

Prelim (preliminary exam) I Tuesday, September 30. 7:30-9:00PM 2 If you have a conflict, you MUST Maria Witlox by Friday night (tomorrow night) so that we know how many people have conflicts. Give Maria: Name. NetId. What the conflict is. Tuesday, we will give you a handout explaining what is on prelim I. But you can see it now, as well as previous prelims, on the course website. Click on exams in left column.

3 Method body: sequence of statements enclosed in { } (interspersed with declarations) to execute, in the order in which they appear /** Constructor: a chapter with title t, number n, and previous chapter null.*/ public Chapter(String t, int n) { title= t; number= n; previous= null; } Execute the three assignments in the order in which they appear. Same scheme is used when a cook uses a recipe. We explain exactly how a method call is executed so that you can understand how parameters and arguments work. parameters: t and n

4 The frame (the box) for a method call Remember: Every method is in a folder (object) or in a file-drawer. method name, instruction counterscope box local variables (don’t deal with these now) parameters scope box contains the name of entity that contains the method —a file- drawer or object. number of the statement of method body to execute next. Helps you keep track of what statement to execute next. Start off with 1. Draw the parameters as variables.

5 Execution of a method call. a0 K p setP(int x) { 1: p= x; } t a0 Execute the call t.setP(7); The first (and only) statement is #1. Procedure setP has one parameter: x. The call has one argument: expression 7. K 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.) 4. Erase the frame for the call. public class K { private int p; public void setP(int x) { p= x; }

6 Execute a function call public class K { private int p ; public int getP() { return p; } 1: a1 K p 5 getP() 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.) 4. Erase the frame for the call. (and, if it is a function use the value of the return-statement expression as the function call value t a1 K x= t.getP() + 1; x 3 int

7 Local variable: a variable declared within a method body /** = x + y */ public static int sum(int x, int y) { int t; t= x + y; return t; } a0 C p setP(int x) { 1: p= x; } sum(int x, int y) {…} C’s file drawer Evaluate this call. C.sum(5, 6); This time, when you create the frame for the call, draw parameters and local variables: 1. Draw a frame for the call. 2. Assign arg values to pars. 3. Execute the method body. 4. Erase frame for call. (If it is a function use value of return- statement expr. as function call value

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

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

10 Syntax of procedure/function/constructor and calls public ( ) { … } public void ( ) { … } public ( ) { … } function procedure constructor ( ) ( ) ; new ( ) function call procedure call constructor call Exec. of a function body must terminate by executing a statement “return ;”, where the has the. Exec. of a proc body may terminate by executing statement “return ;” Exec. of a constructor body initializes a new object of class. :,, …,

11 Local variable: a variable declared in a method body Scope of local variable: the sequence of statements following it. /** = 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.

12 Local variable: a variable declared in a method body Scope of local variable: the sequence of statements following it. /** s contains a name in the form exemplified by “David Gries”. Return the corresponding String “Gries, David”. There may be 1 or more blanks between the names. */ public static String switchFormat(String s) { // Store the first name in variable f and remove f from s int k; // Index of the first blank in s k= s.indexOf(' '); String f; // The first name in s. f= s.substring(0, k); s= s.substring(k); // Remove the blanks from s s= s.trim(); return s + ", " + f; } scope of k scope of f Numbering of characters in a String: “abcdef” declaration assignment