Introduction to Computers and Programming Lecture 13: User defined methods Instructor: Evan Korth New York University.

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.
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.
Introduction to Computers and Programming Lecture 9: For Loops New York University.
Introduction to Computers and Programming Introduction to Methods in Java.
Introduction to Computers and Programming Lecture 4: Mathematical Operators New York University.
C Lecture Notes 1 Program Control (Cont...). C Lecture Notes 2 4.8The do / while Repetition Structure The do / while repetition structure –Similar to.
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.
COMP 14 Introduction to Programming Miguel A. Otaduy May 25, 2004.
Introduction to Computers and Programming for Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to.
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.
FunctionsFunctions Systems Programming. Systems Programming: Functions 2 Functions   Simple Function Example   Function Prototype and Declaration.
COMP 14 Introduction to Programming Mr. Joshua Stough February 28, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218.
 2007 Pearson Education, Inc. All rights reserved C Functions.
1 Introduction to Computers and Programming Quick Review What is a Function? A module of code that performs a specific job.
Introduction to Computers and Programming Lecture 11: Introduction to Methods Professor: Evan Korth New York University.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 8, 2005.
 2007 Pearson Education, Inc. All rights reserved C Functions.
Review for Midterm 2 Nested loops & Math class methods & User defined methods.
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. 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,
Introduction to Methods
FunctionsFunctions Systems Programming Concepts. Functions   Simple Function Example   Function Prototype and Declaration   Math Library Functions.
© 2007 Lawrenceville Press Slide 1 Chapter 7 Top-Down Development  Problem-solving approach  Breaking a task down into smaller subtasks  First level.
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.
METHODS Introduction to Systems Programming - COMP 1005, 1405 Instructor : Behnam Hajian
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. C How To Program - 4th edition Deitels Class 05 University.
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.
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.
Functions Top-down design Breaking a complex problem into smaller parts that we can understand is a common practice. The process of subdividing a problem.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
Procedural programming in Java Methods, parameters and return values.
© 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.
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.
User Defined Methods Methods are used to divide complicated programs into manageable pieces. There are predefined methods (methods that are already provided.
Functions Math library functions Function definition Function invocation Argument passing Scope of an variable Programming 1 DCT 1033.
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.
Chapter 5 Methods 1. Motivations Method : groups statements that perform a function.  Level of abstraction (black box)  Code Reuse – no need to reinvent.
 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.
Programming Fundamentals Enumerations and Functions.
CHAPTER 4 FUNCTIONS Dr. Shady Yehia Elmashad. Outline 1.Introduction 2.Program Components in C++ 3.Math Library Functions 4.Functions 5.Function Definitions.
CSCI 51 Introduction to Programming Dr. Joshua Stough February 24, 2009.
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.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 4.
Functions + Overloading + Scope
Methods Chapter 6.
Object Oriented Systems Lecture 03 Method
Deitel- C:How to Program (5ed)
Introduction to Methods
Functions.
Chapter 5 - Functions Outline 5.1 Introduction
Functions Declarations CSCI 230
Chapter 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.
Chapter 6: Methods CS1: Java Programming Colorado State University
Presentation transcript:

Introduction to Computers and Programming Lecture 13: User defined methods Instructor: Evan Korth New York University

Road Map Method abstraction Divide and conquer Examine parameters Examine return types Argument promotion Reading: , part 4.4, 4.7

review What information can you learn about a method from its header? What is the difference between a formal parameter and an actual parameter? What does Math.random() return?

Benefits of Methods Experience has shown that the best way to develop and maintain large programs is to build it from smaller components or modules. In Java, one such module is the method. Each module is generally simpler and more manageable than the entire program. This concept is known as Divide and Conquer, also with Abstraction in the mix.

Divide and Conquer Divide and Conquer: break large programs into a series of smaller methods. –Helps manage complexity –Makes it easier to build large programs –Makes is easier to debug programs

Abstraction Abstraction: most of the time, you need to know what a method does, but not how it actually does it. –Also helps manage complexity. –You can use other people’s code, without understanding how it works. If you have code that would appear more than once, put it in a method and call that method multiple times –Reduces length and complexity of code –Reduces likelihood of errors in code

 2000 Prentice Hall, Inc. All rights reserved. 7 User defined Method Declarations General format of method declaration: modifiers return-value-type method-name ( parameter1, …, parameterN ) { declarations and statements } Method can also return values: return expression ;

Naming your methods As with variables naming methods is important You should give your methods names which clearly describe what the function is doing –helps debugging –helps others read your code Same rules as naming variables –E.g. public static float calculateTax( int sale ) When you write about a method in an explanation use the parenthesis to indicate you are referencing a method (as opposed to a regular variable): –E.g. //call squareInteger() to calculate the square

Good programming with methods A method should do one and only one useful action –If you see names for your method that suggest multiple actions then it’s time to break it up into separate functions; for example, calculateTaxAndPrintReturnAndSaveFile(); -ugh If you do something more than once in a program, you should write a method for that action.

More Good Programming If you have written a method to do something in one project, and you need to do the same action in another project, you should reuse the method. –In Java this is usually accomplished by using classes which we will not cover this semester.

Return Value Types You can only return one value from a method. Returning void –void : means nothing –A method that returns void therefore returns nothing. –Hence, there is no need for the optional return statement. But using one can force early exit from the method. –Example: public static void printIntro (int n);

Parameter Data Types Unlike return values, you can pass as many parameters as you like. To pass more than one parameter, you need to separate the parameters with commas. public static int maximum (int x, int y) { /*body*/ }

Warning Unlike declaring variables, you must specifically state the type for multiple variables –For example takeTwoFloats( float x, y ) is incorrect –Instead you must write takeTwoFloats(float x, float y)

No parameters You can also have a method that accepts no parameters. In such case, you would just have an empty parameter list. E.g. public static int rollDie () public static void printIntro ()

 2000 Prentice Hall, Inc. All rights reserved 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

 2000 Prentice Hall, Inc. All rights reserved. 16

Exercise 5.24 Implement the following methods –Method convertToCelsius returns the Celsius equivalent of a Fahrenheit temperature (( 5.0 / 9.0 * ( fTemp - 32 ) ) –Method convertToFahrenheit returns the Fehrenheit equivalent of a Celsius temperature ( 9.0 / 5.0 * cTemp + 32 ) –Use these methods to write a program that prints charts showing the Fahrenheit equivalent of all Celsius temperatures from 0 to 100 degrees, and the Celsius equivalents of all Fahrenheit temperatures from 32 to 212 degrees. Print the output in a neat tabular format.  2000 Prentice Hall, Inc. All rights reserved.

Exercise 2 Write a method that will print a line of stars. The method should take one parameter which will determine the number of stars to print. Write a main() method which calls the star printing method 5 times using random numbers from