Methods and Parameters Chapter 5 How to split large programs into isolated sections. Focus on one part of the problem – which can be “called” on or “invoked”

Slides:



Advertisements
Similar presentations
Classes & Objects INTRODUCTION : This chapter introduces classes ; explains data hiding, abstraction & encapsulation and shows how a class implements these.
Advertisements

Spring Semester 2013 Lecture 5
Pass by Value. COMP104 Pass by Value / Slide 2 Passing Parameters by Value * A function returns a single result (assuming the function is not a void function)
Chapter 8 Scope, Lifetime and More on Functions. Definitions Scope –The region of program code where it is legal to reference (use) an identifier Three.
TOPIC 12 CREATING CLASSES PART 1 1 Notes adapted from Introduction to Computing and Programming with Java: A Multimedia Approach by M. Guzdial and B. Ericson,
1 Classes, Encapsulation, Methods and Constructors (Continued) Class definitions Instance data Encapsulation and Java modifiers Method declaration and.
PASSING PARAMETERS 1. 2 Parameter Passing (by Value) Parameters Formal Parameters – parameters listed in the header of the function Variables used within.
Memory Management & Method Calls in Java Program Execution © Allan C. Milne v
Constructors & An Introduction to Methods. Defining Constructor – Car Example Public class car { String Model; double speed; String colour; { Public Car.
Translate, Rotate, Matrix Pages Function Function Definition Calling a function Parameters Return type and return statement.
1 Chapter Three Using Methods. 2 Objectives Learn how to write methods with no arguments and no return value Learn about implementation hiding and how.
Chapter 4: Writing Classes
Chapter 3 Implementing Classes. Instance Variables Instance variables store the data of an object; the fields of an object. Instance of a class: an object.
Overloading methods review When is the return statement required? What do the following method headers tell us? public static int max (int a, int b)
COMP 14 Introduction to Programming Miguel A. Otaduy May 25, 2004.
Week 4 Recap CSE 115 Fall 2006 Section C. Decoupling Separation of concerns Defining what an object can do, not how it does it.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 8, 2005.
Chapter 4: Writing Classes Presentation slides for Java Software Solutions Foundations of Program Design Third Edition by John Lewis and William Loftus.
Java Methods By J. W. Rider. Java Methods Modularity Declaring methods –Header, signature, prototype Static Void Local variables –this Return Reentrancy.
Chapter 4: Writing Classes Presentation slides for Java Software Solutions for AP* Computer Science by John Lewis, William Loftus, and Cara Cocking Java.
C++ for Engineers and Scientists Third Edition
Writing Classes (Chapter 4)
Java Programming, 3e Concepts and Techniques Chapter 3 Section 65 – Manipulating Data Using Methods – Java Applet.
TCU CoSc Introduction to Programming (with Java) Variables and Methods.
Object Oriented Programming … and other things you need to program in java.
© 2006 Pearson Education Chapter 4: Writing Classes Presentation slides for Java Software Solutions for AP* Computer Science A 2nd Edition by John Lewis,
Writing Methods Mrs. C. Furman October 9, Drawing a Square World worldObj = new World(); Turtle turtle1 = new Turtle(100, 100, worldObj); turtle1.forward.
Chapter 4 -2 part Writing Classes 5 TH EDITION Lewis & Loftus java Software Solutions Foundations of Program Design © 2007 Pearson Addison-Wesley. All.
Chapter 12 1 TOPIC 13B l Buttons and Action Listeners Window Interfaces Using Swing Objects.
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.
“Education is a Treasure that follows you everywhere.” – Chines Proverb Methods and Functions.
Functions in C CSE 2451 Rong Shi. Functions Why use functions? – Reusability Same operation, different data – Abstraction Only need to know how to call.
Top-down approach / Stepwise Refinement & Procedures & Functions.
1 Chapter 6 Methods. 2 Motivation Find the sum of integers from 1 to 10, from 20 to 30, and from 35 to 45, respectively.
CSC 143F 1 CSC 143 Constructors Revisited. CSC 143F 2 Constructors In C++, the constructor is a special function automatically called when a class instance.
CSI 3125, Preliminaries, page 1 Generic Class & Generic methods.
Chapter 6 Methods Chapter 6 - Methods.
Methods Chapter 6. 2 Program Modules in Java What we call "functions" in C++ are called "___________________" in Java Purpose –Reuse code –Modularize.
Chapter 5 Methods 1. Motivations Method : groups statements that perform a function.  Level of abstraction (black box)  Code Reuse – no need to reinvent.
 Static  Example for Static Field  Example for Static Method  Math class methods  Casting  Scope of Declaration  Method Overloading  Constructor.
Classes - Intermediate
AP Java Ch. 4 Review Question 1  Java methods can return only primitive types (int, double, boolean, etc).
Methods.
Part III © Copyright by Pearson Education, Inc. All Rights Reserved.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 6A Methods (Concepts)
Building Programs from Existing Information Solutions for programs often can be developed from previously solved problems. Data requirements and solution.
1 Sections 6.4 – 6.5 Methods and Variables Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
Arrays Chapter 7.
Functions + Overloading + Scope
Chapter 4: Writing Classes
Chapter 4: Writing Classes
Intro To Classes Review
Chapter 5 Functions DDC 2133 Programming II.
Chapter 4: Writing Classes
Methods.
Method Mark and Lyubo.
Classes, Encapsulation, Methods and Constructors (Continued)
CSE 115 September 29 – October 3, 2008.
CHAPTER 6 GENERAL-PURPOSE METHODS
Chapter 4 Writing Classes.
CS2011 Introduction to Programming I Methods (II)
CSE 115 September 29 – October 3, 2008.
Chapter 6 Methods.
Chapter 9: Value-Returning Functions
Functions Imran Rashid CTO at ManiWeber Technologies.
Unit-1 Introduction to Java
Corresponds with Chapter 5
10.2 Procedures Passing Parameters 30/08/2019.
Chapter 4 Test Review First day
Presentation transcript:

Methods and Parameters Chapter 5 How to split large programs into isolated sections. Focus on one part of the problem – which can be “called” on or “invoked” by name

paper.drawRect (10, 20, 60, 60); drawRect is called and passed a starting x,y value and an ending x,y value

Writing your own methods Page “LogoMethod” program private void drawLogo (Graphics drawingArea, int xPos, int yPos) { –Method header for drawLogo method –Formal parameter list inside parenthesis Private method may be called by another method in the same class Void = drawLoge returns nothing, other methods may return any type. Body of method will be enclosed by { }

Calling a method drawLogo ( paper, 10, 20); Call must include name of method and arguments (in correct order, number, type) –If any are required

Formal vs. Actual parameters Formal parameters in list –drawLogo (Graphics drawingArea, int xPos, int yPos) –drawingArea, xPos and yPos are formal Actual parameters are given in call… –drawLogo ( paper, 10, 20); –Paper, 10 and 20 are Actual

Trianglemethod Page Page 70 drawTriangle2 (80, 100) (80, )( , ) W = 60 H = 70

Scope local vs. class Pages Name Clashes x, y, z may exist locally in any method, even if they are declared as different types Java treats them a different

Event handling Called by Java run time system main called automatically actionPerformed called when buttons clicked (or another even occurs) A return sends a result back to the rest of the program. Page 73 program “AreaMethod” returns a value “area” to the calling method answer = areaRectangle ( 30, 40);

“drawHouse” page Using drawtriangle and drawRectangle to draw a house

actionPerformed calls drawHouse drawHouse calls drawRect drawHouse calls drawTriangle drawTriangle calls drawLine areaHouse calls areaRectanglr and areaTriangle adds the results, returns the sum.

overloading areaTriangle has an int and a double version based upon the call (int or double) Java decides which to use by type passed in the call