DOC PREVIEW
WUSTL CSE 131 - sp14_5

This preview shows page 1-2-3-4-25-26-27-52-53-54-55 out of 55 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 55 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 55 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 55 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 55 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 55 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 55 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 55 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 55 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 55 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 55 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 55 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 55 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Slide 1Slide 2A Foundation for ProgrammingAnatomy of a Java Method (a.k.a Function)Flow of ControlFlow of ControlFunction Call TraceFunction Call TraceFunction Call TraceFunction Call TraceFunction Call TraceFunction Call TraceFunction Call TraceFunction Call TraceFunction Call TraceFunction Call TraceFunction Call TraceFunction Call TraceFunction Call TraceFunction Call TraceFunction Call TraceFunction Call TraceFunction Call TraceFunction Call TraceFunction Call TraceFunction Call TraceFunction Call TraceFunction Call TraceFunction Call TraceFunction Call TraceFunction Call TraceFunction Call TraceFunction Call TraceFunction Call TraceFunction Call TraceFunction Call TraceFunction Call TraceFunction Call TraceFunction Call TraceFunction Call TraceFunction Call TraceFunction Call TraceFlow of ControlReturn valuesScope of variablesScopeNo duplicate names in the same scopeFunction Challenge 1aFunction Challenge 1bFunction Challenge 1cFunction Challenge 1dFunction Challenge 1ePassing by values vs. passing by referencesPassing by referencePassing by valuesModule 5: MethodsIntroduction to Programming in Java: An Interdisciplinary Approach · Robert Sedgewick and Kevin Wayne · Copyright © 2002–2010 · 1/14/19 07:10:10 AMMethodsfxyzf (x, y, z)3A Foundation for Programmingobjectsmethodsgraphics, sound, and image I/Oarraysconditionals and loopsMath text I/Oassignment statementsprimitive data typesany program you might want to writebuild bigger programsand reuse code4Anatomy of a Java Method (a.k.a Function)Java methods. Easy to write your own.f(x) = xinput2.0 1.414213…output5Flow of ControlKey point. Methods provide a new way to control the flow of execution.6Flow of ControlKey point. Functions provide a new way to control the flow of execution.7public class Newton { public static double sqrt(double c) { double epsilon = 1e-15; if (c < 0) return Double.NaN; double t = c; while (Math.abs(t - c/t) > epsilon * t) t = (c/t + t) / 2.0; return t; } public static void main(String[] args) { double[] a = new double[args.length]; for (int i = 0; i < args.length; i++) a[i] = Double.parseDouble(args[i]); for (int i = 0; i < a.length; i++) System.out.println(sqrt(a[i])); }}Function Call Trace8public class Newton { public static double sqrt(double c) { double epsilon = 1e-15; if (c < 0) return Double.NaN; double t = c; while (Math.abs(t - c/t) > epsilon * t) t = (c/t + t) / 2.0; return t; } public static void main(String[] args) { double[] a = new double[args.length]; for (int i = 0; i < args.length; i++) a[i] = Double.parseDouble(args[i]); for (int i = 0; i < a.length; i++) System.out.println(sqrt(a[i])); }}Function Call Trace% java Newton 1 2 39public class Newton { public static double sqrt(double c) { double epsilon = 1e-15; if (c < 0) return Double.NaN; double t = c; while (Math.abs(t - c/t) > epsilon * t) t = (c/t + t) / 2.0; return t; } public static void main(String[] args) { double[] a = new double[args.length]; for (int i = 0; i < args.length; i++) a[i] = Double.parseDouble(args[i]); for (int i = 0; i < a.length; i++) System.out.println(sqrt(a[i])); }}Function Call Trace% java Newton 1 2 3{ "1", "2", "3" }args[]{ "1", "2", "3" }10public class Newton { public static double sqrt(double c) { double epsilon = 1e-15; if (c < 0) return Double.NaN; double t = c; while (Math.abs(t - c/t) > epsilon * t) t = (c/t + t) / 2.0; return t; } public static void main(String[] args) { double[] a = new double[args.length]; for (int i = 0; i < args.length; i++) a[i] = Double.parseDouble(args[i]); for (int i = 0; i < a.length; i++) System.out.println(sqrt(a[i])); }}Function Call Trace% java Newton 1 2 3args[]{ "1", "2", "3" }a[]{ 0.0, 0.0, 0.0 }11public class Newton { public static double sqrt(double c) { double epsilon = 1e-15; if (c < 0) return Double.NaN; double t = c; while (Math.abs(t - c/t) > epsilon * t) t = (c/t + t) / 2.0; return t; } public static void main(String[] args) { double[] a = new double[args.length]; for (int i = 0; i < args.length; i++) a[i] = Double.parseDouble(args[i]); for (int i = 0; i < a.length; i++) System.out.println(sqrt(a[i])); }}Function Call Trace% java Newton 1 2 3args[]{ "1", "2", "3" }a[]{ 0.0, 0.0, 0.0 }i012public class Newton { public static double sqrt(double c) { double epsilon = 1e-15; if (c < 0) return Double.NaN; double t = c; while (Math.abs(t - c/t) > epsilon * t) t = (c/t + t) / 2.0; return t; } public static void main(String[] args) { double[] a = new double[args.length]; for (int i = 0; i < args.length; i++) a[i] = Double.parseDouble(args[i]); for (int i = 0; i < a.length; i++) System.out.println(sqrt(a[i])); }}Function Call Trace% java Newton 1 2 3a[]{ 1.0, 0.0, 0.0 }args[]{ "1", "2", "3" }i013public class Newton { public static double sqrt(double c) { double epsilon = 1e-15; if (c < 0) return Double.NaN; double t = c; while (Math.abs(t - c/t) > epsilon * t) t = (c/t + t) / 2.0; return t; } public static void main(String[] args) { double[] a = new double[args.length]; for (int i = 0; i < args.length; i++) a[i] = Double.parseDouble(args[i]); for (int i = 0; i < a.length; i++) System.out.println(sqrt(a[i])); }}Function Call Trace% java Newton 1 2 3a[]{ 1.0, 0.0, 0.0 }args[]{ "1", "2", "3" }i114public class Newton { public static double sqrt(double c) { double epsilon = 1e-15; if (c < 0) return Double.NaN; double t = c; while (Math.abs(t - c/t) > epsilon * t) t = (c/t + t) / 2.0; return t; } public static void main(String[] args) { double[] a = new double[args.length]; for (int i = 0; i < args.length; i++) a[i] = Double.parseDouble(args[i]); for (int i = 0; i < a.length; i++) System.out.println(sqrt(a[i])); }}Function Call Trace% java Newton 1 2 3a[]{ 1.0, 2.0, 0.0 }args[]{ "1", "2", "3" }i115public class Newton { public static double sqrt(double c) { double epsilon = 1e-15; if (c < 0) return Double.NaN; double t = c; while (Math.abs(t - c/t) > epsilon * t) t = (c/t + t) / 2.0; return t; } public static void main(String[] args) { double[] a = new double[args.length]; for (int i = 0; i < args.length; i++) a[i] = Double.parseDouble(args[i]); for (int i = 0; i < a.length; i++) System.out.println(sqrt(a[i])); }}Function Call


View Full Document

WUSTL CSE 131 - sp14_5

Documents in this Course
sp14_4

sp14_4

28 pages

sp14_3

sp14_3

29 pages

sp14_2

sp14_2

43 pages

sp14_10

sp14_10

19 pages

sp14_9

sp14_9

16 pages

sp14_8

sp14_8

22 pages

sp14_7

sp14_7

33 pages

sp14_6

sp14_6

27 pages

lecture1

lecture1

33 pages

lab0

lab0

6 pages

Load more
Download sp14_5
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view sp14_5 and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view sp14_5 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?