1 march 2013birkbeck college, u. london1 introduction to programming lecturer: steve maybank...

27
1 March 2013 Birkbeck College, U. London 1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems [email protected] Spring 2013 Week 8: Methods

Upload: marissa-flanagan

Post on 28-Mar-2015

216 views

Category:

Documents


0 download

TRANSCRIPT

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

1 March 2013 Birkbeck College, U. London 1

Introduction to Programming

Lecturer: Steve Maybank

Department of Computer Science and Information Systems

[email protected] 2013

Week 8: Methods

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

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 2013 Birkbeck College, U. London 2

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

Exercise 2: rectangleWrite 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 2013 Birkbeck College, U. London 3

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

Exercise 2: codeimport 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 2013 Birkbeck College, U. London 4

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

Exercise 2: more codefor(int i = 1; i <= numberRows; i++){

for(int j = 1; j <= numberColumns; j++)

{System.out.print("*");

}System.out.println();

}

1 March 2013 Birkbeck College, U. London 5

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

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 averageThe smallest of the valuesThe largest of the valuesThe range

1 March 2013 Birkbeck College, U. London 6

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

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 2013 Birkbeck College, U. London 7

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

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 2013 Birkbeck College, U. London 8

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

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 2013 Birkbeck College, U. London 9

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

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 2013 Birkbeck College, U. London 10

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

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 2013 Birkbeck College, U. London 11

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

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 2013 Birkbeck College, U. London 12

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

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 2013 Birkbeck College, U. London 13

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

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

xy (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 2013 Birkbeck College, U. London 14

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

Method Declaration

public static double cubeVolume(double sideLength)

{double volume = sideLength*sideLength*sideLength;return volume;

}

1 March 2013 Birkbeck College, U. London 15

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

First Line of the Method Declaration

1 March 2013 Birkbeck College, U. London 16

public static

double

cubeVolume

( double sideLength

)

type of return value

name of method

type of parameter variable

name of parameter variable

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

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 2013 Birkbeck College, U. London 17

method body, executedwhen the method iscalled

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

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 2013 Birkbeck College, U. London 18

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

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 2013 Birkbeck College, U. London 19

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

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 2013 Birkbeck College, U. London 20

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

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 2013 Birkbeck College, U. London 21

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

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 2013 Birkbeck College, U. London 22

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

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 2013 Birkbeck College, U. London 23

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

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 2013 Birkbeck College, U. London 24

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

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 2013 Birkbeck College, U. London 25

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

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 2013 Birkbeck College, U. London 26

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

Examplepublic 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 boxStringresult = boxString("Hello"); // incorrect call to boxString

1 March 2013 Birkbeck College, U. London 27