Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 More About Methods in Java CSC 1401: Introduction to Programming with Java Week 7 – Lecture 3 Wanda M. Kunkle.

Similar presentations


Presentation on theme: "1 More About Methods in Java CSC 1401: Introduction to Programming with Java Week 7 – Lecture 3 Wanda M. Kunkle."— Presentation transcript:

1 1 More About Methods in Java CSC 1401: Introduction to Programming with Java Week 7 – Lecture 3 Wanda M. Kunkle

2 2 Why Use Methods? The main method ( public static void main (String[] args) ) of the program should focus on the primary tasks of the program. The main method ( public static void main (String[] args) ) of the program should focus on the primary tasks of the program. Methods make it possible to use code repeatedly without typing the code repeatedly. Methods make it possible to use code repeatedly without typing the code repeatedly. Methods facilitate code reuse by allowing us to use the code by simply invoking (calling) the method in which it has been placed. Methods facilitate code reuse by allowing us to use the code by simply invoking (calling) the method in which it has been placed. Methods can be placed in their own separate classes for use with many different Java programs. Methods can be placed in their own separate classes for use with many different Java programs. Methods support modular programming, an approach to programming in which programs are constructed from reusable components (modules), each of which performs a specific task. Methods support modular programming, an approach to programming in which programs are constructed from reusable components (modules), each of which performs a specific task.

3 3 How Do We Define Methods? 1. Decide what task the method is to perform. 2. Choose a name for the method that is descriptive of the task. 3. Determine what values (if any) must be passed into the method so that it can perform its task. 4. Determine what value (if any) the method must return. 5. Write the code to carry out the method’s task.

4 4 Problem Write a method to determine whether or not a character ch is a lowercase letter. Write a method to determine whether or not a character ch is a lowercase letter.

5 5 Defining a Method to Solve a Problem 1. Decide what task the method is to perform. –The method is to determine whether or not a character ch is a lowercase letter.

6 6 Defining a Method to Solve a Problem 2. Choose a name for the method that is descriptive of the task. –Suggestions?

7 7 Defining a Method to Solve a Problem 3. Determine what values (if any) must be passed into the method so that it can perform its task. –Answer? (Hint: There’s only one value. What is it?)

8 8 Defining a Method to Solve a Problem 4. Determine what value (if any) the method must return. –Now we have to make some decisions.  How do we want the method to indicate that a letter is upper- or lowercase? –Return a String: “lowercase” or “uppercase” –Return a boolean: true if lowercase, false otherwise –Return an integer: 1 if lowercase, 0 otherwise  I know what approach I’d use. What approach would you use?

9 9 Defining a Method to Solve a Problem 5. Write the code to carry out the method’s task. –Once again we have to make some decisions.  What decision structures should we use? –if –switch  Depending upon our choice of decision structure, do we need to use boolean expressions? –ch == ‘a’ || ch == ‘b’ || … || ch == ‘z’  Do we need to use any local variables (variables declared inside the method)?  I know what choices I’d make. What choices would you make?

10 10 Possible Solution The method shown below: // Classify a letter as lower- or uppercase and // return the result of the test static boolean isLowerCase(char ch) { boolean isLower = false; if ('a' <= ch && ch <= 'z') isLower = true; return isLower; } // end isLowerCase The method shown below: // Classify a letter as lower- or uppercase and // return the result of the test static boolean isLowerCase(char ch) { boolean isLower = false; if ('a' <= ch && ch <= 'z') isLower = true; return isLower; } // end isLowerCase

11 11 Sample Program Now let’s look at a sample program that uses this method: Now let’s look at a sample program that uses this method: –DetermineLetterCase.java DetermineLetterCase.java

12 12 General Form of a Java Method static boolean isLowerCase(char ch) { boolean isLower = false; if ('a' <= ch && ch <= 'z') isLower = true; return isLower; } // end isLowerCase static boolean isLowerCase(char ch) { boolean isLower = false; if ('a' <= ch && ch <= 'z') isLower = true; return isLower; } // end isLowerCase Function heading Function definition (i.e., the function itself) Function nameFunction return type Function parameter Function body (i.e., the code)

13 13 Changing the subject …

14 14 One More String Method (You’ll need it for today’s lab.) startsWith(prefix) startsWith(prefix) –Returns true if the calling String object starts with prefix; otherwise, returns false –Example:  output out = new output(); String professor_name = “Dr. Johnson”; if (professor_name.startsWith(“Dr.")) out.writeln("\nYou have a Ph.D."); else out.writeln("\nYou do not have a Ph.D."); // What do you think is displayed?

15 15 String Methods Recall that the String methods can be viewed at: Recall that the String methods can be viewed at: –http://java.sun.com/javase/6/docs/api/index.html http://java.sun.com/javase/6/docs/api/index.html Just look for the String class under All Classes in the lower left-hand frame. Just look for the String class under All Classes in the lower left-hand frame.

16 16 One more thing …

17 17 Have a nice break! Have a nice break!


Download ppt "1 More About Methods in Java CSC 1401: Introduction to Programming with Java Week 7 – Lecture 3 Wanda M. Kunkle."

Similar presentations


Ads by Google