METHODS Lecture Week 6 Introduction Review object has

METHODS Lecture Week 6

Introduction • Review – object has attributes with values • Example book has color = blue; • An object also has capability – actions, functions that can be performed • In object oriented programming called methods

Concept of method • Cleaning the plates – single person – wash with soap – Rinse with water • Cleaning the plates – more than one person ? ? • Team of Surgery ? • Hierarchy in organization?

• Method is separating the solutions or decomposing it into smaller parts/sub solution public class Divided. Problem { public class Big. Problem { public static void main(String[] args) { statement 1; statement 2; statement 3; statement 4; statement 5; statement 6; statement 7; statement 8; statement 9; } } public static void main(String[] args) { statement 1; statement 2; statement 3; } public static void method 2() { statement 4; statement 5; statement 6; } public static void method 3() { statement 7; statement 8; statement 9; } }
![Example public class demo. Method { public static void main (String [] args) { Example public class demo. Method { public static void main (String [] args) {](http://slidetodoc.com/presentation_image_h2/05968a19e459321f1e64f843f052d3d3/image-5.jpg)
Example public class demo. Method { public static void main (String [] args) { int num = 78; if (num %2 ==0) System. out. println(“number “ + num + “ is even”); else System. out. println(“number “ + num + “ is odd”); } } int num = 78; check. Even(num); } public static void check. Even(int n) { if (n %2 ==0) System. out. println(“number “ + n + “ is even”); else System. out. println(“number “ + n + “ is odd”); } }

Void and Returning Value Methods • Void methods – method that do not return a value • Eg System. out. println(“method”); • Returning value methods – methods that return a value • Eg int num = input. Device. next. Int(); String name = input. Device. next. Line();

Void method • Definition of the methods consists of – Header No value return – Body Method name Variables passed • Example public static void check. Even (int n) { if (n %2 == 0) System. out. println(“Number is even); else System. out. println(“Number is odd); }
![Example public class Salam { public void main (String [] args) { for (int Example public class Salam { public void main (String [] args) { for (int](http://slidetodoc.com/presentation_image_h2/05968a19e459321f1e64f843f052d3d3/image-8.jpg)
Example public class Salam { public void main (String [] args) { for (int n=1; n < 7; n++) System. out. println(“Assalamu’alaikum”); } Calling a method } public class Salam { public void main (String [] args) { for (int n=1; n < 7; n++) print. Salam() public class Salam { public void main (String [] args) { print. Salam 7(); } public static void print. Salam 7 () { for (int n=1; n < 7; n++) System. out. println(“Assalamu’alaikum”); } } public static void print. Salam () { System. out. println(“Assalamu’alaikum”); }

Argument • Variables can be passed to method • Make sure the type is the same • Example public class Salam { public void main (String [] args) { print. Salam(7); Variables passed } public static void print. Salam (int max) { Value of max = 7 for (int n=1; n < max ; n++) System. out. println(“Assalamu’alaikum”); } }

Argument/Parameter • Variables can be passed to method • Make sure the type is the same • Example public class Salam { public void main (String [] args) { int repeat =7; print. Salam(repeat); } public static void print. Salam (int max) { for (int n=1; n < max ; n++) System. out. println(“Assalamu’alaikum”); } } repeat 7 max 7
![Multiple parameters public class compare { public void main (String [] args) { int Multiple parameters public class compare { public void main (String [] args) { int](http://slidetodoc.com/presentation_image_h2/05968a19e459321f1e64f843f052d3d3/image-11.jpg)
Multiple parameters public class compare { public void main (String [] args) { int num 1 =9; num 2=15 check. Greater(num 1, num 2); Both parameters must be declared } public static void check. Greater (int n 1, int n 2) { if (n 1 > n 2) System. out. println(n 1 + “ is greater than ” + n 2); else System. out. println(n 2 + “ is greater than ” + n 1); } }

Returning a Value • Example • int n = input. Device. next. Int(); • String name = input. Device. next. Line();

Returning value method • Definition of the methods consists of – Header – type return, method name, parameters – Body public static int read. Input () { Scanner input. Device = new Scanner(System. in); System. out. println(“Enter a number”); int num = input. Device. next. Int(); return num; } Same type – only one variable
![public class read. Number() { public static void main (String [] args) { int public class read. Number() { public static void main (String [] args) { int](http://slidetodoc.com/presentation_image_h2/05968a19e459321f1e64f843f052d3d3/image-14.jpg)
public class read. Number() { public static void main (String [] args) { int number, sum=0; for (int n=1; n < =100; n++){ number = read. Input(); sum = sum + number; } double average = (double) sum/100; System. out. println(“The average of 100 numbers are ” + average); } } public static int read. Input () { Scanner input. Device = new Scanner(System. in); System. out. println(“Enter a number”); int num = input. Device. next. Int(); return num; }

Assignment • Process required ? ? ? • Separate them into methods • Identify whether it is void or returning value
- Slides: 15