Mapping Methods With Arguments

Slides:



Advertisements
Similar presentations
Overloading Having more than one method with the same name is known as overloading. Overloading is legal in Java as long as each version takes different.
Advertisements

Decision Structures - If / Else If / Else. Decisions Often we need to make decisions based on information that we receive. Often we need to make decisions.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 5 Selection Statements Animated Version.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 5 Selection Statements.
Programming In C++ Spring Semester 2013 Lecture 4 Programming In C++, Lecture 3 By Umer Rana.
Expressions and Statements. 2 Contents Side effects: expressions and statements Expression notations Expression evaluation orders Conditional statements.
Μαθαίνοντας Python [Κ4] ‘Guess the Number’
July 13 th.  If/ Else if / Else  Variable Scope  Nested if/else's  Switch statements  Conditional Operator.
CS Oct 2006 Chap 6. Functions General form; type Name ( parameters ) { … return value ; }
Chapter 15 Strings String::Concat String::CompareTo, Equals, == If( string1 == S”Hello”) String1->Equals(S”Hello”) String1->CompareTo(S”Hello”) CompareTo.
Slides prepared by Rose Williams, Binghamton University Chapter 11 Recursion.
Copyright © 2012 Pearson Education, Inc. Chapter 6 Modularizing Your Code with Methods.
Execution Control with If/Else and Boolean Functions
Flow of Control. 2 Control Structures Control structure: An instruction that determines the order in which other instructions in a program are executed.
Functions Art &Technology, 3rd Semester Aalborg University Programming David Meredith
Computer Science Department Data Structure & Algorithms Lecture 8 Recursion.
COMPUTER PROGRAMMING. Functions What is a function? A function is a group of statements that is executed when it is called from some point of the program.
Machine Independent Macro Processor Features Concatenation of Macro Parameters Generation of Unique Labels Conditional Macro Expansion Keyword Macro.
David Streader & Peter Andreae Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington Objects.
Namespace, scope, compile time activities, runtime activities When do the small integer values get stored in RAM? How did the names in the builtin namespace.
Starting Out with Java: From Control Structures through Objects Fifth Edition by Tony Gaddis Chapter 5: Methods.
Algorithms Writing instructions in the order they should execute.
February ,  2/16: Exam 1 Makeup Papers Available  2/20: Exam 2 Review Sheet Available in Lecture  2/27: Lab 2 due by 11:59:59pm  3/2:
1 Methods Introduction to Methods Passing Arguments to a Method More About Local Variables Returning a Value from a Method Problem Solving with Methods.
>> PHP: Insert Query & Form Processing. Insert Query Step 1: Define Form Variables Step 2: Make DB Connection Step 3: Error Handling Step 4: Define the.
Chapter 4: Elementary Number Theory and Methods of Proof 4.8 Application: Algorithms 1 Begin at the beginning…and go on till you come to the end: then.
1 Brief Version of Starting Out with C++, 4th Brief Edition Chapter 6 Functions.
Sudeshna Sarkar, IIT Kharagpur 1 Functions Lecture
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
CS2102: Lecture on Abstract Classes and Inheritance Kathi Fisler.
A: A: double “4” A: “34” 4.
Chapter 5 : Methods Part 2. Returning a Value from a Method  Data can be passed into a method by way of the parameter variables. Data may also be returned.
Lesson thirteen Conditional Statement "if- else" ©
Expressions Methods if else Statements Loops Potpourri.
1 CSC103: Introduction to Computer and Programming Lecture No 16.
Review Expressions and operators Iteration – while-loop – for-loop.
AP Java Ch. 4 Review Question 1  Java methods can return only primitive types (int, double, boolean, etc).
5.3 EVALUATION OF POSTFIX EXPRESSION For example, consider the evaluation of the following postfix expression using stacks: abc+d*f/- where, a=6, b=3,
Under the Hood: A Map This slidedeck shows the various kinds of information that Java tracks under the hood as you create classes and objects and evaluate.
Introduction to Methods ISYS 350. Methods Methods can be used to break a complex program into small, manageable pieces – This approach is known as divide.
Conditional Statements A conditional statement lets us choose which statement will be executed next Conditional statements give us the power to make basic.
ITM 3521 ITM 352 Functions. ITM 3522 Functions  A function is a named block of code (i.e. within {}'s) that performs a specific set of statements  It.
Data Structures & Algorithms CHAPTER 2 Arrays Ms. Manal Al-Asmari.
Follow up from lab See Magic8Ball.java Issues that you ran into.
CS314 – Section 5 Recitation 9
CS314 – Section 5 Recitation 10
Functions.
function definition How to build a color house with number bedrooms.
Loops in Java.
Agenda Warmup Finish 2.4 Assignments
>> PHP: Form Processing
Updating Object Fields
Hash table another data structure for implementing a map or a set
CS2102: Lecture on Abstract Classes and Inheritance
Starting Out with Java: From Control Structures through Objects
Review Operation Bingo
Intro to UML Edited from presentation found at:
slides created by Ethan Apter
slides created by Ethan Apter
GCSE Computer Science – Logic Gates & Boolean Expressions
Flow of Control.
February , 2009 CSE 113 B.
Nate Brunelle Today: Conditional Decision Statements
PROGRAM FLOWCHART Iteration Statements.
Question 1a) What is printed by the following Java program? int s;
DemeterF.
public class Dillo {     public int length;     public boolean isDead;         
slides created by Ethan Apter and Marty Stepp
ITM 352 Functions.
Lecture 2 - Names & Functions
Presentation transcript:

Mapping Methods With Arguments This slidedeck shows how the areas of our “under the hood” map get modified when you call a method that takes another object as input. We again use the Dillo class, but this time after we have written a longerThan method (that takes another Dillo as input).

We start with two objects bound to the names babyDillo and adultDillo. KNOWN CLASSES OBJECTS class Dillo { int length; boolean isDead; Dillo (int length, boolean isDead) { this.length = length; this.isDead = isDead; } boolean longerThan(Dillo other) { return (this.length > other.length); Dillo: length = 8; isDead = false; boolean longerThan(Dillo other) { return (this.length > other.length); } Dillo: length = 24; isDead = true boolean longerThan(Dillo other) { return (this.length > other.length); } We start with two objects bound to the names babyDillo and adultDillo. Notice that each object contains a copy of the method NAMED VALUES EXPRESSION (impact on other areas are in red) babyDillo adultDillo

The name this now refers to adultDillo, and other refers to babyDillo KNOWN CLASSES OBJECTS class Dillo { int length; boolean isDead; Dillo (int length, boolean isDead) { this.length = length; this.isDead = isDead; } boolean longerThan(Dillo other) { return (this.length > other.length); Dillo: length = 8; isDead = false; boolean longerThan(Dillo other) { return (this.length > other.length); } Dillo: length = 24; isDead = true boolean longerThan(Dillo other) { return (this.length > other.length); } Now we call the longerThan method on adultDillo, passing babyDillo as the argument The name this now refers to adultDillo, and other refers to babyDillo NAMED VALUES EXPRESSION (impact on other areas are in red) babyDillo adultDillo adultDillo.longerThan(babyDillo) this other

EXPRESSION (impact on other areas are in red) KNOWN CLASSES OBJECTS class Dillo { int length; boolean isDead; Dillo (int length, boolean isDead) { this.length = length; this.isDead = isDead; } boolean longerThan(Dillo other) { return (this.length > other.length); Dillo: length = 8; isDead = false; boolean longerThan(Dillo other) { return (this.length > other.length); } Dillo: length = 24; isDead = true boolean longerThan(Dillo other) { return (this.length > other.length); } The expression being evaluated now changes to the body of the method from the adultDillo class NAMED VALUES EXPRESSION (impact on other areas are in red) babyDillo adultDillo this.length > other.length this other

Substitute these values in the expression KNOWN CLASSES OBJECTS class Dillo { int length; boolean isDead; Dillo (int length, boolean isDead) { this.length = length; this.isDead = isDead; } boolean longerThan(Dillo other) { return (this.length > other.length); Dillo: length = 8; isDead = false; boolean longerThan(Dillo other) { return (this.length > other.length); } Dillo: length = 24; isDead = true boolean longerThan(Dillo other) { return (this.length > other.length); } To evaluate this expression, follow the arrows from the names: this refers to the lower object, which has length 24, while other refers to the upper object Substitute these values in the expression NAMED VALUES EXPRESSION (impact on other areas are in red) babyDillo adultDillo 24 > 8 this other

EXPRESSION (impact on other areas are in red) KNOWN CLASSES OBJECTS class Dillo { int length; boolean isDead; Dillo (int length, boolean isDead) { this.length = length; this.isDead = isDead; } boolean longerThan(Dillo other) { return (this.length > other.length); Dillo: length = 8; isDead = false; boolean longerThan(Dillo other) { return (this.length > other.length); } Dillo: length = 24; isDead = true boolean longerThan(Dillo other) { return (this.length > other.length); } Next we execute the return, which ends the method. The this and other names go away (they are only active while the method is being evaluated) NAMED VALUES EXPRESSION (impact on other areas are in red) babyDillo adultDillo return true

EXPRESSION (impact on other areas are in red) KNOWN CLASSES OBJECTS class Dillo { int length; boolean isDead; Dillo (int length, boolean isDead) { this.length = length; this.isDead = isDead; } boolean longerThan(Dillo other) { return (this.length > other.length); Dillo: length = 8; isDead = false; boolean longerThan(Dillo other) { return (this.length > other.length); } Dillo: length = 24; isDead = true boolean longerThan(Dillo other) { return (this.length > other.length); } This example illustrates how names (this and parameters) are added to the map when calling a method, then removed when a method is finished NAMED VALUES EXPRESSION (impact on other areas are in red) babyDillo adultDillo