Introduction to Application Programming IST 256 Application Programming for Information Systems Xiaozhong Liu
Procedure public static void main(String[] args) { int number; … ABC (number) … } Private static void ABC (int targetnum) { … }
Concept of Procedures and Functions All modern programming languages have a way to break the program into smaller pieces by placing a piece of code in a procedure or function – This type of modularization is good programming design – Often entire sections of code need to be repeated in different parts of the program, for example sorting a list of data, and making it into a procedure streamlines the process Write once, use many times Java calls these methods
Method definition public static int squareR ( int y) { // variable to hold the square of y int ysquared; ysquared = y * y; return ysquared; } scope keywordsreturn typemethod nameformal parameter list method header method body
Procedure – organize your code public static void main(String[] args) { String query = “???”; String results; …… results = search (query); results = rank (results); show_results(results); … } private static String search (String query) { String results; … return results; }
Wine or water? public static void main(String[] args) { String drink; int age = ??; if (oldenough(age)) { drink = “wine”; } else { drink = “water”; } oldenough(age) Returns a boolean result
…100 public static void main(String[] args) { int result = 0; int start = 1; int end = 100; result = sumnums (start, end); System.out.println(result); } sumnums (start, end) Returns int
Convert temperature public static void main(String[] args) { double Fahrenheit, Celsius = ??; Fahrenheit = convert_temp (Celsius ); System.out.println(Fahrenheit ); } convert_temp (Celsius ) Returns double
Practice … … 1500 public static int sumnum (int startnum, int endnum, int intervalnum) { int number = startnum; int result = 0; while (number < endnum) { result = result + number; number = number + intervalnum; } return result; }