Class 10 ICSE - Java Pure & Impure Method Programs

Class 10th Java aims to empower students by enabling them to build their own applications introducing some effective tools to enable them to enhance their knowledge, broaden horizons, foster creativity, improve the quality of work and increase efficiency.
It also develops logical and analytical thinking so that they can easily solve interactive programs. Students learn fundamental concepts of computing using object oriented approach in one computer language with a clear idea of ethical issues involved in the field of computing
Class 10th java topics includes revision of class 9th, constructors, user-defined methods, objects and classes, library classes , etc.

Impure Method



Program 1: Write a program showing the functionality of impure method.
Solution:
class Sum 
{
    int sum; // global variable
    public int Add(int a , int b)
    {
        sum + = a + b;
        return sum; //returning value
    }
    public static void main(String args[])
    {
        Sum obj = new Sum(); //creating object
        System.out.println("Value of variable sum before calling =" +obj.sum); //0
        System.out.println("Sum of two variables ="+obj.Add(2,3)); //5
        System.out.println("Value of variable sum after Ist calling  =" +obj.sum); //5
        System.out.println("Sum of two variables ="+obj.Add(2,3)); //10
        System.out.println("Value of variable sum after Ist calling  =" +obj.sum); //10
        // the value of sum is changed after each time the function is called
    }
    
}
                        

In the above example, we have created a method named Add(). The method takes two parameters a and b. Here before calling the method the value of variable sum is 0 and after calling its value becomes 5. Hence, the state of global variable sum is changing after each call.
Program 2: Write a program to show the functionality of impure method
Solution:
class Number 
{
    int num=1; // global variable
   public void getNum()
   {
       
       num = num*2;
       System.out.println("Number=" +num);
       
   }
   public static void main(String args[])
   {
     Number obj = new Number();
     System.out.println("Value for first time=" +obj.num);//1
     obj.getNum(); //2
     System.out.println("Value for Second time=" +obj.num);//2
       
   }
    
}

                        

In the above example, we have created a method named GetNum(). Each time the method is called the value of num variable changes.
Program 3: Write a program to show the functionality of Impure Method.
Solution:
class Account 
{
    double balanceamt; // global variable
    public double Credit(double amount)
    {
        balanceamt = balanceamt + amount;
        return balanceamt; //returning value
    }
    public static void main(String args[])
    {
        Account obj = new Account(); //object creation
        System.out.println("value of balance amount before credit=" +obj.balanceamt);//0.0
        System.out.println("value of balance amount after credit=" +obj.Credit(10000));//10000.0
        
    }
}
                        

In the above example, we have created a method named Credit(). The method takes a parameter amount. Here, the balance amount is keep on changing after every call.

Pure Method



Program 4: Write a program using method to find the square of a number.
Solution:
class square
{
    public int findSquare(int n) // formal parameter
    {
        int res = n * n;
        return res;
    }
    
    public void ComputeSquare()
    {
        int num = 5;
        int myResult = findSquare(num); // actual parameter
        System.out.println("Square of " + " " + num + " " + "is =" + myResult); // Square of 5 is =25
    }
}

                        

In the above example, we have created a method named findSquare(). The method takes a parameter n. The method findSquare() returns the square of the number passed.
Program 5: Write a program using methods to compare two numbers and find which number is greatest.
Solution:
class  Compare   
{  
    // Method with an integer return type   
    public int CompareNum(int x, int y)   
    {   
        System.out.println("x = " + x + "\ny = " + y);  
        if(x>y)  
            return x;  
        else  
            return y;  
    }  
      
      
    public static void main(String ar[])   
    {  
        Compare obj = new Compare(); // object creation  
        int result = obj.CompareNum(15,24); //storing the result in a variable  
        System.out.println("The greater number among x and y is: " + result);  
    }  
}
                        

In the above example we have created a method CompareNum() which takes two parameters , the method returns the greatest number.

Contact Us

REACH US

SERVICES

  • CODING
  • ON-LINE PREPARATION
  • JAVA & PYTHON

ADDRESS

B-54, Krishna Bhawan, Parag Narain Road, Near Butler Palace Colony Lucknow
Contact:+ 919839520987
Email:info@alexsir.com