1 / 31

Understanding Functions in Java: Wake-Up Routine Example and Insertion Sort

This lecture focuses on functions in Java, using a relatable wake-up routine as an example. It introduces key concepts such as function definition, modifiers, and void return types. The session explores the construction of simple functions such as `printPhraseCharacters` and `areaOfCircle`, demonstrating how to define and call functions. Additionally, we cover nested loops and arrays in the context of performing an insertion sort. By the end of this lecture, you'll have a clearer understanding of how to create and use functions effectively in Java programming.

dinh
Télécharger la présentation

Understanding Functions in Java: Wake-Up Routine Example and Insertion Sort

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. COP3502: Introduction to CIS I Lecture 7

  2. functions

  3. public class wakeUp { public static voidmain(Stringargs[]) { getOutOfBed(); brushTeeth(); takeShower(); makeCoffee(); eatBreakfast(); } }

  4. function definition

  5. “void” functions <modifiers> void<function name> ( <arg type> <arg name> , …) { <function body> } public static voidprintPhraseCharacters(Stringphrase) { char characters[] = phrase.toCharArray(); for (inti = 0; i < characters.length; i++) { System.out.println(characters[i]); } }

  6. function call public class printTest { public static voidmain(Stringargs[]) { StringmyName = “Sean”; printPhraseCharacters(myName); } }

  7. public class printTest { public static voidmain(Stringargs[]) { ….. } public static voidprintPhraseCharacters(String phrase) { ….. } }

  8. functions w/ return values <modifiers> <return type> <function name> ( <arg type> <arg name> , …) { <function body> } public static doubleareaOfCircle(doubleradius) { double area = Math.PI * (radius * radius); return area; }

  9. return keyword returnexpression; return x; return x + 5; returnrandomNumber();

  10. function call public class printTest { public static voidmain(Stringargs[]) { double radius = 5.2; double area = areaOfCircle(radius); } }

  11. nested loops

  12. arrays … 9 0 1 2 3 4 7 8 5 6

  13. java commandLineTest Happy happy joy joy 0 1 2 3

  14. int[] list = {8, 4, 1, 7, 3, 2} int list[] = {8, 4, 1, 7, 3, 2} 0 1 2 3 4 5

  15. sorting

  16. insertion sort build sorted list one element at a time

  17. insertion sort build sorted list one element at a time

  18. insertion sort if (red < green arrow) { SWAP }

  19. insertion sort if (red < green arrow) { SWAP }

  20. insertion sort if (red < green arrow) { SWAP }

  21. insertion sort if (red < green arrow) { SWAP }

  22. insertion sort if (red < green arrow) { SWAP }

  23. insertion sort if (red < green arrow) { SWAP }

  24. insertion sort if (red < green arrow) { SWAP }

  25. insertion sort if (red < green arrow) { SWAP }

  26. insertion sort if (red < green arrow) { SWAP }

  27. insertion sort if (red < green arrow) { SWAP }

  28. insertion sort if (red < green arrow) { SWAP }

  29. insertion sort if (red < green arrow) { SWAP }

  30. insertion sort if (red < green arrow) { SWAP }

  31. insertion sort if (red < green arrow) { SWAP }

More Related