0% found this document useful (0 votes)
3 views

Chapter-8

Uploaded by

xaeabhishek
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Chapter-8

Uploaded by

xaeabhishek
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

CHAPTER:8: -CLASSES, OBJECT AND METHOD-2

 Actual and Formal Parameters

 Actual parameters
 Actual parameters are the values that are passed to a function when it is called. They are
also known as arguments.
 When a function is called, the actual parameters are evaluated, and their values are copied
into the corresponding formal parameters of the called function.

 Formal parameters
 Formal parameters are the variables declared in the function header that are used to receive
the values of the actual parameters passed during function calls.
 They are also known as function parameters.
 Formal parameters are used to define the function signature and to specify the type and
number of arguments that a function can accept.

Example:
Formal Parameters
class Parameter
{
void m1(int i, int j)
{
System.out.println(i+j);
}
}
class Main
{
public static void main(String[] args)
{
Parameter p=new Parameter();
p.m1(20, 30);
}
} Actual Parameters
/* Output
* 50
*/

By:Ankur Patel
 Topic: Passing Array to Function
 Functions are used to break and divide a huge code into small chunks so that the code
becomes more understandable and thus reduces the complexity of the code. Arrays are the
homogenous data structures for reducing the code complexity, increasing efficiency, and
reducing the execution time of the code. We make use of the array and think of both these
time-saving parts of programming are implemented together. Thereby exists the concept
of Passing Array to Function.
 In this section, we will learn how we can pass an array to a user-defined function and will
notice how it makes the code more optimistic as well as efficient.
 Generally, the purpose of passing an array to a function is to transfer a large amount of data
between methods. To pass an array to a function, just pass the array as function's parameter
(as normal variables), and when we pass an array to a function as an argument, in actual
the address of the array in the memory is passed, which is the reference. Thus, any changes
in the array within the method will affect the actual array values.

/*Array As Arguments:1:- Printing Array Elements*/

import java.util.*;

class PrintArrayElements
{
void array(int x[])
{
for(int i=0;i<x.length;i++)
{
System.out.println("Array Elements are:x["+i+"]="+x[i]);
}
}
}
class Main
{
public static void main(String [] args)
{
Scanner sc=new Scanner(System.in);
System.out.print("Enter Size of array :");
int n=sc.nextInt();
int []a=new int[n];
for(int i=0;i<a.length;i++)
{
System.out.print("Enter Elements of array:a["+i+"]=");
a[i]=sc.nextInt();
}
System.out.println();

By:Ankur Patel
PrintArrayElements pa=new PrintArrayElements();
pa.array(a);
}
}
/* Output
Enter Size of array :3
Enter Elements of array:a[0]=15
Enter Elements of array:a[1]=52
Enter Elements of array:a[2]=25

Array Elements are:x[0]=15


Array Elements are:x[1]=52
Array Elements are:x[2]=25 */

/*Array As Arguments:2:- Printing Array Elements in Increasing Order*/

import java.util.*;
class PrintArrayElements
{
void array(int x[])
{
for(int i=0;i<x.length;i++)
{
System.out.println("Array Elements are:x["+i+"]="+x[i]);
}
}
}
class Main
{
public static void main(String [] args)
{
Scanner sc=new Scanner(System.in);
System.out.print("Enter Size of array :");
int n=sc.nextInt();
int []a=new int[n];
for(int i=0;i<a.length;i++)
{
System.out.print("Enter Elements of array:a["+i+"]=");
a[i]=sc.nextInt();
}
Arrays.sort(a);
System.out.println();
PrintArrayElements pa=new PrintArrayElements();

By:Ankur Patel
pa.array(a);
}
}
/* Output
Enter Size of array :4
Enter Elements of array:a[0]=26
Enter Elements of array:a[1]=13
Enter Elements of array:a[2]=65
Enter Elements of array:a[3]=35

Array Elements are:x[0]=13


Array Elements are:x[1]=26
Array Elements are:x[2]=35
Array Elements are:x[3]=65 */

/*Array As Arguments:3:- Printing Array Elements in Decreasing Order*/

import java.util.*;
class PrintArrayElements
{
void array(int x[])
{
for(int i=x.length-1;i>=0;i--)
{
System.out.println("Array Elements are:x["+i+"]="+x[i]);
}
}
}
class Main
{
public static void main(String [] args)
{
Scanner sc=new Scanner(System.in);
System.out.print("Enter Size of array :");
int n=sc.nextInt();
int []a=new int[n];
for(int i=0;i<a.length;i++)
{
System.out.print("Enter Elements of array:a["+i+"]=");
a[i]=sc.nextInt();
}
Arrays.sort(a);
System.out.println();

By:Ankur Patel
PrintArrayElements pa=new PrintArrayElements();
pa.array(a);
}
}

/* Output
Enter Size of array :4
Enter Elements of array:a[0]=56
Enter Elements of array:a[1]=25
Enter Elements of array:a[2]=13
Enter Elements of array:a[3]=65

Array Elements are:x[3]=65


Array Elements are:x[2]=56
Array Elements are:x[1]=25
Array Elements are:x[0]=13 */

/*Array As Arguments:4:- Printing only Even Array Elements in


increasing Order*/

import java.util.*;
class PrintArrayElements
{
void array(int x[])
{
for(int i=0;i<x.length;i++)
{
if(x[i]%2==0)
{
System.out.println("Even Array Elements are:x["+i+"]="+x[i]);
}
}
}
}
class Main
{
public static void main(String [] args)
{
Scanner sc=new Scanner(System.in);
System.out.print("Enter Size of array :");
int n=sc.nextInt();
int []a=new int[n];

By:Ankur Patel
for(int i=0;i<a.length;i++)
{
System.out.print("Enter Elements of array:a["+i+"]=");
a[i]=sc.nextInt();
}
Arrays.sort(a);
System.out.println();
PrintArrayElements pa=new PrintArrayElements();
pa.array(a);
}
}

/* Output
Enter Size of array :5
Enter Elements of array:a[0]=21
Enter Elements of array:a[1]=26
Enter Elements of array:a[2]=98
Enter Elements of array:a[3]=99
Enter Elements of array:a[4]=12

Even Array Elements are:x[0]=12


Even Array Elements are:x[2]=26
Even Array Elements are:x[3]=98 */

/*Array As Arguments:5:- Printing Greatest and smallest Array


element*/

import java.util.*;
class Max_Min
{
int max,min;
void max(int x[])
{
max=x[0];
for(int i=0;i<x.length;i++)
{
if(x[i]>max)
{
max=x[i];
}
}
}

By:Ankur Patel
void min(int x[])
{
min=x[0];
for(int i=0;i<x.length;i++)
{
if(x[i]<min)
{
min=x[i];
}
}
}
void show()
{
System.out.println("Max="+max);
System.out.println("Min="+min);
}
}
class Main
{
public static void main(String [] args)
{
Scanner sc=new Scanner(System.in);
System.out.print("Enter Size of array :");
int n=sc.nextInt();
int []a=new int[n];
for(int i=0;i<a.length;i++)
{
System.out.print("Enter Elements of array:a["+i+"]=");
a[i]=sc.nextInt();
}
System.out.println();
Max_Min m=new Max_Min();
m.max(a);
m.min(a);
m.show();
}
}

/* Output
Enter Size of array :5
Enter Elements of array:a[0]=25
Enter Elements of array:a[1]=65
Enter Elements of array:a[2]=89
Enter Elements of array:a[3]=12

By:Ankur Patel
Enter Elements of array:a[4]=123

Max=123
Min=12 */

 Array of objects
 Java is an object-oriented programming language. Most of the work done with the help of
objects.
 We know that an array is a collection of the same data type that dynamically creates objects
and can have elements of primitive types.
 Java allows us to store objects in an array.
 In Java, the class is also a user-defined data type. An array that conations class type
elements are known as an array of objects. It stores the reference variable of the object.

//WAP to understand array of Object


//Ex:-1:-WAP to accept 3 Students data and print the same;

import java.util.*;
class ArrayofObject_1
{
public static void main(String[] arg)
{
Scanner sc = new Scanner(System.in);
//Creating array of class
System.out.print("Enter no.of Students n=");
int n=sc.nextInt();
Student s[] = new Student[n];
for(int i=0; i<s.length;i++)
{
//creating object of array
s[i] = new Student();
System.out.println("Enter Student "+(i+1)+" Data");
s[i].getData();
}
System.out.println("=================================== ");
System.out.println("------Your Students record is:------- ");
System.out.println("=================================== ");
for(int i=0; i<s.length;i++)
{
s[i].printData();
}
}

By:Ankur Patel
}
class Student
{
int roll_no;
String name;
double java_marks;

void getData()
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter Roll No: ");
roll_no = sc.nextInt();
sc.nextLine();
System.out.print("Enter Name: ");
name = sc.nextLine();
System.out.print("Enter java Marks: ");
java_marks = sc.nextDouble();
System.out.println();
}
void printData()
{
System.out.println("Roll number = "+roll_no);
System.out.println("Student Name = "+name);
System.out.println("JAVA-1 marks = "+java_marks);
System.out.println();
}
}
/*
Enter no.of Students n=2
Enter Student 1 Data
Enter Roll No: 01
Enter Name: AP
Enter java Marks: 21

Enter Student 2 Data


Enter Roll No: 02
Enter Name: VP
Enter java Marks: 23

Output:
===================================
------Your Students record is:-------
===================================
Roll number = 1
Student Name = AP

By:Ankur Patel
JAVA-1 marks = 21.0

Roll number = 2
Student Name = VP
JAVA-1 marks = 23.0 */

 Call by Value and Call by Reference in Java

 Call by Value (Pass by Value) in Java


 “Call by value” in Java means that argument’s value is copied and is passed to the
parameter list of a method.
 That is, when we call a method with passing argument values to the parameter list,
these argument values are copied into the small portion of memory and a copy of each
value is passed to the parameters of the called method.
 When these values are used inside the method either for “read or write operations”, we
are actually using the copy of these values, not the original argument values which are
unaffected by the operation inside the method.
 That is, the values of the parameters can be modified only inside the scope of the
method but such modification inside the method doesn’t affect the original passing
argument.
 When the method returns, the parameters are gone and any changes to them are lost.
This whole mechanism is called call by value or pass by value.
 Let’s understand the concept “pass by value” mechanism by an example program and
related diagram.

 Examples Program based on Call by Value in Java

 Let’s create an example program where we will pass argument value to the parameter
of the method using call by value.
class CallbyValue
{
int change(int b)
{

++b; // Changes will be in the local variable only.


return b;
}
}
class Main
{
public static void main(String[] args)
{

By:Ankur Patel
// Create an object of class.
CallbyValue obj = new CallbyValue();
int a = 20;
int x = obj.change(a);
System.out.println("Value of a after passing: " +a);
System.out.println("Value of x after modifying: " +x);
}
}

/*E:\java ARP>javac Ex1CV.java

E:\java ARP>java Main


Value of a after passing: 20
Value of x after modifying: 21 */

 The below figure will illustrate how call by value in java works in the above program.

By:Ankur Patel
 Lets See another example for swapping values of a and b by Call by Value

class CallByValue
{
void swapFunction(int a, int b)
{
System.out.println("Before swapping(Inside), a = " + a + " b = " + b);
// Swap n1 with n2
int c = a;
a = b;
b = c;
System.out.println("After swapping(Inside), a = " + a + " b = " + b);
}

}
class Main
{
public static void main(String[] args)
{
CallByValue CV=new CallByValue();
int a = 30;
int b = 45;
System.out.println("Before swapping, a = " + a + " and b = " + b);

// Invoke the swap method

CV.swapFunction(a, b);
System.out.println("**Now, Before and After swapping values will be same here**:");
System.out.println("After swapping, a = " + a + " and b is " + b);
}
}

E:\java ARP>javac CBV.java

E:\java ARP>java Main


Before swapping, a = 30 and b = 45
Before swapping(Inside), a = 30 b = 45
After swapping(Inside), a = 45 b = 30
**Now, Before and After swapping values will be same here**:
After swapping, a = 30 and b is 45

By:Ankur Patel
 Call by Reference in Java
 In Java “Call by Reference” means passing a reference (i.e. address) of the object by value
to a method. We know that a variable of class type contains a reference (i.e. address) to an
object, not object itself.
 Thus, when we pass a variable of class type as an argument to a method, actually, a copy
of a reference (address) to an object is passed by value to the method, not a copy of the
object itself. This is because Java does not copy the object into the memory.
 Actually, it copies reference of the object into the memory and passes the copy to the
parameters of the called method.
 If we change the reference of the object then the original reference does not get changed
because this reference is not original. It’s a copy.

 Lets See the example of Call by Reference

class CallbyRef
{
/*
* The original value of 'a' will be changed as we are trying
* to pass the objects. Objects are passed by reference.
*/

int a = 10;
void call(CallbyRef eg)
{
eg.a = eg.a+10;
}
}
class Main
{
public static void main(String[] args)
{

CallbyRef eg = new CallbyRef();


System.out.println("Before call-by-reference: " + eg.a);

// passing the object as a value using pass-by-reference


eg.call(eg);
System.out.println("After call-by-reference: " + eg.a);

By:Ankur Patel
}
E:\java ARP>javac CallbyRef.java

E:\java ARP>java Main


Before call-by-reference: 10
After call-by-reference: 20

 Object as an Argument
 In Java “Object as an argument” means passing a reference (i.e. address) of the object by
value to a method. We know that a variable of class type contains a reference (i.e. address)
to an object, not object itself.
 Thus, when we pass a variable of class type as an argument to a method, actually, a copy
of a reference (address) to an object is passed by value to the method, not a copy of the
object itself. This is because Java does not copy the object into the memory.
 Actually, it copies reference of the object into the memory and passes the copy to the
parameters of the called method.
 If we change the reference of the object then the original reference does not get changed
because this reference is not original. It’s a copy.

//(9)Object as an argument
class Point
{
int x,y;
void set(int a,int b)
{
x=a;
y=b;
}
void get()
{
System.out.println(x);
System.out.println(y);
}
void copy(Point p1)//Object received as an argument
{
x=p1.x;
y=p1.y;
}
}
class Main
{

By:Ankur Patel
public static void main(String args[])
{
Point p1=new Point();
p1.set(10,20);
p1.get();
Point p2=new Point();
p2.copy(p1);//object passed as an argument
p2.get();
}
}
/* Output:
10
20
10
20
*/

// object as an argument average of marks of two test

class Test
{
double c_marks, java_marks;
String Name, test;
double Total_Marks;
double Avg_Marks;

void set(String n, String t, double x, double y)


{
Name = n;
test = t;
c_marks = x;
java_marks = y;
}

void get()
{
System.out.println("Name=" + Name);
System.out.println("Test=" + test);
System.out.println("c_marks= " + c_marks);
System.out.println("java_marks =" + java_marks);
System.out.println();
}

By:Ankur Patel
//Method receiving object as argument
void average(Test t1, Test t2)
{
c_marks = t1.c_marks + t2.c_marks;
java_marks = t1.java_marks + t2.java_marks;
Total_Marks = (c_marks + java_marks);
Avg_Marks = (c_marks + java_marks) / 2;

}
void printTotalAverage()
{
System.out.println("Total_Marks of T1 and T2= " + Total_Marks);
System.out.println("Avg_Marks of T1 and T2= " + Avg_Marks);
}
}

class Main1
{
public static void main(String args[])
{
Test t1=new Test();
Test t2=new Test();
t1.set("Milan","T1",21.0,19.0);
t2.set("Milan","T2",23.0,21.0);
t1.get();
t2.get();
Test t3=new Test();
t3.average(t1,t2);//Object as an argument
t3.printTotalAverage();
}
}

/*
Name=Milan
Test=T1
c_marks= 21.0
java_marks =19.0

Name=Milan
Test=T2
c_marks= 23.0
java_marks =21.0

Total_Marks of T1 and T2= 84.0

By:Ankur Patel
Avg_Marks of T1 and T2= 42.0
*/

 Method returning object

// object as an argument average of marks of two test with method


returning object

class Test
{
double c_marks, java_marks;
String Name, test;
double Total_Marks;
double Avg_Marks;

void set(String n, String t, double x, double y)


{
Name = n;
test = t;
c_marks = x;
java_marks = y;
}

void get()
{
System.out.println("Name=" + Name);
System.out.println("Test=" + test);
System.out.println("c_marks= " + c_marks);
System.out.println("java_marks =" + java_marks);
System.out.println();
}

Test average(Test t1, Test t2)


{
Test t4=new Test();
c_marks = t1.c_marks + t2.c_marks;
java_marks = t1.java_marks + t2.java_marks;
t4.Total_Marks = (c_marks + java_marks);
t4.Avg_Marks = (c_marks + java_marks) / 2;
return t4;//returning object

By:Ankur Patel
}
void printTotalAverage()
{
System.out.println("Total_Marks of T1 and T2= " + Total_Marks);
System.out.println("Avg_Marks of T1 and T2= " + Avg_Marks);
}
}

class Main1
{
public static void main(String args[])
{
Test t1=new Test();
Test t2=new Test();
t1.set("Milan","T1",21.0,19.0);
t2.set("Milan","T2",23.0,21.0);
t1.get();
t2.get();
Test t3=new Test();

Test t5=t3.average(t1,t2);//storing the returning object


t5.printTotalAverage();
}
}
/*
Name=Milan
Test=T1
c_marks= 21.0
java_marks =19.0

Name=Milan
Test=T2
c_marks= 23.0
java_marks =21.0

Total_Marks of T1 and T2= 84.0


Avg_Marks of T1 and T2= 42.0
*/

By:Ankur Patel

You might also like