Pages

Thursday, 19 May 2022

NESTING OF METHODS

A method of a class can be called only by an object of that class or [class itself in the case of static members] using the dot operator. However there is an exception to this, a method can be called by using only its name by another method of the same class. This is known as nesting of methods. When a method in java, calls another method in the same class, it is called Nesting of methods.
Ex1:
class Nesting
 {
    int m, n;
    Nesting (int x, int y)
    {
    m=x; n=y;
    }
    int largest ( )
    {
    if (m >=n) 
    return(m);
    else
    return(n);
    }
    void Display( )
    {
    int large=largest( );
    System.out. println("Largest Value Is :"+ large);
    }
 }
class Nestmain
 {
    public static void main ( String args[ ])
    {
    Nesting nest=new Nesting(10, 20); 
    nest.Display( );
    }
}
Output:
Largest Value Is :20

Note: A method can call any number of methods. It is also possible for a called method to call another method, i. e method 1 may call method 2, which return may call method 3.

Ex2:
import java.util.Scanner; 
class NstMethd
{
int perimeter(int l,int b,int h)
{
int pr = 4*(l+b+h); return pr;
}
int lsa(int l, int b,int h)
{
int pr = perimeter(l, b,h); 
System.out.println("Perimeter of Cuboid:"+pr); 
int sa = 2*h*(l + b);
return sa;
}
int volume(int l, int b, int h)
{
int ar = lsa(l, b,h);
System.out.println("Lateral Surface Area Of Cuboid:"+ar); 
int vol ;
vol = l * b * h; 
return vol;
}
public static void main(String[] args)
{
Scanner s = new Scanner(System.in); 
System.out.print("Enter length of cuboid:"); 
int l = s.nextInt();
System.out.print("Enter breadth of cuboid:"); 
int b = s.nextInt();
System.out.print("Enter height of cuboid:");
int h = s.nextInt();
NstMethd obj = new NstMethd(); 
int vol = obj.volume(l, b, h);
System.out.println("Volume of Cuboid:"+vol);
}
}
Output:
I:\>javac NstMethd.java 
I:\>java NstMethd
Enter length of cuboid:12 
Enter breadth of cuboid:13 
Enter height of cuboid:14 
Perimeter of Cuboid:156
Lateral Surface Area Of Cuboid:700 
Volume Of Cuboid:2184

Create a class in Java
We can create a class in Java using the class keyword.
Syntax:
   class ClassName
    {
    // fields or instance variables
    // methods or functions
    }

Note : fields and methods together called MEMBERS of the class 
Ex:
class Test
{
    int x;
    void displayname()
    {

    }
    int info()
    {

    }
}

Ex1: Java Program to illustrate how to define a class and fields
//Defining a Student class. 
class Student
{
//defining fields
int id; //field or data member or instance variable 
String name;
//creating main method inside the Student class 
public static void main(String args[])
{
//Creating an object or instance
Student s1=new Student(); //creating an object of Student
//Printing values of the object
System.out.println(s1.id); //accessing member through reference variable 
System.out.println(s1.name);
}
}
Output:
0
null

Object and Class Example: main outside the class
Note: We can have multiple classes in different Java files or single Java file. If you define multiple classes in a single Java source file, it is a good idea to save the file name with the class name which has main() method.

Ex2: Java Program to demonstrate having the main method in another class
//Creating Student class. 
class Student
    {
    int id;
    String name;
    }
//Creating another class TestStudent1 which contains the main method class TestStudent
    {
    public static void main(String args[])
    {
    Student s1=new Student(); 
    System.out.println(s1.id); 
    System.out.println(s1.name);
    }
}
Output: 
0
null
 
Different ways to Initialize an Object
There are 3 ways to initialize object in Java. They are 
By reference variable
By method
By constructor

1)Object and Class Example: Initialization through reference
Initializing an object means storing data into the object. Initialize the object through a reference variable. 
Ex:
class Student
{
    int id;
    String name;
}
class TestStudent2
{
public static void main(String args[])
    {
    Student s1=new Student(); 
    s1.id=1001;
    s1.name="Sandhya";
    System.out.println(s1.id+" "+s1.name); //printing members with a white space
    }
}
Output:
1001 Sandhya

Note: We can also create multiple objects and store information in it through reference variable. 
class Student
{
int id;
String name;
}
class TestStudent3
{
    public static void main(String args[])
    {
    //Creating objects
    Student s1=new Student(); 
    Student s2=new Student();
    //Initializing objects s1.id=1001;
    s1.name="Vinay"; 
    s2.id=1002;
    s2.name="Kumar";
    //Printing data
    System.out.println(s1.id+" "+s1.name); 
    System.out.println(s2.id+" "+s2.name);
    }
}
Output:
1001 Vinay
1002 Kumar

2)Object and Class Example: Initialization through method
Ex:
class Student
{
int rollno; 
String name;
void insertRecord(int r, String n)
{
rollno=r; 
name=n;
}
void displayInformation()
{
System.out.println(rollno+" "+name);
}
}

class TestStudent4
{
public static void main(String args[])
{
Student s1=new Student(); 
Student s2=new Student(); 
s1.insertRecord(111,"Karan"); 
s2.insertRecord(222,"Aryan"); 
s1.displayInformation(); 
s2.displayInformation();
}
}
Output: 
111 Karan
222 Aryan 

In the above figure, object gets the memory in heap memory area. The reference variable refers to the object allocated in the heap memory area. Here, s1 and s2 both are reference variables that refer to the objects allocated in memory.

3)Object and Class Example: Initialization through a constructor
class Employee
{
int id;
String name; 
float salary;
void insert(int i, String n, float s)
{
id=i; 
name=n; 
salary=s;
}
void display()
{
System.out.println(id+" "+name+" "+salary);
}
Employee()
{
id=104;
name="Raghu"; 
salary=35000;
}
}
public class TestEmployee
{
public static void main(String[] args)
{
Employee e1=new Employee(); 
Employee e2=new Employee();
Employee e3=new Employee();
Employee e4=new Employee(); // constructor called 
e1.insert(101,"Sree",45000); 
e2.insert(102,"Vidya",25000); 
e3.insert(103,"Padmini",55000);
e1.display();
e2.display();
e3.display();
e4.display();
}
}
Output:
101 Sree 45000.0
102 Vidya 25000.0
103 Padmini 55000.0
104 Raghu 35000.0

Ex:
class Rectangle
{
    int length;     
    int width;
    void insert(int l, int w)
    {
    length=l; 
    width=w;
    }
    void calculateArea()
    {
    System.out.println("Area Of Rectangle :"+length*width);
    }
}
class TestRectangle1
{
    public static void main(String args[])
    {
    Rectangle r1=new Rectangle(); 
    Rectangle r2=new Rectangle(); 
    r1.insert(11,5);
    r2.insert(3,15); 
    r1.calculateArea(); 
    r2.calculateArea();
    }
}
Output:
Area Of Rectangle: 55 
Area Of Rectangle: 45

No comments:

Post a Comment

Servlet - JSP Programs

JSP(Java Server Pages) Programs : JavaServer Pages (JSP) is a Java standard technology that enables you to write dynamic data-driven pages f...