Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
www.techsparx.webs.com www.techsparx.net Saravanan.G • A function is a set of statements performing a task. In java function is called as method. [<access_specifier>] [<modifier>] <return_type> <function_name>([<parameter_list>]) { Statement1; Statement2; . . Statementn; } Where, <access_specifier> --- is either public, private, protected or default. <modifier> --- is a keyword which modifies the function. It can be static, volatile, synchronized etc. <return_type> --- data type of the variable which is returned from the function <function_name> --- name of the function <parameter_list> --- list of data type and variable pair separated by a comma. • • • • 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 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); } public static void main(String[] args) { //Create an object of the class Calculator calc = new Calculator(); //Call the add function calc.add(); } 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(); } } • WAP to simulate a simple calculator using functions(1st type) • Let the name of the functions be • • • • • findSum() findDifference() findProduct() findQuotient() findRemainder() 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) { //Create an object of the class Calculator calc = new Calculator(); //Call the add function calc.add(10, 20); } 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); } } 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); } } 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 ); } } • WAP to simulate a simple calculator using functions(2nd type) • Let the name of the functions be • • • • • findSum() findDifference() findProduct() findQuotient() findRemainder() public int add(int a, int b) { int sum = a + b; //Return the variable sum return sum; } public int add(int a, int b) { //Return an expression return a + b; } 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); } } 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); } } 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); } } 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); } } • WAP to simulate a simple calculator using functions(3rd type) • Let the name of the functions be • • • • • findSum() findDifference() findProduct() findQuotient() findRemainder() 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; } 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); } } 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); } } • 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. • 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 • 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 • Are also called as instance variables • There is one copy of nonstatic 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”; • 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() { } • 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() { }