0% found this document useful (0 votes)
20 views64 pages

Revision

Iteration Statements 1

Uploaded by

revathyrenjit
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views64 pages

Revision

Iteration Statements 1

Uploaded by

revathyrenjit
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 64

1. Name the keyword that indicates the method does not have any return type.

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)

Int area(int a, int b);


Double area(double a, double b);
Double area(int a, int b);
Int area(int a, int b);
Int area(int x, inty);// here this is not valid as the type and number of
parameters are same

Public int area(int a, int b);


Public int area(int a, int b,int c);
Neither receiving values nor returning
outcome to the caller
• 1. write a program to input name ,rollno,class,division
and three marks of a student using method stuDetail().
• 2. write a program using method revNumber to input a
two digit number and extract the first digit and last digit.
• 3. write a program to input a number and check whether
the number ends with 3 0r 7
• 4. write a program to input a number and check whether
the number is completely divisible by 2 and 3
• 5. write a program to input two numbers and check
whether the first one is a multiple of second using
method check()
Receiving value and not returning outcome to
the caller
1. Write a program to find the sum of two numbers using method sum() that
receives two numbers as arguments, find sum and display the result.
2. Write a program to perform addition, subtraction and multiplication using
method.
sum()= that receives two numbers as arguments find sum and display the
result.
difference()= that receives two numbers as arguments, find difference and
display result
Product()=that receives two numbers as arguments, find product and display
the result.
• Write a program to interchange the values of two variables using a
method that receives two number as parameter with no return value.
3. Write a program to calculate the area of rectangle using
method area()
• area() receives length and breadth as arguments calculate the
area and display the result.
4. Define two methods rarea() and sarea() for calculating area of
rectangle and area of square .
sarea() – it receives side as parameter, after calculating display the
result.
rarea()- it receives the length and breadth as parameter after
calculating display the result.
Not receiving value but return outcome to
the caller.
• Input length and breadth of a rectangle and find the area using
method area() the method takes the input calculate area and return
the result to the main()
• Input side() of a square , find the area using method- sarea(), the
method takes the input .calculate the area and return the result to
the main()
• Input radius of a circle and find the area using method area() the
method takes the input calculate area and return the result to the
main()
Receiving value and returning outcome to the
caller
1. Input side(int) of a square and find the area using method square() the
method receives side as argument, calculate area and return the result to
the main()
2. Input length and breadth of a rectangle and find the area using method
rarea() the method receives length and breadth as argument . Calculate
area and return the result to the main()
3. Define two methods for calculating area of rectangle and area of square .
One method receives length and breadth as parameters ,after calculation
return result to the main()
Second method receives side as parameter and after calculation return the
result to the main()
• Write a program using method check() that receives number as
argument check whether the number ends with 9 or not and return
the result to the main () return either true or false
• Write a program using method check() that receives number as
argument check whether the number ends with 5 or not and return
the result to the main () return either true or false
• Write a program using method check() that receives a number as
parameter and check whether the number is completely divisible by 9
or not and return the result as string
• Write a program using method check() that receives a number from
main and check whether the number is even or odd and return the
result to the main as string
Home work
• Write a program using method check() that receives a number from
main() and check whether the number is even or odd and return the
result to the main() as character.
Method overloading
Define two methods area()for calculating area of rectangle and area of
a square.
area() it receives length and breadth as parameter , return the result to
the main
area() it receives side as parameter and return the result to the main
Page no 323
Write a class with the name volume using function overloading that
computes the volume of a cube, a sphere and a cuboid.
Volume of a cube(vc)=s*s*s
Volume of a sphere(vs)=4/3*22/7*r*r*r
Volume of a cuboid(vcd)= l*b*h
<access Modifier> <return Type>< method Name>(parameter List)
{
// Method body
// Statements to execute
// Optional return statement
}
1. Write the function prototype for a function named isEven that takes an integer variable
(number) as its argument and returns a boolean value.
2. Write the function prototype for a function named findMax that takes three integer variables
(num1, num2, num3) as arguments and returns an integer value.
3. Write the prototype of a function search which takes two arguments a string and a character
and returns an integer values.
4. Write the prototype of a function change that takes two integers as parameters and returns a
double.
5. Give the prototype of a function search which receives a sentence sentnc and a word wrd and
return 1 or 0.
6. Write the function prototype for a function named convertToUpperCase that takes a String
variable (input) as its argument and returns a String value.
7. Write the function prototype for a function named multiply that takes two float variables (x and
y) as arguments and returns a float value.
8. Write the function prototype for a function named reverseString that takes a String variable
(str) as its argument and returns a String value.
9. Write the function prototype for a function named calculateAverage that takes two double
variables (a and b) as arguments and returns a value of double data type.
• double calculateAverage(double a, double b);

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

Computes the sum of factorials from 1 to 5.


The factorials are:
1! = 1
2! = 2
3! = 6
4! = 24
5! = 120
The sum of these factorials is: 1+2+6+24+120=1531 + 2 + 6 + 24 + 120 = 1531+2+6+24+120=153
Recursive function
• A recursive function is a function that calls itself in order to solve a
problem
public class Factorial {
// Recursive method to calculate factorial
public static int factorial(int n)
{
if (n <= 1)
{
return 1;
} else {
return n * factorial(n - 1); // Recursive case
}
}

public static void main()


{
int number = 5;
int result = factorial(number);
System.out.println("Factorial of " + number + " is: " + result);
• This flow ensures that each recursive call returns its result to the
calling method until the final result is computed and printed. Each
step relies on the previous step's computed result, building up until
the final desired computation is achieved.

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
}

You might also like