NPTEL » Programming In Java Week 3 : Programming Assignment 4 Aug 2022

 

Week 3 : Programming Assignment 4

Due on 2022-08-18, 23:59 IST

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.  


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 Test3{

    public static void main( String[] args ) {

        Scanner sc = new Scanner(System.in);

int n1=sc.nextInt();

int n2=sc.nextInt();


        //Called the method sum() to find the sum of two numbers.

        //Called the method multiply() to find the product of two numbers.


        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);

    }

}












No comments

NPTEL » Programming in Java Week 07 : Programming Assignment 1 2 3 4 and 5

  Week 07 : Programming Assignment 1 Due on 2024-09-12, 23:59 IST Write a Java program to find longest word in given input. Select the Langu...