Sunday, August 13, 2023

This program to exercise the call of static and non-static methods. A partial code is given defining two methods, namely sum( ) and multiply ( ). You have to call these methods to find the sum and product of two numbers. Complete the code segment as instructed.


 

This program to exercise the call of static and non-static methods. A partial code is given defining two methods, namely sum( ) and multiply ( ). You have to call these methods to find the sum and product of two numbers. Complete the code segment as instructed.



Program :

import java.util.Scanner;

class QuestionScope {

int sum(int a, int b){ //non-static method

        return a + b;

    }

static int multiply(int a, int b){ //static method

        return a * b;

    }

}

public class Exercise3_4{

public static void main( String[] args ) {

        Scanner sc = new Scanner(System.in);

int n1=sc.nextInt();

int n2=sc.nextInt();

QuestionScope st = new QuestionScope(); // Create an object to call non-  

                                                //static method 

      int result1=st.sum(n1,n2); // Call the method

      int result2=QuestionScope.multiply(n1,n2); // Create an object to call 

                                                //static method 

System.out.println(result1);

System.out.print(result2);

}

}



InputOutput
Test Case 1
3 5
8
15

No comments:

Post a Comment