Math class methods & User defined methods Introduction to Computers and Programming in JAVA: V22.0002.

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

Procedural programming in Java
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.
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.
Introduction to Computers and Programming Introduction to Methods in Java.
Overloading methods review When is the return statement required? What do the following method headers tell us? public static int max (int a, int b)
Introduction to Computers and Programming Lecture 12: Math.random() Professor: Evan Korth New York University.
© 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.
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.
 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.
Introduction to Java Programming, 4E Y. Daniel Liang.
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.
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.
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 COS240 O-O Languages AUBG,
1 Topic 04 Methods Programming II/A CMC2522 / CIM2561 Bavy Li.
 2003 Prentice Hall, Inc. All rights reserved Introduction Modules –Small pieces of a problem e.g., divide and conquer –Facilitate design, implementation,
FunctionsFunctions Systems Programming Concepts. Functions   Simple Function Example   Function Prototype and Declaration   Math Library Functions.
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 4 Methods F Introducing Methods –Benefits of methods, Declaring Methods, and Calling Methods F Passing Parameters –Pass by Value F Overloading.
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.
Chapter 5: Methods 1. Objectives To declare methods, invoke methods, and pass arguments to a method (§ ). To use method overloading and know ambiguous.
 2005 Pearson Education, Inc. All rights reserved Methods: A Deeper Look.
Introduction to Computers and Programming Lecture 14: User defined methods (cont) Professor: Evan Korth New York University.
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.
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.
Procedural programming in Java Methods, parameters and return values.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 4 Methods Chapter.
© 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.
KIC/Computer Programming & Problem Solving 1.  Introduction  Program Modules in C  Math Library Functions  Functions  Function Definitions  Function.
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.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 5 Methods.
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.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 6 Methods Dr. Musab Zghoul.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 6 Methods.
 Pearson Education, Inc. All rights reserved Methods: A Deeper Look.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 4.
Reference: COS240 Syllabus
Methods Chapter 6.
Chapter 6: Methods CS1: Java Programming Colorado State University
Object Oriented Systems Lecture 03 Method
Chapter 6 Methods 1.
Introduction to Methods
Chapter 5 - Functions Outline 5.1 Introduction
Chapter 5 – Part 2 Methods Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved
Group Status Project Status.
CS2011 Introduction to Programming I Methods (II)
Chapter 6 Methods.
Chapter 5 Methods.
BBIT 212/ CISY 111 Object Oriented Programming (OOP)
Chapter 6: Methods CS1: Java Programming Colorado State University
Presentation transcript:

Math class methods & User defined methods Introduction to Computers and Programming in JAVA: V

Math class methods Math.sqrt(4.0) Math.random() java.lang is the library/package that provides Math class methods such as –Math.random() to generate random numbers Java.lang is accessed by all java programs by default. –YOU do not have to include it at the beginning of the program as you do with javax package.

 2000 Prentice Hall, Inc. All rights reserved. Modified for use with this class. 3 Random-Number Generation Often we want our programs to generate random numbers. –games of chance –testing without user interaction Java random-number generators –Math.random() Returns a double value with a positive sign, greater than or equal to 0.0 and less than 1.0. –What if we want to generate random integers?

 2000 Prentice Hall, Inc. All rights reserved. Modified for use with this class. 4 Random-Number Generation –Math.random() Produces double from 0.0 to 1.0 (excluding 1) ( int ) ( Math.random() * 6 ) Produces integers from 0 – 5 –1 + ( int ) ( Math.random() * 6 ) Produces integers from 1 – 6 Scaling Shifting

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.

 2000 Prentice Hall, Inc. All rights reserved. Modified for use with this class. 6 User defined Method returning a value General format of method declaration: modifiers return-value-type method-name ( parameter1, …, parameterN ) { declarations and statements } Method can return one value: return expression ; Or, it can returns nothing using keyword void in the header. Method header Method body Scope of local variables

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 takeInTwoFloats( float x, y ) is incorrect –Instead you must write takeInTwoFloats(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. Modified for use with this class 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. Modified for use with this class. 12

Understanding Scope

 2000 Prentice Hall, Inc. All rights reserved. Modified for use with this class. 14 Local Variables & Scope A local variable: a variable defined inside a method. Scope: the part of the program where the variable can be referenced. The scope of a local variable starts from its declaration and continues to the end of the block that contains the variable. A local variable must be declared before it can be used.

 2000 Prentice Hall, Inc. All rights reserved. Modified for use with this class. 15 Local Variables & Scope, continued You can declare a local variable with the same name multiple times in different non- nesting blocks in a method, but you cannot declare a local variable twice in nested blocks.

Overloading methods

 2000 Prentice Hall, Inc. All rights reserved. Modified for use with this class Method Overloading Method overloading –Several methods of the same name –Different parameter set for each method Number of parameters Parameter types –The Java compiler determines which method to use based on the parameters. –Can also be used in conjunction with argument coercion. The combination can lead to ambiguous invocation which is an error

 2000 Prentice Hall, Inc. All rights reserved. Modified for use with this class. 18 Ambiguous Invocation Sometimes there may be two or more possible matches for an invocation of a method, but the compiler cannot determine the most specific match. This is referred to as ambiguous invocation. Ambiguous invocation is a compilation error.

 2000 Prentice Hall, Inc. All rights reserved. Modified for use with this class. 19 Ambiguous Invocation public class AmbiguousOverloading { public static void main(String[] args) { System.out.println(max(1, 2)); } public static double max(int num1, double num2) { if (num1 > num2) return num1; else return num2; } public static double max(double num1, int num2) { if (num1 > num2) return num1; else return num2; }

review What is a method? What information can you learn about a method from its header? What does it mean to invoke a method? What is call by value? What is scope of local variables within a method? Why don’t we have to import the Math class? What is abstraction in computer science?