150 likes | 153 Views
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
E N D
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?
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
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”); } }
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();
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
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”); }
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
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
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
Returning a Value • Example • int n = inputDevice.nextInt(); • String name = inputDevice.nextLine();
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
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; }
Assignment • Process required ??? • Separate them into methods • Identify whether it is void or returning value