Introduction to Computers and Programming Introduction to Methods in Java.

Slides:



Advertisements
Similar presentations
Procedural programming in Java
Advertisements

Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
JavaScript Part for Repetition Statement for statement Cpecifies each of the items needed for counter-controlled repetition with a control variable.
PAC Introduction to Methods Professor: Evan Korth New York University.
Introduction to Computers and Programming Lecture 11: Introduction to Methods Professor: Evan Korth New York University.
1 Lecture 4: Chapter 6 - Methods Outline Introduction Program Modules in Java Math -Class Methods Method Declarations Java API Packages Random-Number Generation.
Chapter 5 C Functions The best way to develop and maintain a large program is to divide it into several smaller program modules, each of which is more.
Functions Quick Review What is a Function? A module of code that performs a specific job. Examples: Function that determines the maximum of two numbers.
C Lecture Notes 1 Program Control (Cont...). C Lecture Notes 2 4.8The do / while Repetition Structure The do / while repetition structure –Similar to.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program Modules in C 5.3Math Library Functions 5.4Functions.
Introduction to Computers and Programming Lecture 12: Math.random() Professor: Evan Korth New York University.
Math class methods & User defined methods Introduction to Computers and Programming in JAVA: V
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
Introduction to Computers and Programming Lecture 16: Arrays (cont) Professor: Evan Korth New York University.
Math class methods & User defined methods Math class methods Math.sqrt(4.0) Math.random() java.lang is the library/package that provides Math class methods.
 2007 Pearson Education, Inc. All rights reserved C Functions.
Introduction to Computers and Programming Lecture 11: Introduction to Methods Professor: Evan Korth New York University.
 2007 Pearson Education, Inc. All rights reserved C Functions.
1 MATERI PENDUKUNG METHOD Matakuliah: M0074/PROGRAMMING II Tahun: 2005 Versi: 1/0.
Review for Midterm 2 Nested loops & Math class methods & User defined methods.
Introduction to Computers and Programming Lecture 13: User defined methods Instructor: Evan Korth New York University.
 2003 Prentice Hall, Inc. All rights reserved. 1 Functions Modules: functions and classes Programs use new and “prepackaged” modules –New: programmer-defined.
 2003 Prentice Hall, Inc. All rights reserved Introduction Modules –Small pieces of a problem e.g., divide and conquer –Facilitate design, implementation,
 2000 Prentice Hall, Inc. All rights reserved. Functions in C Outline 1Introduction 2Program Modules in C 3Math Library Functions 4Functions 5Function.
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.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. C How To Program - 4th edition Deitels Class 05 University.
 2007 Pearson Education, Inc. All rights reserved C Functions.
Functions in C Outline 1Introduction 2Program Modules in C 3Math Library Functions 4Functions 5Function Definitions 6Function Prototypes 7Header Files.
Chapter 06 (Part I) Functions and an Introduction to Recursion.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Functions.
1 Introduction Modules  Most computer programs solve much larger problem than the examples in last sessions.  The problem is more manageable and easy.
 2005 Pearson Education, Inc. All rights reserved. 1 Methods Called functions or procedures in other languages Modularize programs by separating its tasks.
Introduction to Computers and Programming Lecture 14: User defined methods (cont) Professor: Evan Korth New York University.
Programming in C++ Language ( ) Lecture 5: Functions-Part1 Dr. Lubna Badri.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 6 September 17, 2009.
CPSC 230 Computers and Programming I Spring 2003 Dr. Lynn Lambert.
Chapter 5 - Functions Outline 5.1Introduction 5.2Program Modules in C 5.3Math Library Functions 5.4Functions 5.5Function Definitions 5.6Function Prototypes.
Procedural programming in Java Methods, parameters and return values.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 6 - Functions.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Functions Outline 5.1Introduction 5.2Program Modules.
C++ Programming Lecture 9 Functions – Part I By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
KIC/Computer Programming & Problem Solving 1.  Introduction  Program Modules in C  Math Library Functions  Functions  Function Definitions  Function.
TMC1414/TMC1413 I NTRODUCTION T O P ROGRAMMING Lecture 06 Function.
Java™ How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 5 Methods.
 2000 Prentice Hall, Inc. All rights reserved. 5.2Program Modules in C Functions –Modules in C –Programs combine user-defined functions with library functions.
Review for Nested loops & Math class methods & User defined methods.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 6 - Methods Outline 6.1 Introduction 6.2 Program Modules in Java 6.3 Math -Class Methods 6.4.
Introduction Modules Small pieces of a problem ▴ e.g., divide and conquer Facilitate design, implementation, operation and maintenance of large programs.
CHAPTER 4 FUNCTIONS Dr. Shady Yehia Elmashad. Outline 1.Introduction 2.Program Components in C++ 3.Math Library Functions 4.Functions 5.Function Definitions.
Dale Roberts CSCI N305 Functions Declarations Department of Computer and Information Science, School of Science, IUPUI.
 2000 Prentice Hall, Inc. All rights reserved Program Components in C++ Function definitions –Only written once –These statements are hidden from.
 Pearson Education, Inc. All rights reserved Methods: A Deeper Look.
Dr. Shady Yehia Elmashad
Programming Fundamentals Lecture #7 Functions
CSC113: Computer Programming (Theory = 03, Lab = 01)
Deitel- C:How to Program (5ed)
Introduction to Methods
Dr. Shady Yehia Elmashad
Chapter 5 - Functions Outline 5.1 Introduction
Chapter 5 - Functions Outline 5.1 Introduction
Functions.
Chapter 6 Methods: A Deeper Look
Chapter 5 - Functions Outline 5.1 Introduction
Dr. Shady Yehia Elmashad
Functions Declarations CSCI 230
Chapter 6 Methods: A Deeper Look
MSIS 655 Advanced Business Applications Programming
Java Methods: A Deeper Look Academic 2019 Class: BIT23/BCS10 Chapter 06 Abdulaziz Yasin Nageye Faculty of Computing Java How to Program, 10/e 1 © Co py.
Presentation transcript:

Introduction to Computers and Programming Introduction to Methods in Java

Road Map Introduction to Methods –Divide and Conquer –Abstraction Using Pre-Packaged Modules –Understanding Method signatures –Understanding java.lang.math methods Creating your own methods Reading: – Chapter 4: Sections 4.1 – 4.3; – 4.8.3

Introduction to Methods

 2003 Prentice Hall, Inc. All rights reserved. Modified by Sana Odeh Introduction Modules –Small pieces of a problem e.g., divide and conquer –Facilitate design, implementation, operation and maintenance of large programs

 2003 Prentice Hall, Inc. All rights reserved. Modified by Sana Odeh Program Modules in Java Modules in Java –Methods (based on functions in procedural programming) –Classes (object oriented programming) Java API provides several modules that are already defined in java “system defined” –Math.random() Programmers can also create their own modules –e.g., programmer-defined methods Methods –Invoked by a method call –Returns a result to calling method (caller) –Similar to a boss (caller) asking a worker (called method) to complete a task. The boss method delegates certain jobs to specific methods.

 2003 Prentice Hall, Inc. All rights reserved. Modified by Sana Odeh 6 Fig. 6.1 Hierarchical boss-method/worker-method relationship. boss worker1worker2worker3 worker4worker5

Examples of methods A method that determines the maximum of two numbers. A method that sorts a list of names A method that opens a file from the file system. A method that reads from the open file. A method that opens a new socket to the internet. A method that reads from that socket.

Important concept #1 Divide and Conquer: Break large programs into a series of smaller modules –Helps manage complexity –Makes it easier to build large programs –Makes it easier to debug programs

Important concept #2 Methods provide: –Abstraction (information hiding): – Most of the time, you need to know what a function does, but not how it actually does it. –Also helps manage complexity –You use other people’s code without knowing how it does it’s job.

Using Static methods in the Java API

 2003 Prentice Hall, Inc. All rights reserved. Modified by Sana Odeh Math -Class Methods Class java.lang.Math –Provides common mathematical calculations –Calculate the square root of : Math.sqrt( ) –Method sqrt belongs to class Math Dot (. ) allows access to method sqrt –The argument is located inside parentheses Class Method which belongs to class Math This always performs actions. Parameters or data passed to method to perform an action

 2003 Prentice Hall, Inc. All rights reserved. Modified by Sana Odeh 12

 2003 Prentice Hall, Inc. All rights reserved. Modified by Sana Odeh Methods Declarations Methods –Allow programmers to modularize programs Makes program development more manageable Software reusability Avoid repeating code –Local variables Declared and used in method declaration (its scope is the method only) –Parameters or arguments Communicates information between methods via method calls

 2003 Prentice Hall, Inc. All rights reserved. Modified by Sana Odeh Method Declarations (Cont.) Programmers can write customized methods

Line 9: method call to square line 16: header for method square. States that we have a method that accepts one integer as a parameter and returns one integer line 18: returns the value y * y 1// Fig. 6.3: SquareIntegers.java 2// Creating and using a programmer-defined method. 3public class SquareIntegers { 4public static void main (String args[]) 5{ 6int result; // store result of call to method square 7// loop 10 times 8for ( int counter = 1; counter <= 10; counter++ ) { 9result = square( counter ); // method call 10// print the result of one call to the method 11System.out.println ("The square of " + counter + " is " + 12result ); 13} // end for 14} // end method main() 15// square method declaration 16public static int square( int y ) 17{ 18return y * y; // return square of y 19} // end method square 20} // end class SquareIntegers Method square returns int that result stores Modified by Evan Korth  2003 Prentice Hall, Inc. All rights reserved. Method square returns the square of y y is the parameter of method square

 2003 Prentice Hall, Inc. All rights reserved. Modified by Sana Odeh Method Declarations (cont.) General format of method declaration: modifiers return-value-type method-name ( parameter1, …, parameterN ) { declarations and statements } Method can also return values: return expression ;