Introduction to Method. Example Java Method ( 1 ) The 2 types (kinds) of methods in Java Class methods Instance methods Methods can do more work than.

Slides:



Advertisements
Similar presentations
User-Defined Functions Like short programs Can operate on their own data Can receive data from callers and return data to callers.
Advertisements

Week 4 – Functions Introduction. Functions: Purpose Breaking a large problem into a series of smaller problems is a common problem- solving technique.
BBS514 Structured Programming (Yapısal Programlama)1 Functions and Structured Programming.
Introduction to Computers and Programming Lecture 11: Introduction to Methods Professor: Evan Korth New York University.
ITEC200 – Week03 Inheritance and Class Hierarchies.
© Janice Regan Problem-Solving Process 1. State the Problem (Problem Specification) 2. Analyze the problem: outline solution requirements and design.
Methods in Java CSC 1401: Introduction to Programming with Java Week 7 – Lecture 2 Wanda M. Kunkle.
Math class methods & User defined methods Introduction to Computers and Programming in JAVA: V
CS 201 Functions Debzani Deb.
Top-Down Design with Functions 4 What do programmer’s (not programs!) use as input to the design of a program? –Documentation Problem definition Requirements.
Information Hiding and Encapsulation
Introduction to Computers and Programming Lecture 13: User defined methods Instructor: Evan Korth New York University.
© 2006 Pearson Addison-Wesley. All rights reserved2-1 Console Input Using the Scanner Class Starting with version 5.0, Java includes a class for doing.
1 CSC 1401 S1 Computer Programming I Hamid Harroud School of Science and Engineering, Akhawayn University
1 Interactive Applications (CLI) and Math Interactive Applications Command Line Interfaces The Math Class Example: Solving Quadratic Equations Example:
Chapter 9 Introduction to Procedures Dr. Ali Can Takinacı İstanbul Technical University Faculty of Naval Architecture and Ocean Engineering İstanbul -
CHAPTER 10 Recursion. 2 Recursive Thinking Recursion is a programming technique in which a method can call itself to solve a problem A recursive definition.
Defining Classes and Methods Chapter 4.1. Key Features of Objects An object has identity (it acts as a single whole). An object has state (it has various.
Introduction to Methods
DCT 1123 PROBLEM SOLVING & ALGORITHMS INTRODUCTION TO PROGRAMMING.
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
Expressions, Data Conversion, and Input
1 Web-Enabled Decision Support Systems Objects and Procedures Don McLaughlin IE 423 Design of Decision Support Systems (304)
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Lecture # 5 Methods and Classes. What is a Method 2 A method is a set of code which is referred to by name and can be called (invoked) at any point in.
Chapter 06 (Part I) Functions and an Introduction to Recursion.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Functions.
Major objective of this course is: Design and analysis of modern algorithms Different variants Accuracy Efficiency Comparing efficiencies Motivation thinking.
Functions, Procedures, and Abstraction Dr. José M. Reyes Álamo.
CPS120: Introduction to Computer Science Decision Making in Programs.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 5.
CPS120: Introduction to Computer Science Lecture 14 Functions.
Procedural programming in Java Methods, parameters and return values.
Algorithm Design.
Python Programming, 2/e1 Python Programming: An Introduction to Computer Science Chapter 6 Defining Functions.
1 CS161 Introduction to Computer Science Topic #9.
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.
Tower of Hanoi Puzzle Jianying Yu. I. Introduction The Puzzle: Conditions: n disks and three pegs. Conditions: n disks and three pegs. Goal: Move disks.
Chapter 3: Developing Class Methods Object-Oriented Program Development Using Java: A Class-Centered Approach.
Computing Higher – SD Unit - Topic 8 – Procedure and Standard Algorithms P Lynch, St Andrew’s High School Unit 2 Software Development Process Topic.
Functions  A Function is a self contained block of one or more statements or a sub program which is designed for a particular task is called functions.
Reading input from the console input. Java's console input The console is the terminal window that is running the Java program I.e., that's the terminal.
Introduction to Functions CSIS 1595: Fundamentals of Programming and Problem Solving 1.
Martin T. Press.  Main Method and Class Name  Printing To Screen  Scanner.
Methods.
CSIS 113A Lecture 5 Functions. Introduction to Functions  Building Blocks of Programs  Other terminology in other languages:  Procedures, subprograms,
Invoking methods in the Java library. Jargon: method invocation Terminology: Invoking a method = executing a method Other phrases with exactly the same.
Lecture #1: Introduction to Algorithms and Problem Solving Dr. Hmood Al-Dossari King Saud University Department of Computer Science 6 February 2012.
6. FUNCTIONS Rocky K. C. Chang October 4, 2015 (Adapted from John Zelle’s slides)
© FPT SOFTWARE – TRAINING MATERIAL – Internal use 04e-BM/NS/HDCV/FSOFT v2/3 Using Stored Procedures ADO.NET - Lesson 07  Training time: 15 minutes  Author:
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
Exam #1 You will have exactly 30 Mins to complete the exam.
3 Introduction to Classes and Objects.
Introduction to C++ Introduced by Bjarne Stroustrup of AT&T’s Bell Laboratories in mid-1980’s Based on C C++ extended C to support object-oriented programming.
About the Presentations
JavaScript: Functions
Divide and Conquer Methodology
Starting Out with Java: From Control Structures through Objects
Chapter 3 Introduction to Classes, Objects Methods and Strings
Functions, Procedures, and Abstraction
The method invocation mechanism and the System Stack
Objective of This Course
MSIS 655 Advanced Business Applications Programming
IFS410 Advanced Analysis and Design
Array and Method.
Recursion Chapter 11.
6 Methods: A Deeper Look.
Week 4 Lecture-2 Chapter 6 (Methods).
Teori Bahasa dan Automata Lecture 9: Contex-Free Grammars
Functions, Procedures, and Abstraction
Presentation transcript:

Introduction to Method

Example

Java Method ( 1 ) The 2 types (kinds) of methods in Java Class methods Instance methods Methods can do more work than statements: A statement can only accomplish a very small amount of work. A method contains multiple statements.

Java Method ( 2 ) Methods allows us to build up a library of useful tools You define (describe) a method once Afterwards, you can executed (invoked) the method as many times as you want. Therefore, we can build up a library of problem solving methods E.g., Math.sin(), Math.sqrt(), Scanner methods for input, etc.

Java Method ( 3 ) Methods allows us to solves a complex problem using the divide and conquer methodology In the divide and conquer problem solving technique 1. we break a complex problem/task into a number of smaller problems/tasks 2. Each of the smaller problem/task is then solved using one method.

Steps in using methods in a Java program ( 1 ) First, you must define the method. How to define a method: Write down the steps (= statements) contained in the method. Attach a name to the steps (= statements) Notes: You only need to define a method once (Remember that in Java, you must define the method inside some class.)

Steps in using methods in a Java program ( 2 ) After defining the method, you can then call a method using the name of the method ◦ When a method is called, the statements inside the corresponding method are executed ◦ When all statements in the method has been executed, the execution will resume at the program location of the method call This mechanism is called method invocation (an older term is procedure call) You can invoke a method as many times as you wish

Example Code(1) We have previously seen an algorithm to find the smaller of 2 values x and y

Example Code(2)

Example Code(3)

Example Code(4) A non-savvy user that wants to use the ToolBox.min method does not need to know the statements contained inside the method ToolBox.min !!! A non-savvy user will only need to know the following in order to use the method: 1. The (complete) name of the method (i.e.: ToolBox.min) 2. What information the method needs to do the task (i.e.: 2 numbers) 3. What information the method returns to the user (i.e.: 1 number)

Effect of a return statement: The return statement is used to terminate the execution of a method. When the program executes a return statement, the method terminates The execution will continue at the location where the method was called If a return statement returns an EXPRESSION, then the value (= result) of the EXPRESSION will be used to replace the method call at the point of call.

What happens in a method invocation: (1)

What happens in a method invocation: (2)

What happens in a method invocation: (3)

What happens in a method invocation: (4)

Defining a (class) method (1)

Defining a (class) method (2)

Defining a (class) method (3)

Multiple methods with the same name in the same class (1) When you use/invoke a method in your Java program, the Java compiler will use the following information to identify which method you want to use: The method name (i.e., ClassName.MethodName) The number and the type of the parameters that you specify in the method invocation.

Multiple methods with the same name in the same class (2)

Defining the min method inside the same class as the main method

A short hand for invoking a method defined inside the same class