1 / 53

Warm-Up: April 21

Warm-Up: April 21. Write a loop (type of your choosing) that prints every 3 rd number between 10 and 50. . User-Defined Methods. Pre-AP Computer Science, Cycle 6. Case Study: Sum the integers between 1 and 10, 20 and 30, and 35 to 45. . int sum=0; for ( int i =1; i <= 10; i ++)

bree
Download Presentation

Warm-Up: April 21

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. Warm-Up: April 21 • Write a loop (type of your choosing) that prints every 3rd number between 10 and 50.

  2. User-Defined Methods Pre-AP Computer Science, Cycle 6

  3. Case Study: Sum the integers between 1 and 10, 20 and 30, and 35 to 45. int sum=0; for (inti=1; i <= 10; i++) sum = sum + i; System.out.println(“Sum from 1-10 is “ + sum); sum=0; for (inti=20; i <= 30; i++) sum = sum + i; System.out.println(“Sum from 20-30 is “ + sum); sum=0; for (inti=35; i <= 45; i++) sum = sum + i; System.out.println(“Sum from 35-45 is “ + sum);

  4. Case Study: Sum the integers between 1 and 10, 20 and 30, and 35 to 45. int sum=0; for (inti=1; i <= 10; i++) sum = sum + i; System.out.println(“Sum from 1-10 is “ + sum); sum=0; for (inti=20;i <= 30; i++) sum = sum + i; System.out.println(“Sum from 20-30 is “ + sum); sum=0; for (inti=35; i <= 45; i++) sum = sum + i; System.out.println(“Sum from 35-45 is “ + sum);

  5. Case Study – Using Methods public static int sum(int start, int end) { int sum=0; for (inti = start; i<= end; i++) sum = sum + i; return sum; } public static void main(String[] args) { System.out.println(“Sum from 1-10 is “ + sum(1,10)); System.out.println(“Sum from 20-30 is “ + sum(20,30)); System.out.println(“Sum from 35-45 is “ + sum(35,45)); }

  6. Methods • Associated with classes • Define what objects are capable of doing • Actions • Essentially any command that ends with a pair of parentheses (empty or otherwise) • System.out.print() • Math.max() • console.nextInt()

  7. User-Defined Methods • The methods we’ve used so far have come pre-written, stored in other files called packages • Usually have to import those files into our programs in order to use those pre-written methods (java.util.* to use Scanner) • However, we can write our own methods! • User-defined methods • Examples of possibly useful methods to write • Sum of integers between two numbers • isPrime • isEven or isOdd

  8. Why use methods? • Like loops, they help make code • simpler, • shorter, • more efficient, • and more organized • Modularization!

  9. Dissecting a Method public static int sum(int start, int end) { int sum=0; for (inti = start; i <= end; i++) sum = sum + i; return sum; }

  10. Dissecting a Method public static int sum(int start, int end) { int sum=0; for (inti = start; i <= end; i++) sum = sum + i; return sum; } Modifier – states whether the method is public or private (for our class, ALWAYS PUBLIC STATIC)

  11. Dissecting a Method public static int sum(int start, int end) { int sum=0; for (inti = start; i <= end; i++) sum = sum + i; return sum; } Return type – data type of the information the method will return (int, double, String, void)

  12. Dissecting a Method public static intsum(int start, int end) { int sum=0; for (inti = start; i <= end; i++) sum = sum + i; return sum; } Name of the method – this is the name that will be used when we call the method later (use it)

  13. Dissecting a Method public static int sum(int start, int end) { int sum=0; for (inti = start; i <= end; i++) sum = sum + i; return sum; } Parameters – list of variables that we will pass to the method for it to use in the body code (may or may not be parameters)

  14. Dissecting a Method public static int sum(int start, int end) { int sum=0; for (inti = start; i <= end; i++) sum = sum + i; return sum; } Method body – statements associated with the method to make it perform the necessary task

  15. Dissecting a Method public static int sum(int start, int end) { int sum=0; for (inti = start; i <= end; i++) sum = sum + i; return sum; } Method return – variable or value the method should return as its “answer”

  16. 4 Types of Methods • Methods may or may not return values • Methods may or may not accept parameters • 4 type of methods • No parameters OR returns (Type I) • No parameters WITH returns (Type II) • Parameters with NO returns (Type III) • Parameters AND returns (Type IV)

  17. Calling a Method • Defining a method does not mean it is automatically used • You must call the method within the main method in order to use it • The method executes, then returns to the same spot in the main method where it was called

  18. Calling a Method public static int max(int a, int b) { if (a >= b) return a; else return b; } public static void main(String[] args) { int num1=45; int num2=32; int biggest = max(num1, num2); System.out.println(“Biggest was “ + biggest); }

  19. Calling a Method public static int max(int a, int b) { if (a >= b) return a; else return b; } public static void main(String[] args) { int num1=45; int num2=32; int biggest = max(num1, num2); System.out.println(“Biggest was “ + biggest); }

  20. Calling a Method public static int max(int a, int b) { if (a >= b) return a; else return b; } public static void main(String[] args) { int num1=45; int num2=32; int biggest = max(num1, num2); System.out.println(“Biggest was “ + biggest); }

  21. Calling a Method public static int max(int a, int b) { if (a >= b) return a; else return b; } public static void main(String[] args) { int num1=45; int num2=32; int biggest = max(num1, num2); System.out.println(“Biggest was “ + biggest); }

  22. Calling a Method public static int max(int a, int b) { if (a >= b) return a; else return b; } public static void main(String[] args) { int num1=45; int num2=32; int biggest = max(num1, num2); System.out.println(“Biggest was “ + biggest); }

  23. Calling a Method public static int max(int a, int b) { if (a >= b) return a; else return b; } public static void main(String[] args) { int num1=45; int num2=32; int biggest = max(num1, num2); System.out.println(“Biggest was “ + biggest); }

  24. Calling a Method public static int max(int a, int b) { if (a >= b) return a; else return b; } public static void main(String[] args) { int num1=45; int num2=32; int biggest = max(num1, num2); System.out.println(“Biggest was “ + biggest); }

  25. Warm-Up: April 22 • If you were to write a method called “isOdd”, which determines whether a number is odd or not, • What would be your parameter(s) (if any)? • What would be your return (if any)?

  26. Mastery Level: Awesome Passes ALL LEFT-OVER ML:A PASSES NOT USED FOR AN EXEMPTION FOR AN IN-CLASS OR FINAL EXAM MAY BE REDEEMED FOR EXTRA CREDIT ON ANY EXAM AT THE FOLLOWING EXCHANGE RATE: 1 PASS = 2 E.C. POINTS *Note: Exam scores may not exceed 100

  27. Type I Methods Pre-AP Computer Science, Cycle 6

  28. Methods - Review public static int max(int a, int b) { if (a >= b) return a; else return b; } public static void main(String[] args) { int num1=45; int num2=32; int biggest = max(num1, num2); System.out.println(“Biggest was “ + biggest); }

  29. Parameters and Returns • Parameters • Information passed to the method from the main method • What goes inside the parenthesis • max(a,b)  a,b are parameters • Returns • The answer the method gives back • max(a,b)  the largest would be the return • isPrime(num)  true or false

  30. Type I Methods • No parameters OR returns • Return type will ALWAYS be void • Parenthesis will ALWAYS be empty • NO return statement • Used exclusively to carry out an action • Output a message • Perform a uniform calculation AND output the answer • Uniform  numbers never change

  31. Example – Type I Method • Method that outputs a name public static void name() { System.out.println(“Rachel Alexander”); }

  32. Example – Calling name() public static void name() { System.out.println(“Rachel Alexander”); } public static void main(String[] args) { System.out.print(“Hello “); name(); System.out.println(“Nice to meet you!”); }

  33. Example 2 – Powers of 2 • Write a method that prints the first 10 powers of 2 public static void powersOf2( ) { for (inti=1; i<=10; i++) { System.out.println(pow(2,i)); } }

  34. Warm-Up: April 24 • Write a method called warmup() that prints the word “warmup” 3 times

  35. Warm-Up: April 24 • Write a method called warmup() that prints the word “warmup” 3 times public static void warmup() { System.out.println(“Warmup!”); System.out.println(“Warmup!”); System.out.println(“Warmup!”); }

  36. Type II and III Methods Pre-AP Computer Science, Week 6

  37. Type I Methods - Review • No parameters OR returns • Return type will ALWAYS be void • Parenthesis will ALWAYS be empty • NO return statement • Used exclusively to carry out an action • Output a message • Perform a uniform calculation AND output the answer • Uniform  numbers never change

  38. Example – Type I Method • Method that outputs a name public static void name() { System.out.println(“Rachel Alexander”); }

  39. Type II Methods • No parameters WITH returns • Parenthesis will ALWAYS be empty • Will ALWAYS have a return statements • Return data type will match that of the return • Used for uniform calculations where the answer is returned • Method call MUST be set equal to a variable

  40. Type II Method – Example A public static intgetRandom() { int random = Math.random()*100; return random; } public static void main(String[] args) { intnewNum = getRandom(); System.out.print(“Your new number is: “); System.out.println(newNum); }

  41. Type II Method – Example B public static char letter() { int random = Math.random()*10; if (random < 5) return ‘a’; else return ‘b’; } public static void main(String[] args) { for (inti=0; i<50; i++) { char randLetter = letter(); System.out.println(randLetter); } }

  42. Type III Methods • Parameters with NO returns • Parenthesis will NOT be empty • Will NOT have a return statements • Return data type will ALWAYS be VOID • Used for non-uniform calculations where the answer is immediately outputted • Method call is NOT set equal to a variable

  43. Type III Method – Example A public static void isOdd(intnum) { if (num % 2 == 1) System.out.println(num + “ is odd.”); else System.out.println(num + “is even.”); } public static void main(String[] args) { intnum = console.nextInt(); isOdd(num); }

  44. Type III Method – Example B public static void largest(int a, int b) { if (a >= b) System.out.println(a + “is largest.”); else System.out.println(b + “ is largest.”); } public static void main(String[] args) { intnum = console.nextInt(); int num2 = console.nextInt(); largest(num, num2); }

  45. Warm-Up: April 25 • When calling a method that returns a value, why must we set the method call equal to a variable? Why int biggest = max(a,b); and not max(a,b); ?? *When finished, turn in your warm-ups page

  46. Announcements • Test next Friday • Loops • Methods • Last test of the school year (besides the final) • Will have one more quiz

  47. Overloaded Methods Pre-AP Computer Science, Cycle 6

  48. Methods - Review public static int max(int a, int b) { if (a >= b) return a; else return b; } public static void main(String[] args) { int num1=45; int num2=32; int biggest = max(num1, num2); System.out.println(“Biggest was “ + biggest); }

  49. Parameters and Returns • Parameters • Information passed to the method from the main method • What goes inside the parenthesis • max(a,b)  a,b are parameters • Returns • The answer the method gives back • max(a,b)  the largest would be the return • isPrime(num)  true or false

  50. Overloaded Methods • Occurs when you write multiple methods within the same class with the exact same name, but with different parameters • Useful for situations where you may be sending different numbers of parameters, or parameters of different data types

More Related