Introduction Modules Small pieces of a problem ▴ e.g., divide and conquer Facilitate design, implementation, operation and maintenance of large programs.

Slides:



Advertisements
Similar presentations
JavaScript Part for Repetition Statement for statement Cpecifies each of the items needed for counter-controlled repetition with a control variable.
Advertisements

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.
Introduction to Computers and Programming Introduction to Methods in Java.
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.
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.
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.
Outline IS400: Development of Business Applications on the Internet Fall 2004 Instructor: Dr. Boris Jukic JavaScript: Functions Part I.
 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.
 2004 Prentice Hall, Inc. All rights reserved. 1 Chapter 10 - JavaScript: Functions Outline 10.1 Introduction 10.2 Program Modules in JavaScript 10.3.
1 Lecture 3 Part 1 Functions with math and randomness.
Methods Chapter 6. 2 Program Modules in Java What we call "functions" in C++ are called "methods" in Java Purpose Reuse code Modularize the program This.
Dale Roberts Procedural Programming using Java Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer and.
© 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.
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.
 2005 Pearson Education, Inc. All rights reserved Methods: A Deeper Look.
Part II © Copyright by Pearson Education, Inc. All Rights Reserved.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 6 September 17, 2009.
Chapter 5 - Functions Outline 5.1Introduction 5.2Program Modules in C 5.3Math Library Functions 5.4Functions 5.5Function Definitions 5.6Function Prototypes.
 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.
 Pearson Education, Inc. All rights reserved Methods: A Deeper Look.
© 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.
Java™ How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Java™ How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Using Java MINISTRY OF EDUCATION & HIGHER EDUCATION COLLEGE OF SCIENCE AND TECHNOLOGY KHANYOUNIS- PALESTINE Lecture 15,16 Java’s 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.
Methods Chapter 6. 2 Program Modules in Java What we call "functions" in C++ are called "___________________" in Java Purpose –Reuse code –Modularize.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 6 – Methods Part I.
 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.
 Static  Example for Static Field  Example for Static Method  Math class methods  Casting  Scope of Declaration  Method Overloading  Constructor.
Part III © Copyright by Pearson Education, Inc. All Rights Reserved.
CHAPTER 4 FUNCTIONS Dr. Shady Yehia Elmashad. Outline 1.Introduction 2.Program Components in C++ 3.Math Library Functions 4.Functions 5.Function Definitions.
(C) 2010 Pearson Education, Inc. All rights reserved.  Best way to develop and maintain a large program is to construct it from small, simple pieces,
 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.
Functions Course conducted by: Md.Raihan ul Masood
Functions.
Methods Chapter 6.
Programming Fundamentals Lecture #7 Functions
Deitel- C:How to Program (5ed)
Introduction to Methods
Chapter 5 - Functions Outline 5.1 Introduction
Chapter 5 - Functions Outline 5.1 Introduction
Chapter 10 - JavaScript: Functions
Chapter 6 Methods: A Deeper Look
Chapter 5 - Functions Outline 5.1 Introduction
Chapter 6 Methods: A Deeper Look
Chapter 6 - Functions Outline 5.1 Introduction
6 Methods: A Deeper Look.
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.
6 Functions.
Presentation transcript:

Introduction Modules Small pieces of a problem ▴ e.g., divide and conquer Facilitate design, implementation, operation and maintenance of large programs Promote top-down programming Promote hierachical design Allow programmers to modularize programs ▴ Makes program development more manageable ▴ Software reusability ▴ Avoid repeating code

Program Modules in Java Modules in Java Methods Classes Java API provides several modules Programmers can also create 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

Hierarchical Design boss worker1worker2worker3 worker4worker5 Fig. 6.1 Hierarchical boss-method/worker-method relationship.

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

Math -Class Methods

Method Declarations Methods Local variables ▴ Declared in method declaration Parameters ▴ Communicates information between methods via method calls Programmers can write customized methods General format of method declaration: return-value-type method-name ( parameter1, parameter2, …, parameterN ) { declarations and statements } Method can also return values: return expression ;

Figure 6.3: MaximumFinder.java

Figure 6.4: MaximumFinderTest.java

Calling Methods There are three ways to call a method Using method name to call a method in the same class Using the dot (.) together with the name of a reference to an another object and the method name ▴ input.nextInt() Using the dot (.) together with the name of a class and the name of a static method ▴ Math.sin(x) Methods either return a value, which can be ignored, or have a return type of void Every time a method is called an activation record is made and placed on the run-time stack Each activation record hold local variables and parameters Methods can call themselves (recursion)

Argument Promotion Coercion of arguments Forcing arguments to appropriate type to pass to method ▴ e.g., System.out.println( Math.sqrt( 4 ) ); Evaluates Math.sqrt( 4 ) Then evaluates System.out.println() Promotion rules Specify how to convert types without data loss

Argument Promotion

Java API Packages Packages Classes grouped into categories of related classes Promotes software reuse import statements specify classes used in Java programs ▴ e.g., import Java.util.Scanner; ▴ See the Java API for a complete list

Commonly Used Java API Packages

Random-Number Generation Java random-number generators Math.random() ▴ ( int ) ( Math.random() * 6 ) Produces integers from Use a seed for different random-number sequences

Figure 6.7: RandomIntegers.java

Figure 6.8: RollDie.java

Craps Craps simulation Roll dice first time ▴ If sum equals 7 or 11, the player wins ▴ If sum equals 2, 3 or 12, the player loses ▴ Any other sum (4, 5, 6, 8, 9, 10) is that player’s point Keep rolling dice until… ▴ Sum matches player point Player wins ▴ Sum equals 7 Player loses

Figure 6.9, 6.10: Craps.java

Scope of Declarations Scope Portion of the program that can reference an entity by its name Basic scope rules ▴ Scope of a parameter declaration ▴ Scope of a local-variable declaration ▴ Scope of a label in a labeled break or continue statement ▴ Scope of a local-variable declaration that appears in the initialization section of a for statement’s header ▴ Scope of a method or field of a class

Figure 6.11: Scoping.java

Method Overloading Method overloading Several methods of the same name Different parameter set for each method ▴ Number of parameters ▴ Parameter types

Figure 6.13: MethodOverload.java