Revision
Revision
Void
2. Write down the syntax of method definition.
<access Modifier> <return Type>< method Name>(parameter List)
{
// Method body
// Statements to execute
// Optional return statement
}
3. Parameters used in the method definition are called:
Formal parameters
4. How many values can be returned from a method?
Only one value can be returned
5. Write a program using method check() to check whether a number is
divisible by 11 or not.
import java.util.*;
public class Divisible
{
public static void check()
{
Scanner sc=new Scanner(System.in);
System.out.println("enter the number");
int n=sc.nextInt();
if(n%11==0)
System.out.println("the number is divisible by 11");
else
System.out.println("the number is not divisible by 11");
}
public static void main()
{
check();
}
}
6. Write a program using functions called area() to compute the area of
a (a) circle = 3.14 * r * r (b) square(side*side) (c) rectangle
(length*breadth) ‘ (3 mark) Display the result.
if (lastDigit == 3)
{
System.out.println("The number ends with 3.");
} else if (lastDigit == 7)
{
System.out.println("The number ends with 7.");
}
else
{
System.out.println("The number does not end with 3 or 7.");
}
PASS BY VALUE
• A copy of actual parameter is passed to formal parameters.
• Any change made in formal parameter does not reflect on actual
parameter.
• All the primitive type data like int,float,char,double,Boolean etc are
the default passed by value
public class Change
{
void sample(int x,int y)
{
x=x+10;
y=y+10;
System.out.println(“the values are”+x+” “+y);
}
public static void main()
{
Change ob=new Change();
int p=2, q=3;
ob.sample(p,q);
System.out.println(p+” ”+q);
}
}
PASS BY REFERENCE
• Here the reference/address of actual parameter is passed to the
formal parameter
• Any change made in the formal parameter will be reflected on the
actual parameter
• The non primitive type data like arrays and objects are passed by
reference
(When you pass an argument to a function, it passes a reference to the
actual data. Changes made to the parameter affect the original data.)
PURE METHODS
• A pure method is the one that takes primitives as
arguments but does not modify the passed primitives
• Pure method when invoked does not cause any change
in the state of an object.
• A method that does not modify any state and does not
depend on any state other than its input parameters. It
always produces the same output for the same input.
public static int add(int a, int b)
{
return a + b;
}
IMPURE METHODS/ mutator methods
An impure method modified or changes the value of the actual
parameters
It change the state of its object
It means that the value of actual parameters get modified or changed .
A method that modifies state or interacts with external state (e.g.,
instance variables, global variables) or relies on it. Its output might vary
for the same input.
• METHOD OVERLOAING
• Several method definitions with same name that are
differentiable by the type of arguments or number of
arguments
• How does a method call distinguish its own method
definition when method name are same?
By matching the actual argument with formal
argument with data type and number of arguments
Uses
By using method overloading the programmer need not
bother about the method name .
Method overloading implements polymorphism.
• A method header contains method name along with a number of
parameters.
For eg. (overloaded function prototypes)
1. What are the values of num1 and num2 after the following function is executed, if the values passed are 5 and
15:
void multiplyAdd(int num1, int num2)
{
num1 = num1 * num2;
num2 = num1 + num2;
num1 = num2 - num1;
System.out.println(num1 + ", " + num2);}
Ans: 15,90
2. What are the values of x and y after the following function is executed, if the values passed are 10 and 5:
void modify(int x, int y)
{
x = x * 2;
y = y / 2;
x = x + y;
y = x - y;
System.out.println(x + ", " + y);
}
• Ans 22,20
Text book pg no 325
Design a class to overload a function_num_cal() as follows:
(i) void num_cal (int num, char ch) with one integer argument and one
character argument. It computes the square of an integer if choice ch is
's otherwise, computes its cube.
(ii) void num_cal (int a, int b, char ch) with two integer arguments and
one character argument. It computes the product of integer arguments
if ch is 'p' else adds the integers.
(iii) void num_cal (String str1, String str2) with two String arguments
prints the two Strings are equal or not. [ICSE 2009]
Text book pg no 327
Design a class to overload a function area( ) as follows:
• double area (double a, double b, double c) with three double
arguments, returns the area of a scalene triangle using the formula:
area = √(s(s-a)(s-b)(s-c))
where s = (a+b+c) / 2
• double area (int a, int b, int height) with three integer arguments,
returns the area of a trapezium using the formula:
area = (1/2)height(a + b)
• double area (double diagonal1, double diagonal2) with two double
arguments, returns the area of a rhombus using the formula:
area = 1/2(diagonal1 x diagonal2)
Design a class to overload a function sumSeries() as follows:
(i) void sumSeries(int n, double x): with one integer argument and one
double argument to find and display the sum of the series given below:
𝑠=𝑥/1−𝑥/2+𝑥/3−𝑥/4+𝑥/5... ... ... 𝑡𝑜 𝑛 𝑡𝑒𝑟𝑚𝑠
(ii) void sumSeries(): to find and display the sum of the following
series:
𝑠=1+(1×2)+(1×2×3)+... ... ... +(1×2×3×4... ... ... ×20)s=1+(1×2)+(1×2×3)+..
. ... ... +(1×2×3×4... ... ... ×20)
When i = 1: s=6.0/1=6.0
When i = 2: s=6.0−(6.0/2)=6.0−3.0=3.0
When i = 3: s=3.0+(6.0/3)=3.0+2.0=5.0
END
• Write a program using function area() calculate the area
• Rectangle
• Square
• Circle
• Display the menu to output the area as per the users choice
switch (expression)
{
case value1:
// Code to be executed if expression equals value1
break;
case value2:
break;
// You can have any number of case
statements
default:
// Code to be executed if expression doesn't match any case
}