Presentation is loading. Please wait.

Presentation is loading. Please wait.

Www.techsparx.webs.com www.techsparx.net Saravanan.G.

Similar presentations


Presentation on theme: "Www.techsparx.webs.com www.techsparx.net Saravanan.G."— Presentation transcript:

1 www.techsparx.webs.com www.techsparx.net Saravanan.G

2

3 A function is a set of statements performing a task. In java function is called as method.

4 [ ] [ ] ([ ]) { Statement1; Statement2;.. Statementn; } Where, --- is either public, private, protected or default. --- is a keyword which modifies the function. It can be static, volatile, synchronized etc. --- data type of the variable which is returned from the function --- name of the function --- list of data type and variable pair separated by a comma.

5 Function which does not accept and does not return Function which accepts but does not return Function which accepts and returns Function which does not accept but returns

6

7 public void add() { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); System.out.println(“Enter first number”); int a = Integer.parseInt(br.readLine()); System.out.println(“Enter second number”); int b = Integer.parseInt(br.readLine()); int sum = a + b; System.out.println(“The sum of “ + a + “ and “ + b + “ is : “ + sum); }

8 public static void main(String[] args) { //Create an object of the class Calculator calc = new Calculator(); //Call the add function calc.add(); }

9 import java.io.*; public class Calculator { public void add() throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); System.out.println(“Enter first number”); int a = Integer.parseInt(br.readLine()); System.out.println(“Enter second number”); int b = Integer.parseInt(br.readLine()); int sum = a + b; System.out.println(“The sum of “ + a + “ and “ + b + “ is : “ + sum); } public static void main(String[] args) throws IOException { Calculator calc = new Calculator(); //Call the add function calc.add(); } }

10 WAP to simulate a simple calculator using functions(1 st type) Let the name of the functions be findSum() findDifference() findProduct() findQuotient() findRemainder()

11

12 public void add (int a, int b) { int sum = a + b; System.out.println(“The sum of “ + a + “ and “ + b + “ is : “ + sum); }

13 public static void main(String[] args) { //Create an object of the class Calculator calc = new Calculator(); //Call the add function calc.add(10, 20); }

14 public class Calculator { public void add(int a, int b) { int sum = a + b; System.out.println(“The sum of “ + a + “ and ” + b + “ is : ” + sum); } public static void main(String[] args) { Calculator calc = new Calculator(); //Call the add function calc.add(10, 40); } }

15 import java.io.*; public class Calculator { public void add(int a, int b) { int sum = a + b; System.out.println(“The sum of “ + a + “ and “ + b + “ is : “ + sum); } public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); System.out.println(“Enter first number”); int x = Integer.parseInt(br.readLine()); System.out.println(“Enter second number”); int y = Integer.parseInt(br.readLine()); Calculator calc = new Calculator(); //Call the add function calc.add(x, y); } }

16 import java.io.*; public class Calculator { public void add(int a, int b) { int sum = a + b; System.out.println(“The sum of “ + a + “ and “ + b + “ is : “ + sum); } public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); System.out.println(“Enter first number”); int x = Integer.parseInt(br.readLine()); System.out.println(“Enter second number”); int y = Integer.parseInt(br.readLine()); Calculator calc = new Calculator(); //Call the add function calc.add( x + y, x - y ); } }

17 WAP to simulate a simple calculator using functions(2 nd type) Let the name of the functions be findSum() findDifference() findProduct() findQuotient() findRemainder()

18

19 public int add (int a, int b) { int sum = a + b; //Return the variable sum return sum; }

20 public int add (int a, int b) { //Return an expression return a + b; }

21 public class Calculator { public int add(int a, int b) { int sum = a + b; return sum; } public static void main(String[] args) { Calculator calc = new Calculator(); //Call the add function and store the returned value int result = calc.add(10, 40); System.out.println(“The sum is “ + result); } }

22 import java.io.*; public class Calculator { public int add(int a, int b) { int sum = a + b; return sum; } public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); System.out.println(“Enter first number”); int x = Integer.parseInt(br.readLine()); System.out.println(“Enter second number”); int y = Integer.parseInt(br.readLine()); Calculator calc = new Calculator(); //Call the add function int result = calc.add(x, y); System.out.println(“The sum of ” + x + “ and ” + y + “ is : ” + result); } }

23 import java.io.*; public class Calculator { public int add(int a, int b) { int sum = a + b; return sum; } public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); System.out.println(“Enter first number”); int x = Integer.parseInt(br.readLine()); System.out.println(“Enter second number”); int y = Integer.parseInt(br.readLine()); Calculator calc = new Calculator(); //Call the add function int result = calc.add(x + y, x - y); System.out.println(“The sum of “ + x + “ and “ + y + “ is : “ + result); } }

24 import java.io.*; public class Calculator { public double add(double a, double b) { double sum = a + b; return sum; } public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); System.out.println(“Enter first number”); double x = Double.parseDouble(br.readLine()); System.out.println(“Enter second number”); double y = Double.parseDouble(br.readLine()); Calculator calc = new Calculator(); //Call the add function double result = calc.add(x, y); System.out.println(“The sum of “ + x + “ and “ + y + “ is : “ + result); } }

25 WAP to simulate a simple calculator using functions(3 rd type) Let the name of the functions be findSum() findDifference() findProduct() findQuotient() findRemainder()

26

27 public int add() throws IOException { System.out.println(“Enter first number”); int a = Integer.parseInt(br.readLine()); System.out.println(“Enter second number”); int b = Integer.parseInt(br.readLine()); int sum = a + b; //Return the variable sum return sum; }

28 import java.io.*; public class Calculator { public int add() throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); System.out.println(“Enter first number”); int a = Integer.parseInt(br.readLine()); System.out.println(“Enter second number”); int a = Integer.parseInt(br.readLine()); int sum = a + b; //Return the variable sum return sum; } public static void main(String[] args) throws IOException { Calculator calc = new Calculator(); //Call the add function int result = calc.add(); System.out.println(“The sum of is : “ + result); }

29 import java.io.*; public class Calculator { public double add() throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); System.out.println(“Enter first number”); double a = Double.parseDouble(br.readLine()); System.out.println(“Enter second number”); double b = Double.parseDouble(br.readLine()); double sum = a + b; //Return the variable sum return sum; } public static void main(String[] args) throws IOException { Calculator calc = new Calculator(); //Call the add function double result = calc.add(); System.out.println(“The sum of is : “ + result); }

30 Two functions are said to be overloaded functions if the function names are same and parameter list is unique that is The number of parameters in two functions must be different. But if the number of parameters are same, then the data types of corresponding parameters in two functions must be different.

31 A constructor is like a function having same name as class name and does not have a return type A constructor is always called during instantiation There are two types of constructors Non-parameterized constructor – Constructor which does not have any parameters Parameterized Constructor – Constructor which has a list of parameter Default Constructor: Whenever the programmer does not define a constructor explicitly a default constructor is provided by the compiler

32 Are also called as instance variables There is one copy of non- static variable in every instance of that class Example: String name Non-static variables can be accessed only by an object Example: Student obj = new Student(); obj.name = “comp”; Are also called as class variables There is only one copy of a static variable available for the entire class Example: static int numOfStudents; Static variables can be accessed directly using class name. No need of creating an object. Example: Math.PI

33 Non-static functions should be called only using objects. Example: char charAt(int) String toUpperCase() A member function is by default a non-static function void add() {}{} Static functions can directly be called using the class name, without creating an object. Example: double Math.pow(int, int) A function can be made as a static function by using a static modifier in the function prototype static void main() {}{}

34


Download ppt "Www.techsparx.webs.com www.techsparx.net Saravanan.G."

Similar presentations


Ads by Google