Methods in Java

3,293 views

Published on

Methods in Java

  1. 1. Methods  in  Java   Jussi  Pohjolainen   Tampere  University  of  Applied  Sciences  
  2. 2. Problem   public class HugeProgram { public static void main(String [] args) { 1. codeline 2. codeline 3. codeline ... 10 000. codeline } } How  are  you   going  to   manage  this   huge   codeblock?  
  3. 3. Problem   •  It's  possible  that  your  whole  program  is   implemented  inside  the  main  –  method   •  Result:  No  one  will  understand  the  code!   •  You  have  to  create  the  code  that  is   – Reusable   – You  and  others  can  manage  it   •  Well  how?  
  4. 4. How  to  divide  your  code?   •  Best  pracJce:  Use  Object-­‐Orientated   Programming   – Classes,  Methods,  Inheritance,  ...   •  Lot  of  possibiliJes  with  OO!   •  But  first  let's  concentrate  on  methods!  
  5. 5. Methods:  Divide  your  Code?   public class HugeProgram { public static void main(String [] args) { doSomething1(); doSomething2(); doSomething3(); } public static void doSomething1() { // something } public static void doSomething2() { // something } public static void doSomething3() { // something } }
  6. 6. Methods:  Divide  your  Code?   public class HugeProgram { public static void main(String [] args) { doSomething1(); doSomething2(); } public static void doSomething1() { System.out.print("Hello "); } public static void doSomething2() { System.out.println("World!"); } } java HugeProgram Hello World!
  7. 7. Method  Call   Method   Method  
  8. 8. Examples   •  Flash  demo:  Method  1  (no  parameters,  no   return)   •  MethodDemo1.java   •  MethodDemo2.java   •  MethodDemo3.java   •     
  9. 9. IT'S  NOT  THAT  EASY:  PARAMETERS   AND  RETURN  
  10. 10. Real  Life  Example   I  really  want  to  buy  christmas  gis   for  my  children.  The  doll  costs  5   euros  and  the  toy  car  costs  6  euros.   I  have  12  euros,  is  that  enough?   Why  didn't  I  listen  to  the  math   teacher  at  school?  Why  didn't  I   learn  sum  –  calculaJons?  Why  oh   why?  
  11. 11. Real  Life  Example   Hello  Hello!  My  name  is  Mr.   Sum  and  I  can  sum  everything!   Please  use  my  knowledge!  
  12. 12. Real  Life  Example   Well  Mr  Sum.  I  have  a  problem  I   don't  know  what  is  5  plus  6.    
  13. 13. Real  Life  Example   Don't  understand  your   problem.  I  only  do  simple   calculaJons,  I  really  need  to   have  two  integer  values  to  be   able  to  do  the  sum.  Please  just   give  my  two  numbers!  
  14. 14. Parameters!   5   6  
  15. 15. Return!   11  
  16. 16. Real  Life  Example   Well  thanks,  at  least  I  know  my   numbers  and  11  is  less  than  12,   so  great!  My  children  will  be   very  happy!  
  17. 17. Mr.  Sum?   Mr.  Sum   5   6   11   parameter   return  
  18. 18. Real  Life  Example  in  the  Nerd  World   I  am  implemenJng  a  applicaJon   and  I  really  need  a  random   floaJng  point  value  between   [0,1[   Well  Howdy,  my  name  is  Mr.   Random  and  I  live  in  a  town   called  Math.  I  can  give  you  a   random  floaJng  point  value   between  [0,1[  
  19. 19. Real  Life  Example  in  the  Nerd  World   Give  random  number.   0.34523456  
  20. 20. Mr.  Random?   Mr.  Random     0.34523456   return  
  21. 21. Real  Life  Example  in  the  Nerd  World   Did  you  know  that  in  the  town  Math   there  other  guys  living  too?  For   example:  Mr.  Round,  Mr  Max,  Mr  Min,   Mr  Sin...  
  22. 22. LET'S  START  CODING  
  23. 23. The  usage  of  Class  Library   import java.lang.Math; public class Example { public static void main(String [] args) { double myRandomNumber = Math.random(); System.out.println(myRandomNumber); } } Import  the  class   class   method  
  24. 24. Mr.  Max  in  Code   import java.lang.Math; public class Example { public static void main(String [] args) { int maxNumber = Math.max(5,10); System.out.println(maxNumber ); } } Import  the  class   class   method   parameters  
  25. 25. Your  own  Methods   public class Example { public static void main(String [] args) { printSomething(); } public static void printSomething() { System.out.println("Hello World!"); } }
  26. 26. Your  own  Methods   public class Example { public static void main(String [] args) { PrintClass.printSomething(); } } class PrintClass { public static void printSomething() { System.out.println("Hello World!"); } }
  27. 27. Parameters   public class Example { public static void main(String [] args) { MyMath.max(5, 10); } } class MyMath { public static void max(int a, int b) { if(a > b) System.out.println(a); else System.out.println(b); } }
  28. 28. Return   public class Example { public static void main(String [] args) { int maxNumber = MyMath.max(5, 10); System.out.println(maxNumber); } } class MyMath { public static int max(int a, int b) { if(a > b) return a; else return b; } }
  29. 29. In  One  class   public class Example { public static void main(String [] args) { int maxNumber = max(5, 10); System.out.println(maxNumber); } public static int max(int a, int b) { if(a > b) return a; else return b; } }
  30. 30. Examples   •  Flash-­‐demo:  Method  2   •  Flash-­‐demo:  Method  3   •  Flash-­‐demo:  Method  4   •  MethodDemo4.java  
  31. 31. VISIBILITY  OF  VARIABLES  
  32. 32. This  does  not  work,  why?   import java.util.Scanner; class Example { public static void main(String [] args) { Scanner input = new Scanner(System.in); int x = input.nextInt(); if(x == 5) { int b = 5; } System.out.println(b); } }
  33. 33. This  does  not  work,  why?   import java.util.Scanner; class MethodDemo3 { public static void main(String [] args) { askName(); printName(); } public static void askName() { Scanner input = new Scanner(System.in); String name = input.nextLine(); } public static void printName() { System.out.println(name); } }
  34. 34. SoluJon   import java.util.Scanner; class MethodDemo3 { public static void main(String [] args) { String n = askName(); printName(n); } public static String askName() { Scanner input = new Scanner(System.in); String name = input.nextLine(); return name; } public static void printName(String name) { System.out.println(name); } }
  35. 35. EXAMPLE  
  36. 36. Exercise   •  Implement  a  Java-­‐app  that  prints  square.  The   height  of  the  square  is  asked  from  the  user.   java Rectangle Height? 4 XXXX X X X X XXXX
  37. 37. import java.util.Scanner; class Rectangle { public static void main(String [] args) { Scanner input = new Scanner(System.in)a; int height = input.nextInt(); // Go all the lines for(int line=0; line<height; line++) { // If the line is the first or last if(line == 0 || line == height-1) { // Print 'X' height amount of times } else { // Print 'X' // Print ' ' height-2 amount of times // Print 'X' } // Print enter System.out.println(); } } }
  38. 38. Let's  use  Methods!   class Rectangle { public static void print(char mark, int amount) { for(int i=0; i<amount; i++) { System.out.print(mark); } } ... }
  39. 39. public static void main(String [] args) { Scanner input = new Scanner(System.in); int height = input.nextInt(); // Go all the lines for(int line=0; line<height; line++) { // If the line is the first or last if(line == 0 || line == height-1) { print('X', height); // METHOD CALL! } else { print('X', 1); // METHOD CALL! print(' ', height-2); // METHOD CALL! print('X', 1); // METHOD CALL! } // Print enter System.out.println(); } }

×