Introduction to Object-Oriented Programming with Java--Wu

Slides:



Advertisements
Similar presentations
Chapter 4 Constructors and Destructors. Objectives Constructors – introduction and features The zero-argument constructor Parameterized constructors Creating.
Advertisements

Etter/Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 4 Modular Programming with Functions.
DONT PANIC!! Lots of new notions coming in these slides Dont worry if not all of it makes perfect sense Well meet most of this stuff again in detail later.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Four Defining Your Own Classes.
Objects & Methods Defining Classes. Slide 2 Reference Variables Revisited Remember: Object variables are references (aka pointers) Point to “null” by.
Topic 10 Java Memory Management. 1-2 Memory Allocation in Java When a program is being executed, separate areas of memory are allocated for each class.
Programmer-defined classes Part 2. Topics Returning objects from methods The this keyword Overloading methods Class methods Packaging classes Javadoc.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 7: User-Defined Functions II.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 7: User-Defined Functions II.
Chapter 7: User-Defined Functions II
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 7: User-Defined Functions II.
Chapter 7: User-Defined Functions II Instructor: Mohammad Mojaddam.
1 Lecture 18:User-Definded function II(cont.) Introduction to Computer Science Spring 2006.
1 Chapter 7 User-Defined Methods Java Programming from Thomson Course Tech, adopted by kcluk.
COMPSCI 125 Spring 2005 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Four: Defining Your Own Classes *Instantiable.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 7 More on Defining Classes Overloading.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 7 Defining Your Own Classes Part 2.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 7 Defining Your Own Classes Part 2.
Chapter Four Defining Your Own Classes continued.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Why Programmer-Defined Classes Using just the String,
EE4E. C++ Programming Lecture 1 From C to C++. Contents Introduction Introduction Variables Variables Pointers and references Pointers and references.
Local Variables A local variable is a variable that is declared within a method declaration. Local variables are accessible only from the method in which.
Runtime Environments Compiler Construction Chapter 7.
Chapter 6: User-Defined Functions
C++ Programming: From Problem Analysis to Program Design, Fifth Edition, Fifth Edition Chapter 7: User-Defined Functions II.
Methods F Hello World! F Java program compilation F Introducing Methods F Declaring Methods F Calling Methods F Passing Parameters by value F Overloading.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Sample Slides - 1 Sample Animated Slides for Wu, Intro to OOP.
User Defined Functions Chapter 7 2 Chapter Topics Void Functions Without Parameters Void Functions With Parameters Reference Parameters Value and Reference.
© 2000 McGraw-Hill Modified by C.W.Pang with author's permission Intro to OOP with Java--Wu Chapter Chapter 1 Introduction to Object-oriented Programming.
1 Methods Introduction to Methods Passing Arguments to a Method More About Local Variables Returning a Value from a Method Problem Solving with Methods.
Functions: Part 2 of /11/10: Lecture 16 CMSC 104, Section 0101 John Y. Park 1.
Chapter 10: Classes and Data Abstraction. Objectives In this chapter, you will: Learn about classes Learn about private, protected, and public members.
An Introduction to Programming with C++ Sixth Edition Chapter 10 Void Functions.
Building java programs, chapter 3 Parameters, Methods and Objects.
Functions Math library functions Function definition Function invocation Argument passing Scope of an variable Programming 1 DCT 1033.
1 Chapter 5: Methods FIntroducing Methods FDeclaring Methods FCalling Methods FLocal Variables in Methods FPass by Value FOverloading Methods FMethod.
DCS 2133 Object Oriented Programming Defining Your Own Classes Part 2.
Methods Awesomeness!!!. Methods Methods give a name to a section of code Methods give a name to a section of code Methods have a number of important uses.
Chapter 10: Classes and Data Abstraction. Classes Object-oriented design (OOD): a problem solving methodology Objects: components of a solution Class:
Classes - Intermediate
Methods.
Methods and Parameters. Slide 2 Internal “Convenience” Methods External Methods ways to respond to requests from external “users” e.g., deposit( int ),
Chapter 7- Defining Your Own Classes Part 2 : Objectives After you have read and studied this chapter, you should be able to –Describe how objects are.
Functions + Overloading + Scope
Chapter 7 User-Defined Methods.
Chapter 7: User-Defined Functions II
Chapter 10: Void Functions
FIGURE 4-10 Function Return Statements
User-Defined Functions
2008/11/10: Lecture 16 CMSC 104, Section 0101 John Y. Park
Chapter 4 Defining Instantiable Classes
More Object Oriented Programming
FIGURE 4-10 Function Return Statements
Defining Your Own Classes
4-2 Functions in C In C, the idea of top–down design is done using functions. A C program is made of one or more functions, one and only one of which.
Variables and Their scope
Introduction to Object-Oriented Programming with Java--Wu
Functions, Part 2 of 3 Topics Functions That Return a Value
Array and Method.
FIGURE 4-10 Function Return Statements
Chapter 7: User-Defined Functions II
Chapter 9: Value-Returning Functions
CS150 Introduction to Computer Science 1
FIGURE 4-10 Function Return Statements
Functions, Part 2 of 3 Topics Functions That Return a Value
Functions, Part 2 of 3 Topics Functions That Return a Value
Corresponds with Chapter 5
Presentation transcript:

Introduction to Object-Oriented Programming with Java--Wu Local Variables A local variable is a variable that is declared within a method declaration. Local variables are accessible only from the method in which they are declared. Memory space for local variables are allocated only during the execution of the method. When the method execution completes, memory space will be deallocated. Grukkee … out to reality LocalVariables.java © 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu

Introduction to Object-Oriented Programming with Java--Wu Parameters The formal parameters defined in the method header are local variables Formal parameters receive copies of the actual parameters passed in the call The values are copied in order, 1st to 1st, 2nd to 2nd, etc Ooodles … out to reality Parameters.java © 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu

Introduction to Object-Oriented Programming with Java--Wu Chapter 4 Sample Method private static double fromDollar(double dollar) { double amount, fee; fee = exchangeRate - feeRate; amount = dollar * fee; return amount; } Parameter Local Variables © 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Intro to OOP w/Java--Wu

Memory Allocation for Local Variables - 1 Chapter 4 Memory Allocation for Local Variables - 1 Code A private static double fromDollar( double dollar) { double amount, rate; rate = EXCHANGE_RATE - FEE_RATE; amount = dollar * rate; return(amount); } finalAmount = fromDollar(200); At before fromDollar A A. Local variables do not exist before the method execution State of Memory © 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Intro to OOP w/Java--Wu

Memory Allocation for Local Variables - 2 Chapter 4 Memory Allocation for Local Variables - 2 Code private static double fromDollar( double dollar) { double amount, rate; rate = EXCHANGE_RATE - FEE_RATE; amount = dollar * rate; return(amount); } B finalAmount = fromDollar(200); After is executed B dollar amount 200.0 rate B. Memory space is allocated for the local variables and parameter. State of Memory © 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Intro to OOP w/Java--Wu

Memory Allocation for Local Variables - 3 Chapter 4 Memory Allocation for Local Variables - 3 Code private static double fromDollar( double dollar) { double amount, rate; rate = EXCHANGE_RATE - FEE_RATE; amount = dollar * rate; return(amount); } finalAmount = fromDollar(200); C After is executed C dollar amount 200.0 rate 24846.3 124.2315 C. Computed values are assigned to the local variables. State of Memory © 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Intro to OOP w/Java--Wu

Memory Allocation for Local Variables - 4 Chapter 4 Memory Allocation for Local Variables - 4 Code private static double fromDollar( double dollar) { double amount, rate; rate = EXCHANGE_RATE - FEE_RATE; amount = dollar * rate; return(amount); } finalAmount = fromDollar(200); D At after fromDollar D D. Memory space is deallocated upon exiting the fromDollar method. State of Memory © 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Intro to OOP w/Java--Wu

Pass-By-Value Scheme - 1 Chapter 4 Pass-By-Value Scheme - 1 Code A x = 10; y = 20; myMethod( x, y ); private static void myMethod(int one, float two) { one = 25; two = 35.4f; } At before myMethod A x 10 A. Local variables do not exist before the method execution y 10 20 State of Memory © 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Intro to OOP w/Java--Wu

Pass-By-Value Scheme - 2 Chapter 4 Pass-By-Value Scheme - 2 Code x = 10; y = 20; myMethod( x, y ); private static void myMethod(int one, float two) { one = 25; two = 35.4f; } B Values are copied at B x 10 one 10 B. The values of arguments are copied to the parameters. y 10 20 two 10 20.0f State of Memory © 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Intro to OOP w/Java--Wu

Pass-By-Value Scheme - 3 Chapter 4 Pass-By-Value Scheme - 3 Code x = 10; y = 20; myMethod( x, y ); private static void myMethod(int one, float two) { one = 25; two = 35.4f; } C After is executed C x 10 y 20 one 25 two 35.4f C. The values of parameters are changed. State of Memory © 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Intro to OOP w/Java--Wu

Pass-By-Value Scheme - 4 Chapter 4 Pass-By-Value Scheme - 4 Code x = 10; y = 20; myMethod( x, y ); private static void myMethod(int one, float two) { one = 25; two = 35.4f; } D At after myMethod D x 10 D. Parameters are erased. Arguments remain unchanged. y 10 20 State of Memory © 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Intro to OOP w/Java--Wu

Introduction to Object-Oriented Programming with Java--Wu Actual Parameters Formal and actual parameters need not (but can) have the same name. Changes to the formal parameters have no effect in the calling code because they are local! Actual parameters need not be variables. Ack … out to reality NonVariableParameters.java © 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu

Arguments & Parameters: Points to Remember Chapter 4 Arguments & Parameters: Points to Remember Arguments are passed to a method using the pass-by-value scheme. Arguments are matched to the parameters from left to right. The data type of an argument must be assignment compatible to the data type of the matching parameter. The number of arguments in the method call must match the number of parameters in the method definition. Parameters and arguments do not have to have the same name. Local copies, which are distinct from arguments, are created even if the parameters and arguments share the same name. Parameters are input to a method, and they are local to the method. Changes made to the parameters will not affect the value of corresponding arguments. © 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Intro to OOP w/Java--Wu