# 1 March 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems

## Presentation on theme: "1 March 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems"— Presentation transcript:

1 March 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems sjmaybank@dcs.bbk.ac.uk Spring 2013 Week 8: Methods

Overview Java Lab 7, Exercises 2 and 3 Definition of a method Parameters The reserved word void See Java for Everyone, Ch. 5 1 March 2013Birkbeck College, U. London2

Exercise 2: rectangle Write a program … to request two non-negative integers numberRows and numberColumns … and print out numberRows rows of asterisks, such that each row contains numberColumns asterisks. For example, if numberRows = 2 and numberColumns = 4, then the output is **** 1 March 2013Birkbeck College, U. London3

Exercise 2: code import java.util.Scanner; public class Rectangle { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.print("Type in the number of rows: "); int numberRows = in.nextInt(); System.out.print("Type in the number of columns: "); int numberColumns = in.nextInt(); // more code here } 1 March 2013Birkbeck College, U. London4

Exercise 2: more code for(int i = 1; i <= numberRows; i++) { for(int j = 1; j <= numberColumns; j++) { System.out.print("*"); } System.out.println(); } 1 March 2013Birkbeck College, U. London5

Exercise 3: number properties Write a program that reads a set of strictly positive integers from the keyboard … The end of the input is indicated by the integer 0. … The program then prints out the following numbers. The average The smallest of the values The largest of the values The range 1 March 2013Birkbeck College, U. London6

Exercise 3: code(1) import java.util.Scanner; public class NumberProperties { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.println("Input one number at a time, with each number "); System.out.println("followed by a return. Numbers must be strictly "); System.out.println("positive integers. Use 0 to terminate the input."); // more code } 1 March 2013Birkbeck College, U. London7

Exercise 3: code(2) int n = 0, currentInteger = -1, minValue = 0, maxValue = 0; double total = 0.0; while (currentInteger != 0) { currentInteger = in.nextInt(); if (currentInteger != 0) { // more code (input, update minValue, maxValue) } // more code (find range, average, and print results) 1 March 2013Birkbeck College, U. London8

Exercise 3: code(3) n = n+1; total += currentInteger; if (n == 1) { minValue = currentInteger; maxValue = currentInteger; } else { minValue = Math.min(currentInteger, minValue); maxValue = Math.max(currentInteger, maxValue); } 1 March 2013Birkbeck College, U. London9

Exercise 3: code(4) If(n==0) {System.out.println(No numbers input);} else { double average = total/n; int range = maxValue-minValue+1; System.out.printf("average: %10.3f\n", average); System.out.println("smallest value: "+minValue); System.out.println("largest value: "+maxValue); System.out.println("range: "+range); } 1 March 2013Birkbeck College, U. London10

Methods A method is a sequence of instructions with a name. When a method is called, the instructions are executed. When the execution of the instructions is complete, the method may return a value to the calling program. 1 March 2013Birkbeck College, U. London11

Example public static void main(String[] args) { double z = Math.pow(2, 3); /* The method Math.pow has parameter values 2, 3 and returns the value 8. */ … } 1 March 2013Birkbeck College, U. London12

Specification of a Method Name of the method Name and type of each parameter Type of the result Specification of the way in which the result depends on the parameters 1 March 2013Birkbeck College, U. London13

Specification of Math.pow Full name: java.lang.Math.pow Name and type of each parameter: double x, double y Type of the result: double This method returns the value x y (x>0, or x=0 and y>0, or x<0 and y is an integer) (See JFE page 528. Note: the specification does not describe the way in which the method is implemented.) 1 March 2013Birkbeck College, U. London14

Method Declaration public static double cubeVolume(double sideLength) { double volume = sideLength*sideLength*sideLength; return volume; } 1 March 2013Birkbeck College, U. London15

First Line of the Method Declaration 1 March 2013Birkbeck College, U. London16 public static doublecubeVolume(doublesideLength) type of return value name of method type of parameter variable name of parameter variable

Remaining Part of the Method Declaration public static double cubeVolume(double sideLength) { double volume = sideLength*sideLength*sideLength; return volume; // exit method and return the result } 1 March 2013Birkbeck College, U. London17 method body, executed when the method is called

Example of the use of a Method public class Cubes { public static void main(String[] args) { double result = cubeVolume(2); System.out.println("A cube with side length 2 has volume "+result); } public static double cubeVolume(double sideLength) { double volume = sideLength*sideLength*sideLength; return volume; } 1 March 2013Birkbeck College, U. London18

Method Comments /** Computes the volume of a cube. @param sideLength the side length of the cube @return the volume */ public static cubeVolume(double sideLength) { double volume = sideLength*sideLength*sideLength; return volume; } 1 March 2013Birkbeck College, U. London19

javadoc Convention for Method Comments Comments are enclosed in /** and */ delimiters. Each @param clause describes a parameter variable. The @return clause describes the return value. The comment does not document the implementation, only the specification. 1 March 2013Birkbeck College, U. London20

Parameter Passing Parameter variable: created when the method is called. Formal parameter: alternative name for a parameter variable. Parameter value: value used to initialise a parameter variable. Actual parameter, argument: alternative names for a parameter value. 1 March 2013Birkbeck College, U. London21

Example of Parameter Passing double result = cubeVolume(2); /* On calling cubeVolume, the parameter variable sideLength is created and initialised with the parameter value 2. */ 1 March 2013Birkbeck College, U. London22

Return from a Method Call double result = cubeVolume(2); /* The variable result is assigned the value 8. All other results of the computations within cubeVolume are discarded. */ 1 March 2013Birkbeck College, U. London23

Multiple Method Calls double result1 = cubeVolume(2); double result2 = cubeVolume(10); /* The variables sideLength and volume used in the calculation of result1 are discarded. New variables are created for the calculation of result2. */ 1 March 2013Birkbeck College, U. London24

Attempt to Modify Parameters public static double addTax(double price, double rate) { double tax = price*rate/100; price = price+tax; return tax; } … double total = 10; addTax(total, 7.5); /* The attempt to update the value of total from 10 to 10.75 fails. The value 10 of total is unchanged. */ 1 March 2013Birkbeck College, U. London25

Methods Without Return Values The absence of a return value is indicated by the reserved word void. Example: a method which only prints text. 1 March 2013Birkbeck College, U. London26

Example public static void boxString(String str) { int n = str.length(); for(int i = 0; i < n+2; i++){System.out.print("-");} System.out.println(); System.out.println("|"+str+"|"); for(int i = 0; i < n+2; i++){System.out.print("-");} System.out.println(); //no return statement } … boxString("Hello"); // correct call to boxString result = boxString("Hello"); // incorrect call to boxString 1 March 2013Birkbeck College, U. London27

Similar presentations