Class 10 ICSE - Java Methods 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.
Program 1: Write a program using method to subtract two numbers and print the result for the same.
Solution:
class Difference {

    // create a method
    public int subtract(int a, int b) {
      int diff = a - b;
      // return value
      return diff;
    }
  
    public static void main(String[] args) {
      
      int num1 = 25;
      int num2 = 15;
  
      // create an object of class
      Difference obj = new Difference();
      // calling method
      int result = obj.subtract(num1, num2);
      System.out.println("Difference is: " + result);
    }
  }
                        

In the above example, we have created a method named subtract(). The method takes two parameters a and b.
int result = obj.subtract(num1, num2);
Here, we have called the method by passing two arguments num1 and num2. Since the method is returning some value, we have stored the value in the result variable and displaying the difference on terminal

Method Parameters




Program 2: Write a program using method. The method take value as age and print the message shown below :

age greater than or equal to 18 - "You are elegible for voting".
age less than 18 - "You are not elegible for voting".
Solution:
class Elegibility {

    // Create a method
    static void checkAge(int age) {
  
      // If age is less than 18, print "You are not elegible for voting"
      if (age < 18) {
        System.out.println("You are not elegible for voting");
  
      // If age is greater than, or equal to, 18, print "You are elegible for voting"
      } else {
        System.out.println("You are elegible for voting");
      }
  
    }
  
    public static void main(String[] args) {
      checkAge(20); // Call the checkAge method and pass age 20
    }
  }
                        

In the above example, we have created a method named checkAge(). The method takes a parameter age.
checkAge(20);
Here, we have called the method by passing argument 20. "age" is formal parameter and value "20" is the actual parameter. The method is not returning any value.
Program 3: 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.
int myResult = findSquare(num);
Here, we have called the method by passing argument num. "n" is formal parameter and "num" is the actual parameter. Since the method is returning a value we have stored the result in a variable.

Invoking Methods




Program 4: Write a program to display two messages using invoking methods , one with no parameter and other with a parameter.
Solution:
class Main 
{

  // method with no parameter
  public void display1() {
    System.out.println("Method with no parameter");
  }

  // method with single parameter
  public void display2(int n) {
    System.out.println("Method with a parameter: " + n);
  }

  public static void main(String[] args) {
    
    // create an object
    Main obj = new Main();

    // calling method
    obj.display1();
    
    // calling method
    obj.display2(24);
  }
}
                        

In the above example, we have created two methods display1() with no parameter and display2() with single parameter. We are creating object of the class and then calling both the methods using (.) dot keyword.

Returning from Methods




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 and prints the message along with the 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