SlideShare une entreprise Scribd logo
1  sur  30
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 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
Introduction
   Have you ever invoke (call) a method?
    Scanner sc = new Scanner(System.in);
    int x = sc.nextInt();

    System.out.println(“Hello”);
Method definition
Method concept
Methods – Without Arguments
public 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
}
Methods – With Arguments
public 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);
        }
}
Methods – with multiple arguments
public 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);
        }
}
Methods with shared data
public 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);
          }
}
Methods – return Value
public 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
        }
}
The type of the expression in the
 return statement must match the
return type in the Method header.
• 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.
It is the nature of the task to be
performed, not the amount of code,
    that determines if a Method
            should be used.
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 B
class ABC                               public class B
{                                       {
         static void func1()                      public static void main(String[] args)
         {                                        {
         System.out.println(“hello”);                        ABC a1 = new ABC();
         }                                                   a1.func1();
}                                                 }
                                        }
class ABC
{
       static void func1()
       {
               System.out.println("Hello");
       }

}
public class xyz
{
        public static void main(String[] args)
        {
                ABC a1 = new ABC();
                a1.func1();
        }

}
Command-Line Arguments
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.
String Command-Line Arguments
public class Echo
 {
           public static void main (String[] args)
           {
                      for (String s: args)
                      {
                                 System.out.println(s);
                      }
            }
}


javac Echo.java
java Echo Hello to the World

Output:
String Command-Line Arguments
public class Echo
{
          public static void main (String[] args)
          {
          for (int i=0; i<args.length; i++)
                    {
                             System.out.println(args[i]);
          }
          }
}
javac Echo.java
java Echo Hello to the World

Output:
Integer Command-Line Arguments
public 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
RecuRsion
Factorial (3) recursively
Fibonacci numbers
Every recursive call must either solve
part 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 factorial
public 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
                       }
           }
}

Contenu connexe

Tendances

The Ring programming language version 1.5.4 book - Part 33 of 185
The Ring programming language version 1.5.4 book - Part 33 of 185The Ring programming language version 1.5.4 book - Part 33 of 185
The Ring programming language version 1.5.4 book - Part 33 of 185Mahmoud Samir Fayed
 
Introduction to functional programming using Ocaml
Introduction to functional programming using OcamlIntroduction to functional programming using Ocaml
Introduction to functional programming using Ocamlpramode_ce
 
The Ring programming language version 1.5.2 book - Part 31 of 181
The Ring programming language version 1.5.2 book - Part 31 of 181The Ring programming language version 1.5.2 book - Part 31 of 181
The Ring programming language version 1.5.2 book - Part 31 of 181Mahmoud Samir Fayed
 
11. Java Objects and classes
11. Java  Objects and classes11. Java  Objects and classes
11. Java Objects and classesIntro C# Book
 
Chapter i(introduction to java)
Chapter i(introduction to java)Chapter i(introduction to java)
Chapter i(introduction to java)Chhom Karath
 
The Ring programming language version 1.3 book - Part 24 of 88
The Ring programming language version 1.3 book - Part 24 of 88The Ring programming language version 1.3 book - Part 24 of 88
The Ring programming language version 1.3 book - Part 24 of 88Mahmoud Samir Fayed
 
Basic of octave matlab programming language
Basic of octave matlab programming languageBasic of octave matlab programming language
Basic of octave matlab programming languageAulia Khalqillah
 
05. Java Loops Methods and Classes
05. Java Loops Methods and Classes05. Java Loops Methods and Classes
05. Java Loops Methods and ClassesIntro C# Book
 
The Ring programming language version 1.5.2 book - Part 32 of 181
The Ring programming language version 1.5.2 book - Part 32 of 181The Ring programming language version 1.5.2 book - Part 32 of 181
The Ring programming language version 1.5.2 book - Part 32 of 181Mahmoud Samir Fayed
 
Fp in scala part 2
Fp in scala part 2Fp in scala part 2
Fp in scala part 2Hang Zhao
 

Tendances (20)

The Ring programming language version 1.5.4 book - Part 33 of 185
The Ring programming language version 1.5.4 book - Part 33 of 185The Ring programming language version 1.5.4 book - Part 33 of 185
The Ring programming language version 1.5.4 book - Part 33 of 185
 
Introduction to functional programming using Ocaml
Introduction to functional programming using OcamlIntroduction to functional programming using Ocaml
Introduction to functional programming using Ocaml
 
Java generics
Java genericsJava generics
Java generics
 
Templates
TemplatesTemplates
Templates
 
Chapter 2 Java Methods
Chapter 2 Java MethodsChapter 2 Java Methods
Chapter 2 Java Methods
 
The Ring programming language version 1.5.2 book - Part 31 of 181
The Ring programming language version 1.5.2 book - Part 31 of 181The Ring programming language version 1.5.2 book - Part 31 of 181
The Ring programming language version 1.5.2 book - Part 31 of 181
 
11. Java Objects and classes
11. Java  Objects and classes11. Java  Objects and classes
11. Java Objects and classes
 
Chapter i(introduction to java)
Chapter i(introduction to java)Chapter i(introduction to java)
Chapter i(introduction to java)
 
The Ring programming language version 1.3 book - Part 24 of 88
The Ring programming language version 1.3 book - Part 24 of 88The Ring programming language version 1.3 book - Part 24 of 88
The Ring programming language version 1.3 book - Part 24 of 88
 
Basic of octave matlab programming language
Basic of octave matlab programming languageBasic of octave matlab programming language
Basic of octave matlab programming language
 
GNU octave
GNU octaveGNU octave
GNU octave
 
OOP Core Concept
OOP Core ConceptOOP Core Concept
OOP Core Concept
 
Core C#
Core C#Core C#
Core C#
 
05. Java Loops Methods and Classes
05. Java Loops Methods and Classes05. Java Loops Methods and Classes
05. Java Loops Methods and Classes
 
Class method
Class methodClass method
Class method
 
Java introduction
Java introductionJava introduction
Java introduction
 
Java Generics
Java GenericsJava Generics
Java Generics
 
The Ring programming language version 1.5.2 book - Part 32 of 181
The Ring programming language version 1.5.2 book - Part 32 of 181The Ring programming language version 1.5.2 book - Part 32 of 181
The Ring programming language version 1.5.2 book - Part 32 of 181
 
Fp in scala part 2
Fp in scala part 2Fp in scala part 2
Fp in scala part 2
 
Oop objects_classes
Oop objects_classesOop objects_classes
Oop objects_classes
 

En vedette

Case studys of RockSound and Kerrang!
Case studys of RockSound and Kerrang!Case studys of RockSound and Kerrang!
Case studys of RockSound and Kerrang!Chloe Main
 

En vedette (8)

Comp102 lec 4
Comp102   lec 4Comp102   lec 4
Comp102 lec 4
 
Comp102 lec 5.1
Comp102   lec 5.1Comp102   lec 5.1
Comp102 lec 5.1
 
Comp102 lec 9
Comp102   lec 9Comp102   lec 9
Comp102 lec 9
 
Comp102 lec 10
Comp102   lec 10Comp102   lec 10
Comp102 lec 10
 
Comp102 lec 3
Comp102   lec 3Comp102   lec 3
Comp102 lec 3
 
Comp102 lec 8
Comp102   lec 8Comp102   lec 8
Comp102 lec 8
 
Comp102 lec 5.0
Comp102   lec 5.0Comp102   lec 5.0
Comp102 lec 5.0
 
Case studys of RockSound and Kerrang!
Case studys of RockSound and Kerrang!Case studys of RockSound and Kerrang!
Case studys of RockSound and Kerrang!
 

Similaire à Comp102 lec 7

Similaire à Comp102 lec 7 (20)

Java Class Design
Java Class DesignJava Class Design
Java Class Design
 
Java interface
Java interfaceJava interface
Java interface
 
Nabil code
Nabil  codeNabil  code
Nabil code
 
Nabil code
Nabil  codeNabil  code
Nabil code
 
Nabil code
Nabil  codeNabil  code
Nabil code
 
Chap2 class,objects contd
Chap2 class,objects contdChap2 class,objects contd
Chap2 class,objects contd
 
Lec 5 13_aug [compatibility mode]
Lec 5 13_aug [compatibility mode]Lec 5 13_aug [compatibility mode]
Lec 5 13_aug [compatibility mode]
 
Java ppt
Java pptJava ppt
Java ppt
 
Kotlin Bytecode Generation and Runtime Performance
Kotlin Bytecode Generation and Runtime PerformanceKotlin Bytecode Generation and Runtime Performance
Kotlin Bytecode Generation and Runtime Performance
 
Sam wd programs
Sam wd programsSam wd programs
Sam wd programs
 
Les nouveautés de C# 6
Les nouveautés de C# 6Les nouveautés de C# 6
Les nouveautés de C# 6
 
Basic java, java collection Framework and Date Time API
Basic java, java collection Framework and Date Time APIBasic java, java collection Framework and Date Time API
Basic java, java collection Framework and Date Time API
 
Core java pract_sem iii
Core java pract_sem iiiCore java pract_sem iii
Core java pract_sem iii
 
Class 5 2ciclo
Class 5 2cicloClass 5 2ciclo
Class 5 2ciclo
 
Java programs
Java programsJava programs
Java programs
 
JDK 8
JDK 8JDK 8
JDK 8
 
An introduction to scala
An introduction to scalaAn introduction to scala
An introduction to scala
 
LECTURE 2 MORE TYPES, METHODS, CONDITIONALS.pdf
LECTURE 2 MORE TYPES, METHODS, CONDITIONALS.pdfLECTURE 2 MORE TYPES, METHODS, CONDITIONALS.pdf
LECTURE 2 MORE TYPES, METHODS, CONDITIONALS.pdf
 
C# for Java Developers
C# for Java DevelopersC# for Java Developers
C# for Java Developers
 
Java Generics
Java GenericsJava Generics
Java Generics
 

Comp102 lec 7

  • 1. Methods(Methods) Methods, Command Line Arguments, Aliasing and Recursion
  • 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. Introduction  Have you ever invoke (call) a method? Scanner sc = new Scanner(System.in); int x = sc.nextInt(); System.out.println(“Hello”);
  • 6. Methods – Without Arguments public 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. Methods – With Arguments public 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. Methods – with multiple arguments public 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. Methods with shared data public 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. Methods – return Value public 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. The type of the expression in the return statement must match the return type in the Method header.
  • 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. It is the nature of the task to be performed, not the amount of code, that determines if a Method should be used.
  • 14. Variables are in scope from their point of definition until the end of their Method or block.
  • 15. ACCESSING Method Defined in some other class CLASS A CLASS B class ABC public class B { { static void func1() public static void main(String[] args) { { System.out.println(“hello”); ABC a1 = new ABC(); } a1.func1(); } } }
  • 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(); } }
  • 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. String Command-Line Arguments public class Echo { public static void main (String[] args) { for (String s: args) { System.out.println(s); } } } javac Echo.java java Echo Hello to the World Output:
  • 20. String Command-Line Arguments public class Echo { public static void main (String[] args) { for (int i=0; i<args.length; i++) { System.out.println(args[i]); } } } javac Echo.java java Echo Hello to the World Output:
  • 21. Integer Command-Line Arguments public 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
  • 25. Every recursive call must either solve part of the problem or reduce the size of the problem.
  • 27. Towers solution for two disks
  • 28. Towers solution for three disks
  • 29. Towers solution for three disks (continued)
  • 30. Recursive program of factorial public 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 } } }