Methods(Methods)Methods, Command Line Arguments, Aliasing and Recursion
Introduction   Method is a part of class   Contains particular set of instructions       One method in one class of pro...
Introduction   Have you ever invoke (call) a method?    Scanner sc = new Scanner(System.in);    int x = sc.nextInt();    ...
Method definition
Method concept
Methods – Without Argumentspublic class xyz{       public static void main(String[] args)       {               foo(); // ...
Methods – With Argumentspublic class xyz{        public static void main(String[] args)        {                int x;    ...
Methods – with multiple argumentspublic class xyz{        public static void main(String[] args)        {                i...
Methods with shared datapublic class xyz{          static int z= 20; // shared by all Methods of class xyz          public...
Methods – return Valuepublic class xyz{        public static void main(String[] args)        {                 int x = 15,...
The type of the expression in the return statement must match thereturn type in the Method header.
• Formal parameters are variables that are  declared in the header of the Method  definition.• Actual parameters are the e...
It is the nature of the task to beperformed, not the amount of code,    that determines if a Method            should be u...
Variables are in scope from their point  of definition until the end of their           Method or block.
ACCESSING Method Defined in some other    class           CLASS A                                       CLASS Bclass ABC  ...
class ABC{       static void func1()       {               System.out.println("Hello");       }}public class xyz{        p...
Command-Line Arguments
Command-Line Arguments   A Java application can accept any number of arguments    from the command line.   This allows t...
String Command-Line Argumentspublic class Echo {           public static void main (String[] args)           {            ...
String Command-Line Argumentspublic class Echo{          public static void main (String[] args)          {          for (...
Integer Command-Line Argumentspublic class Echo{          public static void main (String[] args)          {          int ...
RecuRsion
Factorial (3) recursively
Fibonacci numbers
Every recursive call must either solvepart of the problem or reduce the size            of the problem.
Towers of Hanoi—start position
Towers solution for two disks
Towers solution for three disks
Towers solution for three disks (continued)
Recursive program of factorialpublic class recursive{           public static void main (String[] args)           {       ...
Upcoming SlideShare
Loading in...5
×

Comp102 lec 7

377

Published on

0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
377
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
8
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Comp102 lec 7

  1. 1. Methods(Methods)Methods, Command Line Arguments, Aliasing and Recursion
  2. 2. Introduction Method is a part of class Contains particular set of instructions  One method in one class of program  Main Method Classes can contain many methods A method should perform a single well defined task Two parts to be done in a program  Declare and Define a method  Call a Method  Program jumps to execute instructions written in program and after executing them in sequential manner, comes back to the calling instruction
  3. 3. Introduction Have you ever invoke (call) a method? Scanner sc = new Scanner(System.in); int x = sc.nextInt(); System.out.println(“Hello”);
  4. 4. Method definition
  5. 5. Method concept
  6. 6. Methods – Without Argumentspublic class xyz{ public static void main(String[] args) { foo(); // Method call } static void foo() // Method definition { // start of Method body System.out.println("Hello World"); // Method body }// end of Method body}
  7. 7. Methods – With Argumentspublic class xyz{ public static void main(String[] args) { int x; x=15; foo(x); // passing x as parameter } static void foo(int a) // creating a alias of x { System.out.println("value is: "+a); }}
  8. 8. Methods – with multiple argumentspublic class xyz{ public static void main(String[] args) { int x = 15, y= 20; double z= 20.5; foo(x, y, z); } static void foo(int a, int y, double c) { System.out.println("x or a: "+a); System.out.println("y: "+y); System.out.println("z or c:"+c); }}
  9. 9. Methods with shared datapublic class xyz{ static int z= 20; // shared by all Methods of class xyz public static void main(String[] args) { int x; x=15; foo(x); System.out.println("Z=: "+z); } static void foo(int a) { System.out.println("value is: "+a); System.out.println("value is: "+z); }}
  10. 10. Methods – return Valuepublic class xyz{ public static void main(String[] args) { int x = 15, y= 20; int result = foo(x, y); //outcome is received in variable System.out.println("result is: "+result); } static int foo(int a, int b) // data type of return is defined { int sum; sum= a+b; return sum; // return a variable or value }}
  11. 11. The type of the expression in the return statement must match thereturn type in the Method header.
  12. 12. • Formal parameters are variables that are declared in the header of the Method definition.• Actual parameters are the expressions in the calling statement.• The formal and actual parameters must match exactly in type, order, and number. Their names, however, do not need to be the same.
  13. 13. It is the nature of the task to beperformed, not the amount of code, that determines if a Method should be used.
  14. 14. Variables are in scope from their point of definition until the end of their Method or block.
  15. 15. ACCESSING Method Defined in some other class CLASS A CLASS Bclass ABC public class B{ { static void func1() public static void main(String[] args) { { System.out.println(“hello”); ABC a1 = new ABC(); } a1.func1();} } }
  16. 16. class ABC{ static void func1() { System.out.println("Hello"); }}public class xyz{ public static void main(String[] args) { ABC a1 = new ABC(); a1.func1(); }}
  17. 17. Command-Line Arguments
  18. 18. Command-Line Arguments A Java application can accept any number of arguments from the command line. This allows the user to specify configuration information when the application is launched. The user enters command-line arguments when invoking the application and specifies them after the name of the class to be run While running the program any things written after the name of the class are the command line arguments. Arguments are delimited by the space.
  19. 19. String Command-Line Argumentspublic class Echo { public static void main (String[] args) { for (String s: args) { System.out.println(s); } }}javac Echo.javajava Echo Hello to the WorldOutput:
  20. 20. String Command-Line Argumentspublic class Echo{ public static void main (String[] args) { for (int i=0; i<args.length; i++) { System.out.println(args[i]); } }}javac Echo.javajava Echo Hello to the WorldOutput:
  21. 21. Integer Command-Line Argumentspublic class Echo{ public static void main (String[] args) { int argument; for (int i=0; i<args.length; i++) { argument = Integer.parseInt(args[i]); System.out.println(argument); } }}javac Echo.java
  22. 22. RecuRsion
  23. 23. Factorial (3) recursively
  24. 24. Fibonacci numbers
  25. 25. Every recursive call must either solvepart of the problem or reduce the size of the problem.
  26. 26. Towers of Hanoi—start position
  27. 27. Towers solution for two disks
  28. 28. Towers solution for three disks
  29. 29. Towers solution for three disks (continued)
  30. 30. Recursive program of factorialpublic class recursive{ public static void main (String[] args) { int n=5; int factorial=fact(n); System.out.println("factorial is: "+factorial); } static int fact(int number) { if(number == 1) { return 1; } else { return (number * fact(number-1));//self calling Methods } }}
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×