1 / 15

METHODS

Learn about methods in OOP, how they separate solutions, and the concepts of void and returning value methods. Understand how to define, call, and pass variables to methods with examples and explanations.

louiep
Download Presentation

METHODS

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. METHODS Lecture Week 6

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

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

  4. public class DividedProblem { public static void main(String[] args) { statement1; statement2; statement3; } public static void method2() { statement4; statement5; statement6; } public static void method3() { statement7; statement8; statement9; } } public class BigProblem { public static void main(String[] args) { statement1; statement2; statement3; statement4; statement5; statement6; statement7; statement8; statement9; } } • Method is separating the solutions or decomposing it into smaller parts/sub solution

  5. Example public class demoMethod { public static void main (String [] args) { int num = 78; checkEven(num); } public static void checkEven(int n) { if (n %2 ==0) System.out.println(“number “ + n + “ is even”); else System.out.println(“number “ + n + “ is odd”); } } public class demoMethod { 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”); } }

  6. 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 = inputDevice.nextInt(); String name = inputDevice.nextLine();

  7. Void method • Definition of the methods consists of • Header • Body • Example public static void checkEven (int n) { if (n %2 == 0) System.out.println(“Number is even); else System.out.println(“Number is odd); } Method name No value return Variables passed Body

  8. 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++) printSalam() } public static void printSalam () { System.out.println(“Assalamu’alaikum”); } public class Salam { public void main (String [] args) { printSalam7(); } public static void printSalam7 () { for (int n=1; n < 7; n++) System.out.println(“Assalamu’alaikum”); }

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

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

  11. Multiple parameters public class compare { public void main (String [] args) { int num1 =9; num2=15 checkGreater(num1, num2); } public static void checkGreater (int n1, int n2) { if (n1 > n2) System.out.println(n1 + “ is greater than ” + n2); else System.out.println(n2 + “ is greater than ” + n1); } } Both parameters must be declared

  12. Returning a Value • Example • int n = inputDevice.nextInt(); • String name = inputDevice.nextLine();

  13. Returning value method • Definition of the methods consists of • Header – type return, method name, parameters • Body public static int readInput () { Scanner inputDevice = new Scanner(System.in); System.out.println(“Enter a number”); int num = inputDevice.nextInt(); return num; } Same type – only one variable

  14. public class readNumber() { public static void main (String [] args) { int number, sum=0; for (int n=1; n < =100; n++){ number = readInput(); sum = sum + number; } double average = (double) sum/100; System.out.println(“The average of 100 numbers are ” + average); } } public static int readInput () { Scanner inputDevice = new Scanner(System.in); System.out.println(“Enter a number”); int num = inputDevice.nextInt(); return num; }

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

More Related