comp102 lec 7

30
Methods(Methods) Methods, Command Line Arguments, Aliasing and Recursion

Upload: fraz-bakhsh

Post on 10-Nov-2014

705 views

Category:

Documents


1 download

DESCRIPTION

 

TRANSCRIPT

Page 1: Comp102   lec 7

Methods(Methods)

Methods, Command Line Arguments, Aliasing and Recursion

Page 2: Comp102   lec 7

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

Page 3: Comp102   lec 7

Introduction Have you ever invoke (call) a method?

Scanner sc = new Scanner(System.in);int x = sc.nextInt();

System.out.println(“Hello”);

Page 4: Comp102   lec 7

Method definition

Page 5: Comp102   lec 7

Method concept

Page 6: Comp102   lec 7

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}

Page 7: Comp102   lec 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); }

}

Page 8: Comp102   lec 7

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);

}}

Page 9: Comp102   lec 7

Methods with shared datapublic class xyz{

static int z= 20; // shared by all Methods of class xyzpublic 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);

}}

Page 10: Comp102   lec 7

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

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

}}

Page 11: Comp102   lec 7

The type of the expression in the return statement must match the

return type in the Method header.

Page 12: Comp102   lec 7

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

Page 13: Comp102   lec 7

It is the nature of the task to be performed, not the amount of code,

that determines if a Methodshould be used.

Page 14: Comp102   lec 7

Variables are in scope from their point of definition until the end of their

Method or block.

Page 15: Comp102   lec 7

ACCESSING Method Defined in some other class

CLASS A CLASS Bpublic class B{

public static void main(String[] args)

{ABC a1 = new ABC();a1.func1();

}}

class ABC{

static void func1(){

System.out.println(“hello”);}

}

Page 16: Comp102   lec 7

class ABC{

static void func1(){

System.out.println("Hello");}

}public class xyz{

public static void main(String[] args){

ABC a1 = new ABC();a1.func1();

}

}

Page 17: Comp102   lec 7

Command-Line Arguments

Page 18: Comp102   lec 7

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.

Page 19: Comp102   lec 7

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 World

Output:

Page 20: Comp102   lec 7

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 World

Output:

Page 21: Comp102   lec 7

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

Page 22: Comp102   lec 7

Recursion

Page 23: Comp102   lec 7

Factorial (3) recursively

Page 24: Comp102   lec 7

Fibonacci numbers

Page 25: Comp102   lec 7

Every recursive call must either solve part of the problem or reduce the size

of the problem.

Page 26: Comp102   lec 7

Towers of Hanoi—start position

Page 27: Comp102   lec 7

Towers solution for two disks

Page 28: Comp102   lec 7

Towers solution for three disks

Page 29: Comp102   lec 7

Towers solution for three disks (continued)

Page 30: Comp102   lec 7

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

}}

}