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.

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

Chapter 5 Methods to our madness. Review Identifiers are names used for variables, constants, methods, classes, and packages. Must start with a letter,
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 4 Mathematical Functions, Characters,
Methods Liang, Chapter 4. What is a method? A method is a way of running an ‘encapsulated’ series of commands. System.out.println(“ Whazzup ”); JOptionPane.showMessageDialog(null,
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 19 Recursion.
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, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 5 Methods.
Syllabus (1) WeekChapters 1Introduction to the course, basic java language programming concepts: Primitive Data Types and Operations 1, 2 2Methods, Control.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 5 Methods.
Method Abstraction Java Programming. Key to developing software is to apply the concept of abstraction. Method abstraction is defined as separating the.
Functions. COMP104 Lecture 13 / Slide 2 Review of Array: Bubble Sort for (j=0; j List[j+1]) swap(List[j], List[j+1]); }
Introduction to Java Programming, 4E Y. Daniel Liang.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 5 Methods.
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, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 4 Methods.
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.
Topic 2A – Library Functions and Casting. CISC 105 – Topic 2A Functions A function is a piece of code which performs a specific task. When a function.
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.
 2000 Prentice Hall, Inc. All rights reserved. Functions in C Outline 1Introduction 2Program Modules in C 3Math Library Functions 4Functions 5Function.
INF120 Basics in JAVA Programming AUBG, COS dept, Spring 2014 Lecture 08 Title: Methods, Part 2 Reference: MalikFarrell, chap 1, Liang, Ch 5.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 4 Mathematical Functions, Characters,
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.
COMP102 Lab 091 COMP 102 Programming Fundamentals I Presented by : Timture Choi.
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.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 4 Methods.
Programming Fundamentals I (COSC-1336), Lecture 5 (prepared after Chapter 5 of Liang’s 2011 textbook) Stefan Andrei 4/23/2017 COSC-1336, Lecture 5.
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.
Methods 1. Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Introducing Methods.
Liang,Introduction to Java Programming,revised by Dai-kaiyu 1 Chapter 4 Methods 条条大路通罗马.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 4 Mathematical Functions, Characters,
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.
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.
TOPIC 6 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: Methods Method Structure Method Structure Declaring Methods Declaring Methods Calling Methods Calling Methods Passing Parameters Passing Parameters.
1 Chapter 8 Recursion. 2 Objectives  To know what is a recursive function and the benefits of using recursive functions (§8.1).  To determine the base.
Chapter 5 Methods 1. Motivations Method : groups statements that perform a function.  Level of abstraction (black box)  Code Reuse – no need to reinvent.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 6 Methods Dr. Musab Zghoul.
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.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 5 Methods.
Chapter 4 Mathematical Functions, Characters, and Strings 1.
Chapter 4: Mathematical Functions, Characters, and Strings
Chapter 4 Mathematical Functions, Characters, and Strings
Chapter 4 Mathematical Functions, Characters, and Strings
Chapter 19 Recursion.
Chapter 5 Methods.
Chapter 6: Methods CS1: Java Programming Colorado State University
Chapter 3 Methods.
Chapter 5 Methods.
Chapter 6 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 5 Methods.
METHODS (FUNCTIONS) By: Lohani Adeeb khan.
Chapter 6 Methods.
Chapter 5 Methods.
Chapter 5 Methods Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved
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 4 Methods F Introducing Methods F Declaring Methods F Calling Methods F Passing Parameters F Pass by Value F Overloading Methods F Method Abstraction  The Math Class F Recursion (Optional)

Introducing Methods Method Structure A method is a collection of statements that are grouped together to perform an operation.

Declaring Methods public static int max(int num1, int num2) { if (num1 > num2) return num1; else return num2; }

Calling Methods Example 4.1 Testing the max method This program demonstrates calling a method max to return the largest of the int values TestMax

Passing Parameters void nPrintln(String message, int n) { for (int i=0; i<n; i++) System.out.println(message); }

Pass by Value Example 4.2 Testing Pass by value This program demonstrates passing values to the methods. TestPassByValue

Overloading Methods Example 4.3 Overloading the max Method double max(double num1, double num2) { if (num1 > num2) return num1; else return num2; } TestMethodOverloading

Method Abstraction You can think of the method body as a black box that contains the detailed implementation for the method.

The Math Class F Class constants: –PI –E F Class methods: –Trigonometric Methods –Exponent Methods –Miscellaneous

Trigonometric Methods F sin(double a) F cos(double a) F tan(double a) F acos(double a) F asin(double a) F atan(double a)

Exponent Methods  exp(double a) Returns e raised to the power of a.  log(double a) Returns the natural logarithm of a.  pow(double a, double b) Returns a raised to the power of b.  sqrt(double a) Returns the square root of a.

Rounding Methods  double ceil(double x) x rounded up to its nearest integer. This integer is returned as a double value.  double floor(double x) x is rounded down to its nearest integer. This integer is returned as a double value.  double rint(double x) x is rounded to its nearest integer. If x is equally close to two integers, the even one is returned as a double.  int round(float x) Return (int)Math.floor(x+0.5).  double round(double x) Return (long)Math.floor(x+0.5).

min, max, abs, and random  max(a, b) and min(a, b) Returns the maximum or minimum of two parameters.  abs(a) Returns the absolute value of the parameter.  random() Returns a random double value in the range [0.0, 1.0).

Using Math Methods Example 4.4 Computing Mean and Standard Deviation Generate 10 random numbers and compute the mean and standard deviation ComputeMeanDeviation

Case Studies Example 4.5 Displaying Calendars The program reads in the month and year and displays the calendar for a given month of the year. PrintCalendar

Design Diagram

Recursion (Optional) Example 4.6 Computing Factorial factorial(0) = 1; factorial(n) = n*factorial(n-1); ComputeFactorial

Fibonnaci Numbers Example 4.7 Computing Finonacci Numbers Find Fibonacci numbers using recursion. fib(0) = 1; fib(1) =1; fib(n) = fib(n-2) + fib(n-1); n>=2 ComputeFibonacci

Towers of Hanoi Example 4.8 Solving the Towers of Hanoi Problem Solve the towers of Hanoi problem. TowersOfHanoi