Download presentation
Presentation is loading. Please wait.
Published byIsaac Payne Modified over 9 years ago
1
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Methods (a.k.a. Functions)
2
Methods (a.k.a. Functions) We’ve seen and used methods already.... These are all examples of method calls r = Math.sqrt( 9.0 ); q = Math.pow( r, 3.0 ); answer = Math.sqrt(Math.pow(x, 2.0) + Math.pow(y, 2.0)); z = Math.abs( y – x ); p = Math.exp( n );
3
Methods Method calls change a program’s flow of control: The flow of control jumps to the method Method code is executed An output is returned and flow of control returns to the calling method r = Math.sqrt(9.0); execute the square root code (algorithm) 2 capture the returned value 3 1 call the sqrt() method 1 3 9.0 input to method 3.0 output of method 2 1 3
4
Writing our own Methods We can build our own methods in Java by identifying the three required elements : Input values (and data types)... Name of method... Output data type... public static double cube(double x) a Java method can have no more than 1 output (to have zero outputs, use void instead)
5
Writing our own Methods public class CubeProgram { public static void main(String args[]) { double v = 12.5; double vCubed = cube(v); // CALL THE METHOD System.out.println(v + " cubed is: " + vCubed); } public static double cube(double x) { double y = x * x * x; // METHOD CODE return y; } Add a quadruple() method and call it from main()
6
Writing our own Methods public class CubeProgram { public static void main(String args[]) { double v = 12.5; double vCubed = cube(v); // CALL THE METHOD System.out.println(v + " cubed is: " + vCubed); } public static double cube(double x) { double y = x * x * x; // METHOD CODE return y; } Add a quadruple() method and call it from main() Write a program that asks the user to enter four numbers in main() ; write a method called calcAverage() that calculates and returns the average of the four numbers Write a program to simulate the rolling of two dice, displaying the results; use a method called rollDice()
7
Why Create Methods? Methods are reusable program units Write once, use repeatedly Algorithm details are hidden, increasing readability Methods are compact modules that serve a specific purpose for which they were designed It’s easier to read, write, and maintain individual modules
8
Decision-Making Methods Aside from calculations, methods are often used to make decisions Return value is of type boolean (i.e. true or false ) public static boolean isPassingGrade( int grade ) { if ( grade >= 60 ) { return true; } else { return false; }
9
Decision-Making Methods public static void main( String[] args ) {... if ( isPassingGrade( grade ) ) { System.out.println( "You passed!" );... }... } public static boolean isPassingGrade( int grade ) { if ( grade >= 60 ) { return true; } else { return false; }
10
Input Parameters When we call a method, we pass in values int max = findMax( num1, num2 ); double c = cube( 9.0 ); double r = power( x, 4 ); int fact = factorial( n ); int total = rollDice();...or sometimes we don’t pass in any values at all
11
Input Parameters public class FindMaxProgram { public static void main( String[] args ) { int num1 = 19, num2 = 24; int max = findMax( num1, num2 ); // CALL METHOD System.out.println( max + " is the maximum." ); } public static int findMax( int x, int y ) { // METHOD CODE GOES HERE... } Write the findMax() method code ( HINT : use an if-else statement)
12
Input Parameters public static void main( String[] args ) {... int max = findMax( num1, num2 ); // CALL METHOD... } public static int findMax( int x, int y ) { // METHOD CODE GOES HERE... } 19 24 Pass values to the method
13
Input Parameters 1924 19 storage in memory Variables x and y are usable only within the findMax() method public static void main( String[] args ) {... int max = findMax( num1, num2 ); // CALL METHOD... } public static int findMax( int x, int y ) { // METHOD CODE GOES HERE... }
14
Variable Scope We know that a variable must be declared before it can be used public static void main( String[] args ) { int x = 5; System.out.println( x ); System.out.println( y ); } Variable y is not declared! Declaring variable x creates storage for it in memory 5 x
15
Variable Scope Variables are usable (in scope) only within the section of code in which they are declared public static void main( String[] args ) { int x = 5; int r; r = cube( x ); } 5 x r public static int cube( int w ) {... 5 w x and r only usable within main() method w only usable within cube() method
16
public static int cube( int w ) {... public static void main( String[] args ) { int x = 5; int r; r = cube( x ); } Variable Scope Variables are usable (in scope) only within the section of code in which they are declared 5 xr main() 5 w cube() w only usable within cube() method x and r only usable within main() method
17
public static int cube( int w ) { int z = w * w * w; public static void main( String[] args ) { int x = 5; int r; r = cube( x ); } Variable Scope Variables are usable (in scope) only within the section of code in which they are declared 5 xr 5 w main() cube() w and z only usable within cube() method x and r only usable within main() method 125 z
18
public static int cube( int w ) { int z = w * w * w; return z; } public static void main( String[] args ) { int x = 5; int r; r = cube( x ); } Variable Scope Variables are usable (in scope) only within the section of code in which they are declared 5 xr 5 w main() cube() w and z only usable within cube() method x and r only usable within main() method 125 z
19
public static int cube( int w ) { int z = w * w * w; return z; } public static void main( String[] args ) { int x = 5; int r; r = cube( x ); } Variable Scope Variables are usable (in scope) only within the section of code in which they are declared 5 x 125 r 5 w main() cube() w and z only usable within cube() method x and r only usable within main() method 125 z
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.