1 / 9

Introduction to Computing Concepts

Introduction to Computing Concepts. Note Set 18. Methods. Using the divide and conquer method of problem solving. Take a big problem Break it down to component pieces Code those pieces separately. Allows the programmer to reuse code rather than re-typing/redeveloping it

hall
Télécharger la présentation

Introduction to Computing Concepts

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Introduction to Computing Concepts Note Set 18

  2. Methods • Using the divide and conquer method of problem solving. • Take a big problem • Break it down to component pieces • Code those pieces separately. • Allows the programmer to reuse code rather than re-typing/redeveloping it • Think about all of the GUI components you’ve used

  3. Anatomy of a Method private double square (double number) { return number * number; }

  4. Method Header & Body private double square (double number) { return number * number; }

  5. Multiple Parameters private double sum(double number1, double number2) { return number1 + number2; }

  6. No parameters • Void return type – Nothing is returned • Empty () – method doesn’t accept any params. private void printHello() { System.out.println(“Hello”); }

  7. Calling a method • Making the method execute is called invoking the method. double val = square (1.23); Invokes the method and sends 1.23 to the parameter

  8. Flow of Control • After a method is done executing, it returns to the place where it was called. public void doubleAndPrint(int x, int y) { int ret = square(10); System.out.println(ret); ret = square(20); System.out.println(ret); } public void square(intval) { int answer = val * val; return answer; }

  9. Practice • Implement a method that will return the larger of two parameters passed to it.

More Related