150 likes | 171 Views
COM S 207 Method Instructor: Ying Cai Department of Computer Science Iowa State University yingcai@iastate.edu. Repeated Code. int hours = 0; while (hours < 1 || hours > 12) { System.out.println( “ Enter Hours (1-12): ” ); hours = in.nextInt(); } int minutes = 0;
E N D
COM S 207 Method Instructor: Ying Cai Department of Computer Science Iowa State University yingcai@iastate.edu
Repeated Code int 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(); }
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);
A method is a named sequence of instructions Math.pow(2, 3) is a method to compute 2^3 public static void main(String[] args) { double z = Math.pow(2, 3); } 2 3 math.pow black box 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^Y What is the return value of Math.pow(Math.pow(2, 2), 2)?
Implementing methods Example: volume of a cube type of parameter variable Name of parameter variables name of method type of return value public static double cubeVolume(double sideLength) { double volume = sideLength * sideLength * sideLength; return volume; } method body: executed when the method is called
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; } }
What will be printed out? 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; } }
Exercise • Write java code to do the following tasks: • 1 + 2 + 3 + … + 1000 • 231 + 232 + 233 + … + 999 • 30 + 31 + … + 8989 • 41 + 42 + … + 2345
Exercise • Write java code to do the following tasks: • 1 + 2 + 3 + … + 1000 • 231 + 232 + 233 + … + 999 • 30 + 31 + … + 8989 • 41 + 42 + … + 2345
Parameter Passing 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; } } • 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)
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
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
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
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.
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: The password must be at least 8 characters long. The password must have at least one uppercase and one lowercase letter The password must have at least one digit