Presentation is loading. Please wait.

Presentation is loading. Please wait.

Methods in Java CSC 1401: Introduction to Programming with Java Week 7 – Lecture 2 Wanda M. Kunkle.

Similar presentations


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

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

2 2 Problem Solving in General Divide and conquer Divide and conquer –Fundamental approach to human problem solving –Process consisting of two stages: 1.Decomposition 2.Composition

3 3 Problem Solving in General Divide and conquer Divide and conquer 1.Decomposition  Divide a complex problem into simpler problems  Solve each simple problem one at a time 2.Composition  Combine the solutions to the simple problems to …  Form a solution to the complex problem

4 4 Problem Solving in Java How do we implement divide and conquer in Java? How do we implement divide and conquer in Java? 1.Decomposition  Use stepwise programming to break a larger task into smaller tasks which can be programmed one at a time (e.g., single statements)

5 5 Problem Solving in Java How do we implement divide and conquer in Java? How do we implement divide and conquer in Java? 2.Composition  Combine the smaller program units to form a complete program using composition mechanisms.  Composition mechanisms that you have used so far are: –Structured statements  if, if-else, switch, for, while, do-while –Data structures  Objects such as input, output, and String  Text files –Methods  read, readline, readint, readdouble, writeln, etc.

6 6 Methods in Java A method is a piece of code that performs a specific task. A method is a piece of code that performs a specific task. There are two main types of methods in Java: There are two main types of methods in Java: –Built-in  A built-in method is a method written by the authors of a programming language for use by other programmers.  A built-in method you have seen is Math.pow (If you recall, I used it in a demo program, powers.java) powers.java –Programmer-defined  A programmer-defined method is a method written by a programmer for his own use.  Some programmer-defined methods you have used are read, readint, and writeln

7 7 Methods in Java The author of your text further divides methods into two general categories: The author of your text further divides methods into two general categories: –Functions  A function is a method that performs a task and returns a value. –Procedures  A procedure is a method that performs a task but does not return a value.

8 8 Built-in Methods Java’s creators at Sun Microsystems have provided us with many built-in methods. Java’s creators at Sun Microsystems have provided us with many built-in methods. For now, we’ll just look at some of the built-in math methods, which can be viewed at: For now, we’ll just look at some of the built-in math methods, which can be viewed at: –http://java.sun.com/javase/6/docs/api/index. html http://java.sun.com/javase/6/docs/api/index. htmlhttp://java.sun.com/javase/6/docs/api/index. html

9 9 Sample Program Now let’s look at a sample program that demonstrates some of these methods: Now let’s look at a sample program that demonstrates some of these methods: –mathFunctionDemo1.java mathFunctionDemo1.java

10 10 Programmer-Defined Methods The program we just looked at contains a lengthy section of code at the beginning that does nothing more than display a menu of options to the user. The program we just looked at contains a lengthy section of code at the beginning that does nothing more than display a menu of options to the user. This “clutters up” the program, detracting from its main purpose, which is to demonstrate the built-in math functions. This “clutters up” the program, detracting from its main purpose, which is to demonstrate the built-in math functions. Would it not be better to move this code elsewhere and simply call it as needed, as we do with the math functions? Would it not be better to move this code elsewhere and simply call it as needed, as we do with the math functions?

11 11 Programmer-Defined Methods This, in essence, is the purpose of methods. This, in essence, is the purpose of methods. Programmer-defined methods provide us with a means of defining routines that we, as programmers, need to use repeatedly without repeating the code. Programmer-defined methods provide us with a means of defining routines that we, as programmers, need to use repeatedly without repeating the code.

12 12 Sample Program The following sample program demonstrates a programmer-defined method that our author would classify as a procedure: The following sample program demonstrates a programmer-defined method that our author would classify as a procedure: –mathFunctionDemo2.java mathFunctionDemo2.java

13 13 Sample Program The following sample program demonstrates a programmer-defined method that our author would classify as a function: The following sample program demonstrates a programmer-defined method that our author would classify as a function: –ClassifyDayOfWeek.java ClassifyDayOfWeek.java –It improves upon the original DaysOfWeek.java program. DaysOfWeek.java

14 14 Friday’s Lecture More on methods … More on methods …


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

Similar presentations


Ads by Google