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

Slides:



Advertisements
Similar presentations
PACKAGES. PACKAGES IN JAVA A package is a collection of related classes and interfaces in Java Packages help in logical grouping of classes and interfaces.
Advertisements

Unit 181 Recursion Definition Recursive Methods Example 1 How does Recursion work? Example 2 Problems with Recursion Infinite Recursion Exercises.
Slides prepared by Rose Williams, Binghamton University Chapter 11 Recursion.
1 2-D Arrays Overview l Why do we need Multi-dimensional array l 2-D array declaration l Accessing elements of a 2-D array l Declaration using Initializer.
Unit 191 Recursion General Algorithm for Recursion When to use and not use Recursion Recursion Removal Examples Comparison of the Iterative and Recursive.
1 One-Dimensional (1-D) Array Overview l Why do we need 1-D array l 1-D array declaration and Initialization l Accessing elements of a 1-D array l Passing.
SCOPE & I/O CSC 171 FALL 2004 LECTURE 5. CSC171 Room Change Thursday, September 23. CSB 209 THERE WILL BE A (group) QUIZ! - topic: the CS department at.
1 Methods Overview l Closer Look at Methods l Parameter passing l Passing parameters by value l Passing parameters by reference.
1 Memory Model of A Program, Methods Overview l Closer Look at Methods l Memory Model of JVM »Method Area »Heap »Stack l Preview: Parameter Passing.
1 Methods Overview l Closer Look at Methods l Parameter passing l Passing parameters by value l Passing parameters by reference.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
Slides prepared by Rose Williams, Binghamton University Chapter 11 Recursion.
1 Memory Model of A Program, Methods Overview l Memory storage areas for an executing program l Introduction to methods and methods definitions l General.
COMP 110 Introduction to Programming Mr. Joshua Stough September 10, 2007.
Reading Input -- the Keyboard Class The Keyboard class is NOT part of the Java standard class library The Keyboard class is in package cs1. It provides.
Java Methods By J. W. Rider. Java Methods Modularity Declaring methods –Header, signature, prototype Static Void Local variables –this Return Reentrancy.
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals; Apply data abstraction.
Methods CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Classes and Objects. Topics The Class Definition Declaring Instance Member Variables Writing Instance Member Methods Creating Objects Sending Messages.
Static Keyword. What is static The static keyword is used when a member variable of a class has to be shared between all the instances of the class. All.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
CSI 1390: Introduction to Computers TA: Tapu Kumar Ghose Office: STE 5014
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
CS1101: Programming Methodology Recitation 3 – Control Structures.
Lecture # 8 Constructors Overloading. Topics We will discuss the following main topics: – Static Class Members – Overloaded Methods – Overloaded Constructors.
1. 2 Reference... Student stu; Reference of Student stu When the reference is created it points to a null value. Before access the reference, objects.
ARRAYS 1 TOPIC 8 l Array Basics l Arrays and Methods l Programming with Arrays Arrays.
Chapter 3: Developing Class Methods Object-Oriented Program Development Using Java: A Class-Centered Approach.
CIS 260: App Dev I. 2 Programs and Programming n Program  A sequence of steps designed to accomplish a task n Program design  A detailed _____ for implementing.
Methods F Hello World! F Java program compilation F Introducing Methods F Declaring Methods F Calling Methods F Passing Parameters by value F Overloading.
Methods in Java. Program Modules in Java  Java programs are written by combining new methods and classes with predefined methods in the Java Application.
Java 1.5 The New Java Mike Orsega Central Carolina CC.
Java - Classes JPatterson. What is a class? public class _Alpha { public static void main(String [] args) { } You have been using classes all year – you.
Rina System development with Java Instructors: Rina Zviel-Girshin Lecture 4.
Classes. Student class We are tasked with creating a class for objects that store data about students. We first want to consider what is needed for the.
Lecture 10:Static & Final Kenya © 2005 MIT-Africa Internet Technology Initiative MyMath Example public class MyMath { public double PI = ;
© 2007 Lawrenceville Press Slide 1 Chapter 8 Objects  A variable of a data type that is a class. Also called an instance of a class.  Stores data  Can.
1 Chapter 5: Defining Classes. 2 Basics of Classes An object is a member of a class type What is a class? Fields & Methods Types of variables: –Instance:
Static. 2 Objectives Introduce static keyword –examine syntax –describe common uses.
Department of Computer Engineering Using Objects Computer Programming for International Engineers.
User Defined Methods Methods are used to divide complicated programs into manageable pieces. There are predefined methods (methods that are already provided.
CSI 3125, Preliminaries, page 1 Java I/O. CSI 3125, Preliminaries, page 2 Java I/O Java I/O (Input and Output) is used to process the input and produce.
MIT AITI 2004 Lecture 10 Static and Final. public class MyMath { public double PI = ; public double square (double x) { return x * x; } public.
CSI 3125, Preliminaries, page 1 Overloading Methods In Java it is possible to define two or more methods within the same class that share the same name,
I/O Basics 26 January Aside from print( ) and println( ), none of the I/O methods have been used significantly. The reason is simple: most real.
 Static  Example for Static Field  Example for Static Method  Math class methods  Casting  Scope of Declaration  Method Overloading  Constructor.
Classes - Intermediate
Object Oriented Programming I ( ) Dr. Adel hamdan Part 03 (Week 4) Dr. Adel Hamdan Date Created: 7/10/2011.
Methods.
Topics Instance variables, set and get methods Encapsulation
CiS 260: App Dev I. 2 Introduction to Arrays n An array is an object that contains a collection of components (_________) of the same data type. n For.
5.1 Basics of defining and using classes A review of class and object definitions A class is a template or blueprint for an object A class defines.
Structured Programming Dr. Atif Alhejali Lecture 4 Modifiers Parameters passing 1Structured Programming.
Class Everything in Java is in a class. The class has a constructor that creates the object. If you do not supply a constructor Java will create a default.
Exam 2 EXAM 2 Thursday!!! 25% of Final Grade Know: loops, switch/case Files Input failure (e.g. scan.hasNextInt())
Methods What is a method? Main Method the main method is where a stand alone Java program normally begins execution common compile error, trying.
Programming in Java Transitioning from Alice. Becomes not myFirstMethod but …. public static void main (String[] arg) { // code for testing classes goes.
Methods Matthew Harrison. Overview ● There are five main aspects of methods... ● 1) Modifiers – public, private ● 2) Method Name ● 3) Parameters ● 4)
Static data members Constructors and Destructors
Examples of Classes & Objects
Chapter 3: Using Methods, Classes, and Objects
University of Central Florida COP 3330 Object Oriented Programming
Method Mark and Lyubo.
Introduction to Java Programming
Classes & Objects: Examples
Unit-2 Objects and Classes
Classes Lecture 7 from Chapter /1/11.
JAVA Constructors.
Presentation transcript:

Saravanan.G

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

[ ] [ ] ([ ]) { 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.

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(1 st 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(2 nd 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(3 rd 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 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

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() {}{}