Download presentation
Presentation is loading. Please wait.
Published byBerenice Cooper Modified over 9 years ago
1
COM S 207 Method Instructor: Ying Cai Department of Computer Science Iowa State University yingcai@iastate.edu
2
Repeated Code int hours = 0; while (hours 12) { System.out.println(“Enter Hours (1-12):”); hours = in.nextInt(); } int minutes = 0; while (minutes 59) { System.out.println(“Enter Minutes (0-59):”); minutes = in.nextInt(); } int seconds = 0; while (seconds 59) { System.out.println(“Enter Seconds (0-59):”); seconds = in.nextInt(); }
3
Turn Repeated Code into a Method public static int readValueBetween(String s, int low, int high) { int input = low-1; while (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);
4
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 23 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)?
5
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
6
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; }
7
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?
8
Exercise Write java code to do the following tasks: 1)1 + 2 + 3 + … + 1000 2)231 + 232 + 233 + … + 999 3)30 + 31 + … + 8989 4)41 + 42 + … + 2345
9
Exercise Write java code to do the following tasks: 1)1 + 2 + 3 + … + 1000 2)231 + 232 + 233 + … + 999 3)30 + 31 + … + 8989 4)41 + 42 + … + 2345
10
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)
11
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
12
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
13
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
14
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.
15
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 letter 3.The password must have at least one digit
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.