Java 8 function example

java 8 functional interface example and java code in javascript function example
Dr.AlexanderTyler Profile Pic
Dr.AlexanderTyler,India,Teacher
Published Date:21-07-2017
Your Website URL(Optional)
Comment
2.1 Functions Introduction to Programming in Java: An Interdisciplinary Approach · Robert Sedgewick and Kevin Wayne · Copyright © 2002–2010 · 2/17/11 9:58 PM 2.1 Functions x y f (x, y, z) f z A Foundation for Programming any program you might want to write objects build bigger programs functions and modules and reuse code graphics, sound, and image I/O arrays conditionals and loops Math text I/O primitive data types assignment statements 3 Functions (Static Methods) Java function.   Takes zero or more input arguments.   Returns one output value. more general than   Side effects (e.g., output to standard draw). mathematical functions Applications.   Scientists use mathematical functions to calculate formulas.   Programmers use functions to build modular programs.   You use functions for both. Examples.   Built-in functions: Math.random(), Math.abs(), Integer.parseInt().   Our I/O libraries: StdIn.readInt(), StdDraw.line(), StdAudio.play().   User-defined functions: main(). 4 Anatomy of a Java Function Java functions. Easy to write your own. input output 2.0 f(x) = √x 1.414213… 5 Flow of Control Key point. Functions provide a new way to control the flow of execution. 6 Flow of Control Key point. Functions provide a new way to control the flow of execution. What happens when a function is called:   Control transfers to the function code.   Argument variables are assigned the values given in the call.   Function code is executed.   Return value is assigned in place of the function name in calling code.   Control transfers back to the calling code. Note. This is known as "pass by value." 7 Scope Scope (of a name). The code that can refer to that name. Ex. A variable's scope is code following the declaration in the block. Best practice: declare variables to limit their scope. 8 Function Challenge 1a Q. What happens when you compile and run the following code? public class Cubes1 public static int cube(int i) int j = i i i; return j; public static void main(String args) int N = Integer.parseInt(args0); for (int i = 1; i = N; i++) StdOut.println(i + " " + cube(i)); % javac Cubes1.java % java Cubes1 6 1 1 2 8 3 27 4 64 5 125 6 216 9 Function Challenge 1b Q. What happens when you compile and run the following code? public class Cubes2 public static int cube(int i) int i = i i i; return i; public static void main(String args) int N = Integer.parseInt(args0); for (int i = 1; i = N; i++) StdOut.println(i + " " + cube(i)); 10 Function Challenge 1c Q. What happens when you compile and run the following code? public class Cubes3 public static int cube(int i) i = i i i; public static void main(String args) int N = Integer.parseInt(args0); for (int i = 1; i = N; i++) StdOut.println(i + " " + cube(i)); 11 Function Challenge 1d Q. What happens when you compile and run the following code? public class Cubes4 public static int cube(int i) i = i i i; return i; public static void main(String args) int N = Integer.parseInt(args0); for (int i = 1; i = N; i++) StdOut.println(i + " " + cube(i)); 12 Function Challenge 1e Q. What happens when you compile and run the following code? public class Cubes5 public static int cube(int i) return i i i; public static void main(String args) int N = Integer.parseInt(args0); for (int i = 1; i = N; i++) StdOut.println(i + " " + cube(i)); 13 Gaussian Distribution Gaussian Distribution Standard Gaussian distribution.   "Bell curve."   Basis of most statistical analysis in social and physical sciences. Ex. 2000 SAT scores follow a Gaussian distribution with mean µ = 1019, stddev σ = 209. 601 1437 810 1019 1228 2 2 2 −x /2 −(x−µ) / 2σ 1 1 φ(x) = e φ (x, µ,σ ) = e 2π σ 2π x− µ = φ /σ ( ) σ € 15 € Java Function for φ(x) Mathematical functions. Use built-in functions when possible; build your own when not available. 2 −x /2 1 φ(x) = e public class Gaussian 2π public static double phi(double x) return Math.exp(-xx / 2) / Math.sqrt(2 Math.PI); € public static double phi(double x, double mu, double sigma) return phi((x - mu) / sigma) / sigma; x− µ φ (x, µ,σ) = φ /σ ( ) σ € Overloading. Functions with different signatures are different. Multiple arguments. Functions can take any number of arguments. Calling other functions. Functions can call other functions. library or user-defined 16 Gaussian Cumulative Distribution Function Goal. Compute Gaussian cdf Φ(z). Challenge. No "closed form" expression and not in Java library. 2 −x /2 1 φ(x) = e 2π Φ(z) € z Taylor series Bottom line. 1,000 years of mathematical formulas at your fingertips. 17 Java function for Φ(z) public class Gaussian public static double phi(double x) // as before public static double Phi(double z) if (z -8.0) return 0.0; if (z 8.0) return 1.0; double sum = 0.0, term = z; for (int i = 3; sum + term = sum; i += 2) sum = sum + term; term = term z z / i; return 0.5 + sum phi(z); accurate with absolute error -16 less than 8 10 public static double Phi(double z, double mu, double sigma) return Phi((z - mu) / sigma); z Φ(z, µ, σ ) = φ (z, µ,σ ) = Φ((z− µ) /σ ) ∫ −∞ 18 € SAT Scores Q. NCAA requires at least 820 for Division I athletes. What fraction of test takers in 2000 do not qualify? A. Φ(820, 1019, 209) ≈ 0.17051. approximately 17% area = 0.17 601 1437 810 1019 1228 820 double fraction = Gaussian.Phi(820, 1019, 209); 19 Gaussian Distribution Q. Why relevant in mathematics? A. Central limit theorem: under very general conditions, average of a set of random variables tends to the Gaussian distribution. Q. Why relevant in the sciences? A. Models a wide range of natural phenomena and random processes.   Weights of humans, heights of trees in a forest.   SAT scores, investment returns. Caveat. “ “ E Tout ver l ye body monde bel y ie c ve rs oi itn t ce he pe e nde xpone nt, c nt ar ial le lsaw of expé e rir m ror ent s: t eurhe s s e 'ix m pe agi rim ne ent nt ers, because t que hey c t'hi esnk t un t it ché an be orem pr de ov m ed by athé m mat athe ique mat s, e icts l ; and t es mat he hé m mat athe icim ens at que icians  , because c the 'es yt be un f lie ai ve t e it x has péri be mee nt n e als . tabl ” ished by observation. ” — — M. L M. Li ippm ppman i an in a l n a le et tt te er r t to H o H. P . Poi oinc ncar aré é 20