Methods Every Java application must have a main method. The JVM calls main when it runs your program. When main finishes control goes back to the JVM. Up to now, we have always coded public static void main(string[] args){ We have used many methods developed by other programmers. For example Math.max, System.out.println, Character.isDigit, Now we write our own methods 1
Methods Methods are used to improve readability and to provide reusable pieces of code Methods can be designed to Return a value Return nothing Have parameters (for passing values into a method) 2
Methods Example Sieve of Eratosthenes 1. Background and the algorithm 2. straightforward implementation with main method 3. introduce 1 more method 4. introduce 2 more methods 3 versions: Sieve1 Sieve2 Sieve3 3
Methods Example Sieve of Eratosthenes 1. Background and the algorithm 2. straightforward implementation with main method 3. introduce 1 more method 4. introduce 2 more methods 4
Methods Sieve of Eratosthenes Sieve of Eratosthenes an algorithm for finding prime numbers. attributed to a Greek mathematician, Eratosthenes of Cyrene, who lived more than 2, 000 years ago Example of sieve in action see https://en.wikipedia.org/wiki/sieve_of_eratosthenes 5
Methods Sieve of Eratosthenes Sieve of Eratosthenes Begin with a list of integers ranging from 2 up to a limit, say 120 1. next 2 // begin crossing-off multiples starting with 2 2. for next ranging from 2 to limit in increments of 1 (a) if list next is not crossed-off // cross-off all multiples of next i. p 2*next ii. while p <= limit A. list p 0 // cross-off this multiple B. p p+next // next multiple 6
Methods Example: Sieve of Eratosthenes Sieve of Eratosthenes 1. Background and the algorithm 2. straightforward implementation with main method 3. introduce 1 more method 4. introduce 2 more methods 7
Methods Listing 7.1 Sieve with main method import javax.swing.joptionpane; import java.util.arraylist; public class Sieve1 One method, main { public static void main(string[] args){ // get limit from user String limitasstring = JOptionPane.showInputDialog("Enter upper limit:"); int limit = Integer.parseInt(limitAsString); // set up the list of integers // apply the sieve technique Code left out // display primes JOptionPane.showMessageDialog(null,"primes < "+limit+" are "+result); 8
Methods Listing 7.1 Sieve with main method Sieve1. Can be considered a well-written program Relatively easy to follow Some prefer the use of methods to break a program up into its component parts and make it more readable, and hence easier to see its overall construction 9
Methods Example: Sieve of Eratosthenes Sieve of Eratosthenes 1. Background and the algorithm 2. straightforward implementation with main method 3. introduce 1 more method 4. introduce 2 more methods 10
Methods Sieve with two methods, Listing 7.2 Sieve2 The program has 2 parts to it: 1. main 2. getlimitfromuser main Main calls getlimitfromuser getlimitfromuser 11
import javax.swing.joptionpane; import java.util.arraylist; Methods Sieve with two methods, Listing 7.2 public class Sieve2 { public static void main(string[] args){ int limit = getlimitfromuser(); // set up the list of integers ArrayList<Integer> list = new ArrayList<>(); main calls getlimitfromuser returns an int no parameters public static int getlimitfromuser(){ String limitasstring = JOptionPane.showInputDialog("Enter upper limit:"); int number = Integer.parseInt(limitAsString); return number; Control returns to main and the value of number replaces the call 12
Methods Example: Sieve of Eratosthenes Sieve of Eratosthenes 1. Background and the algorithm 2. straightforward implementation with a main method 3. introduce 1 more method 4. introduce 2 more methods 13
Methods Sieve with 3 methods, Listing 7.3 Move more code out into methods producing a trivial main method public static void main ( String [] args ) { int limit = getlimitfromuser () ; ArrayList < Integer > result = applysieve ( limit ) ; displayresults ( result ) ; main getlimitfromuser applysieve displayresults 14
Methods Example, Listing 7.3 This structure diagram shows which method calls which method and any parameters and/or return values main limit limit list of primes list of primes getlimitfromuser applysieve displayresults 15
import javax.swing.joptionpane; import java.util.arraylist; Methods Example, Listing 7.3 public class Sieve3 { public static void main(string[] args){ int limit = getlimitfromuser(); ArrayList<Integer> result = applysieve(limit); displayresults(result); public static void displayresults(arraylist<integer> listofprimes){ public static int getlimitfromuser(){ public static ArrayList<Integer> applysieve(int upperlimit){ 16
Methods - summary Methods can be designed to Return a value Return nothing With parameters (for passing values into a method) 17
Methods Value-returning methods Methods can be designed to return a value to the point where called. This is the usual way to return values to a calling method. Example. reverse the letters of a string Return type public static String reverseword(string w){ String r = ""; for (int i=w.length()-1; i>=0 ; i--){ r+=w.charat(i); return r; return statement causes execution of reverseword to terminate, and the value of r is returned to the point where reverseword was called Note: a value-returning method must have a return statement could have more than one. 18
Methods - void methods Methods can be designed to accomplish some task, and do not need to return anything Example. display a word in reverse Return type public static void displayreverse(string w){ String r = ""; for (int i=w.length()-1; i>=0 ; i--){ r+=w.charat(i); System.out.println( r ); Usually no return statement When execution of method reaches its end, the JVM automatically goes back to point where method was invoked 19
Methods - Parameters A method with parameters must be invoked with similar arguments. When invoked primitive arguments are passed in to primitive parameters, but nothing is passed back on return. objects are passed by address; any changes made to an object persist after the method call The common way to pass values back to a calling method is to use a valuereturning method Example. calculate the average of an integer ArrayList public static double average(arraylist<integer> list){ double sum = 0; for (Integer e : list) sum+=e; return sum/list.size(); 20
Methods - exercise Review the sample answer to the last lab Note the use of methods Exercise: Draw the structure diagram for the program showing the types of values passed into methods and the values returned. 21