Methods in Java

39
Methods in Java Jussi Pohjolainen Tampere University of Applied Sciences

description

 

Transcript of Methods in Java

Page 1: Methods in Java

Methods  in  Java  

Jussi  Pohjolainen  Tampere  University  of  Applied  Sciences  

Page 2: Methods in Java

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?  

Page 3: Methods in Java

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?  

Page 4: Methods in Java

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!  

Page 5: Methods in Java

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 } }

Page 6: Methods in Java

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!

Page 7: Methods in Java

Method  Call  

Method   Method  

Page 8: Methods in Java

Examples  

•  Flash  demo:  Method  1  (no  parameters,  no  return)  

•  MethodDemo1.java  •  MethodDemo2.java  •  MethodDemo3.java  •     

Page 9: Methods in Java

IT'S  NOT  THAT  EASY:  PARAMETERS  AND  RETURN  

Page 10: Methods in Java

Real  Life  Example  

I  really  want  to  buy  christmas  gi\s  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?  

Page 11: Methods in Java

Real  Life  Example  

Hello  Hello!  My  name  is  Mr.  Sum  and  I  can  sum  everything!  Please  use  my  knowledge!  

Page 12: Methods in Java

Real  Life  Example  

Well  Mr  Sum.  I  have  a  problem  I  don't  know  what  is  5  plus  6.    

Page 13: Methods in Java

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!  

Page 14: Methods in Java

Parameters!  

5  6  

Page 15: Methods in Java

Return!  

11  

Page 16: Methods in Java

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!  

Page 17: Methods in Java

Mr.  Sum?  

Mr.  Sum  

5  

6  

11  

parameter   return  

Page 18: Methods in Java

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[  

Page 19: Methods in Java

Real  Life  Example  in  the  Nerd  World  

Give  random  number.  

0.34523456  

Page 20: Methods in Java

Mr.  Random?  

Mr.  Random     0.34523456  

return  

Page 21: Methods in Java

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...  

Page 22: Methods in Java

LET'S  START  CODING  

Page 23: Methods in Java

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  

Page 24: Methods in Java

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  

Page 25: Methods in Java

Your  own  Methods  public class Example {

public static void main(String [] args) {

printSomething();

}

public static void printSomething() {

System.out.println("Hello World!");

}

}

Page 26: Methods in Java

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!"); } }

Page 27: Methods in Java

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); } }

Page 28: Methods in Java

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; } }

Page 29: Methods in Java

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; } }

Page 30: Methods in Java

Examples  

•  Flash-­‐demo:  Method  2  •  Flash-­‐demo:  Method  3  •  Flash-­‐demo:  Method  4  •  MethodDemo4.java  

Page 31: Methods in Java

VISIBILITY  OF  VARIABLES  

Page 32: Methods in Java

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); } }

Page 33: Methods in Java

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); } }

Page 34: Methods in Java

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); } }

Page 35: Methods in Java

EXAMPLE  

Page 36: Methods in Java

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

Page 37: Methods in Java

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(); } } }

Page 38: Methods in Java

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);

}

}

...

}

Page 39: Methods in Java

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();

}

}