15
COM S 207 Method Instructor: Ying Cai Department of Computer Science Iowa State University [email protected]

COM S 207 Method Instructor: Ying Cai Department of Computer Science Iowa State University

Embed Size (px)

DESCRIPTION

COM S 207 Method Instructor: Ying Cai Department of Computer Science Iowa State University [email protected]. Repeated Code. int hours = 0; while (hours < 1 || hours > 12) { System.out.println( “ Enter Hours (1-12): ” ); hours = in.nextInt(); } int minutes = 0; - PowerPoint PPT Presentation

Citation preview

Page 1: COM S 207 Method Instructor: Ying Cai Department of Computer Science Iowa State University

COM S 207Method

Instructor: Ying Cai

Department of Computer ScienceIowa State [email protected]

Page 2: COM S 207 Method Instructor: Ying Cai Department of Computer Science Iowa State University

Repeated Codeint hours = 0;while (hours < 1 || hours > 12){ System.out.println(“Enter Hours (1-12):”); hours = in.nextInt();}

int minutes = 0;while (minutes < 0 || minutes > 59){ System.out.println(“Enter Minutes (0-59):”); minutes = in.nextInt();}

int seconds = 0;while (seconds < 0 || seconds > 59){ System.out.println(“Enter Seconds (0-59):”); seconds = in.nextInt();}

Page 3: COM S 207 Method Instructor: Ying Cai Department of Computer Science Iowa State University

Turn Repeated Code into a Method

public static int readValueBetween(String s, int low, int high){ int input = low-1; while (input < low || input > high) { System.out.println(“Enter ” + s + “ (“ + low + “-” + high + “):”); input = in.nextInt(); }

return input;}

int hours = readValueBetween(“hours”, 1, 12);int minutes = readValueBetween(“minutes”, 0, 59);int seconds = readValueBetween(“seconds”, 0, 59);

Page 4: COM S 207 Method Instructor: Ying Cai Department of Computer Science Iowa State University

A method is a named sequence of instructions

public static void main(String[] args){ double z = Math.pow(2, 3);}

Math.pow(2, 3) is a method to compute 2^3

black box

math.pow

2 3

8

The implementation is hidden from the users. Users need to know only the specification of the methods, no the implementation:If you provide parameter values X and Y, the method returns X^YWhat is the return value of Math.pow(Math.pow(2, 2), 2)?

Page 5: COM S 207 Method Instructor: Ying Cai Department of Computer Science Iowa State University

Implementing methods

public static double cubeVolume(double sideLength){ double volume = sideLength * sideLength * sideLength; return volume;}

Example: volume of a cube

method body: executed when the method is called

type of return value

name of method

type of parameter variable

Name of parameter variables

Page 6: COM S 207 Method Instructor: Ying Cai Department of Computer Science Iowa State University

public class Cubes { public static void main(String[] args) { double result1 = cubeVolume(2.0); double result2 = cubeVolume(10.0); System.out.println(“Cube with side 2.0 has volume “ + result1); System.out.println(“Cube with side 10.0 has volume “ + result1); } public static double cubeVolume(double sideLength) { double volume = sideLength * sideLength * sideLength; return volume; }}

Page 7: COM S 207 Method Instructor: Ying Cai Department of Computer Science Iowa State University

public class Mystery{ public static void main(String[] args) { double number = mystery(2, 5); System.out.println(“The mystery function returns: “ + number); double cube = cubeVolume(5.0); System.out.println(“The cubeVolume function returns: “ + cube); }

public static double cubeVolume(double sideLength) { double volume = sideLength * sideLength * sideLength; return volume; } public static int mystery(int x, int y) { int result = (x + y) / (y – x); return result; }}

What will be printed out?

Page 8: COM S 207 Method Instructor: Ying Cai Department of Computer Science Iowa State University

Exercise

Write java code to do the following tasks:

1)1 + 2 + 3 + … + 10002)231 + 232 + 233 + … + 9993)30 + 31 + … + 89894)41 + 42 + … + 2345

Page 9: COM S 207 Method Instructor: Ying Cai Department of Computer Science Iowa State University

Exercise

Write java code to do the following tasks:

1)1 + 2 + 3 + … + 10002)231 + 232 + 233 + … + 9993)30 + 31 + … + 89894)41 + 42 + … + 2345

Page 10: COM S 207 Method Instructor: Ying Cai Department of Computer Science Iowa State University

public class Mystery{ public static void main(String[] args) { for (int x=1; x<=5; x++) { System.out.println(mystery(x)); } }

public static int mystery(int x) { int result = 0; for (int i=0; i<=x; i++) { result = result + i; }

return result; }}

Parameter Passing

• Memory space of a parameter

• Valid scope of variables

• Modification of a parameter within a method

• Return value must match (if it is void, no return value)

Page 11: COM S 207 Method Instructor: Ying Cai Department of Computer Science Iowa State University

Excercise

Write a method to compute the volume of a pyramid whose base is a square

height

base length

volume = 1/3 * base length^2 * height

Page 12: COM S 207 Method Instructor: Ying Cai Department of Computer Science Iowa State University

Write a method that does this work

• Take two parameters, double a and integer b, and return a^b

• Take three integer numbers as its arguments, and return the smallest of the numbers

• Take three doubles as its arguments, and return the average of them

• Take three integers as its arguments and return true if they are sorted

Page 13: COM S 207 Method Instructor: Ying Cai Department of Computer Science Iowa State University

Write the following methods

• int lastDigit(int n) //return the last digit of n

• int firstDigit(int n) //returning the first digit of n

• int digits(int n) //return the number of digits of n

Page 14: COM S 207 Method Instructor: Ying Cai Department of Computer Science Iowa State University

Telephone Number Converter

Write a program that translates a telephone number with letters in it (such as 1-800-FLOWERS) into the actual phone number. Use the standard letters on a phone pad.

Page 15: COM S 207 Method Instructor: Ying Cai Department of Computer Science Iowa State University

Password Checking

Write a program that asks for a password and then asks again to confirm it, if passwords don’t match or the following rules are not fullfilled, prompt again:1.The password must be at least 8 characters long.2.The password must have at least one uppercase and one lowercase letter3.The password must have at least one digit