Chapter 5 Methods to our madness. Review Identifiers are names used for variables, constants, methods, classes, and packages. Must start with a letter,

Slides:



Advertisements
Similar presentations
Chapter 4 Methods F Introducing Methods –Benefits of methods, Declaring Methods, and Calling Methods F Passing Parameters –Pass by Value F Overloading.
Advertisements

Programmer-defined classes Part 2. Topics Returning objects from methods The this keyword Overloading methods Class methods Packaging classes Javadoc.
BBS514 Structured Programming (Yapısal Programlama)1 Functions and Structured Programming.
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
Chapter 5 Functions.
Chapter 4 Methods F Introducing Methods –Benefits of methods, Declaring Methods, and Calling Methods F Passing Parameters –Pass by Value F Overloading.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 5 Methods.
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
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 110 Introduction to Programming Mr. Joshua Stough October 8, 2007.
Introduction to Java Programming, 4E Y. Daniel Liang.
 2003 Prentice Hall, Inc. All rights reserved. 1 Functions Modules: functions and classes Programs use new and “prepackaged” modules –New: programmer-defined.
Chapter 4 Methods F Introducing Methods –Benefits of methods, Declaring Methods, and Calling Methods F Passing Parameters –Pass by Value F Overloading.
1 Chapter 5 Methods. 2 Introducing Methods A method is a collection of statements that are grouped together to perform an operation.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 5 Methods.
1 Topic 04 Methods Programming II/A CMC2522 / CIM2561 Bavy Li.
©2004 Brooks/Cole Chapter 6 Methods and Scope. Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 A Method Can Be Viewed as a Black Box To use a.
1 Functions Modules: functions and classes Programs use new and “prepackaged” modules –New: programmer-defined functions, classes –Prepackaged: from the.
 2000 Prentice Hall, Inc. All rights reserved. Functions in C Outline 1Introduction 2Program Modules in C 3Math Library Functions 4Functions 5Function.
C++ Functions. 2 Agenda What is a function? What is a function? Types of C++ functions: Types of C++ functions: Standard functions Standard functions.
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.
Functions in C Outline 1Introduction 2Program Modules in C 3Math Library Functions 4Functions 5Function Definitions 6Function Prototypes 7Header Files.
Chapter 4 Methods F Introducing Methods –Benefits of methods, Declaring Methods, and Calling Methods F Passing Parameters –Pass by Value F Overloading.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition, Fifth Edition Chapter 7: User-Defined Functions II.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 5 Methods.
1 Introducing Methods A method is a collection of statements that are grouped together to perform an operation.
Chapter 5: Methods 1. Objectives To declare methods, invoke methods, and pass arguments to a method (§ ). To use method overloading and know ambiguous.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Method Abstraction You can.
Chapter 4 Methods F Introducing Methods F Declaring Methods F Calling Methods F Passing Parameters F Pass by Value F Overloading Methods F Method Abstraction.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 4 Mathematical Functions, Characters,
Lecture 5 Methods. Sometimes we want to perform the same sequence of operations multiple times in a program. While loops allow us to do this, they are.
1 Chapter 6 Methods. 2 Objectives F To declare methods, invoke methods, and pass arguments to a method. F To use method overloading and know ambiguous.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 5 Methods.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 5 Methods.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 4 Methods Chapter.
C++ Programming Lecture 9 Functions – Part I By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Java™ How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
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.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 2A Reading, Processing and Displaying Data (Concepts)
1 Java Library Lecture 9 by Dr. Norazah Yusof. 2 Java Library Java has pre-defined classes that consist of the basic language classes in Java (organized.
1 Chapter 6 Methods. 2 Objectives F To declare methods, invoke methods, and pass arguments to a method. F To use method overloading and know ambiguous.
Chapter 5 Methods 1. Motivations Method : groups statements that perform a function.  Level of abstraction (black box)  Code Reuse – no need to reinvent.
Chapter 7 - Functions. Functions u Code group that performs single task u Specification refers to what goes into and out of function u Design refers to.
Lecture 5: java.lang.Math, java.lang.String and Characters Michael Hsu CSULA.
Chapter 5 Methods F Introducing Methods F Declaring Methods F Calling Methods F Passing Parameters F Pass by Value F Overloading Methods F Method Abstraction.
Chapter 4 Mathematical Functions, Characters, and Strings 1.
Chapter 4 Mathematical Functions, Characters, and Strings
Chapter 7: User-Defined Functions II
Java Primer 1: Types, Classes and Operators
Chapter 4 Mathematical Functions, Characters, and Strings
Chapter 5 Methods.
Chapter 6 Methods 1.
Chapter 3 Methods.
Chapter 5 – Part 2 Methods Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved
Chapter 4: Mathematical Functions, Characters, and Strings
Chapter 6 Methods: A Deeper Look
METHODS (FUNCTIONS) By: Lohani Adeeb khan.
Group Status Project Status.
Chapter 4 Writing Classes.
Chapter 6 Methods.
Chapter 5 Methods.
Chapter 5 Methods Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved
CSE 142 Lecture Notes Global Constants, Parameters, Return Values
Chapter 4 Methods Introducing Methods Declaring Methods
Functions in C Math Library Functions Functions Function Definitions
Ch 5 : Mathematical Functions, Characters, and Strings
Presentation transcript:

Chapter 5 Methods to our madness

Review Identifiers are names used for variables, constants, methods, classes, and packages. Must start with a letter, ‘_’, or ‘$’ character Additional characters can be letters, ‘_’, ‘$’, and digits. Cannot be a reserved word Cannot be “true,” “false,” or “null” Identifiers can be any length

Suppose we want to add three ranges of integers?

We can repeat the code three times, changing the values for each copy

This approach has four drawbacks Copying and modifying code blocks takes (hopefully) valuable time for a repetitive task. Compiling repetitive code blocks makes the program larger on disk and in memory and reduces locality for caching. Every instance of the code has to be debugged and tested separately. Repeating code blocks over and over makes the program harder to understand (maintain).

Defining methods solves the problems Methods define a block of code to perform a specific task using a set of parameters. The method can be written once and debugged, tested, and maintained in one place. Changes can be Partitioning programs into methods makes understanding the processing easier. Methods save space and improve locality

Defining a method modifiers return_value_type method_name (parameters) { statements }

Calling a method Write a statement comprised of the method name with actual parameters inside the parentheses and separated by commas

The method signature The method name and the parameter list constitute the method signature. The return type is not part of the method signature and cannot be used to distinguish methods. This will be important later when we learn about polymorphism and inheritance.

Sounding like a pro Java has methods; other languages have functions and procedures. We define methods and declare variables Correct terminology is a social convention, but it matters a lot to some people and it will help your career if you use the accepted terms

Call stack example Make intelligent comments here

Java is a “call by value” language A copy of the value is passed as a parameter The value outside the method is not affected by any operation inside the method

Example of “call by value”

Modularization/Encapsulation Improves design by isolating components and algorithms into circumscribed modules Simplifies maintenance by confining debugging, testing, and trouble-shooting to a defined, independent module Allows re-using the code in other programs

Overloading methods An overloaded methods have the same method name, but different parameters Different parameters can be different types or different number of parameters Overloaded methods are not distinguished by different return values.

Scope Scope refers to where in the program a variable can be referenced Generally, variables can be referenced anywhere in the block of code they are declared in Method parameters are variables within the method

Examples of scope

The Math class methods double sin(double radians); // sine double cos(double radians); // cosine double tan(double radians); // tangent double asin(double radians); // arcsine double acos(double radians); // arccosine double atan(double radians); // arctangent

More Math class methods double toRadians(double degrees); // converts from degrees to radians double toDegrees(double radians); // converts from radians to degrees

Exponential Math class methods

Rounding methods in Math class double ceil(double x);// round up double floor(double x);// round down double rint(double x); // note caveat int round(float x); long round(double x);

The min, max, and abs methods A family of overloaded methods for any combination of int, long, float, and double Math.min(a, b); // minimum of a and b Math.max(a, b); // maximum of a and b Math.abs(x); // absolute value of x

Random numbers Math.random() returns a double value in the range 0.0 <= x < 1.0 The formula a + Math.random() * b generates numbers in the range a <= x < (a+b) Do not rely on built-in random number generators for serious numerical processing applications—use special libraries

Program Design Top-down and bottom-up Stubbing out modules Coding and testing modules

The Luhn Algorithm Counting from the rightmost digit and moving left, double the value of every second digit. Sum the digits of the products (e.g., 10: = 1, 14: = 5) together with the undoubled digits from the original number. If the total modulo 10 is equal to 0 (if the total ends in zero) then the number is valid according to the Luhn formula; else it is not valid.modulo

Example from Wikipedia Account number " " will have a check digit added, making it of the form x: The yellow boxes are the doubled digits; sum the digits if doubled value is greater than 9

Check digit To make x a valid credit card, the sum needs to be an even multiple of 10. Since the sum is 67, the ‘x’ digit should be a 3 to make the sum equal to 70.

Lab 1 Write a program that accepts a credit card number and prints a message indicating whether the number is valid or invalid using the Luhn algorithm. Extra credit: determine the type of credit card for five different issuers and print the name of the credit card along with the valid or invalid message (or issuer unknown message).