NPTEL Programming in Java Week 5 Programming Assignment 1 Answer

An interface Number is defined in the following program.  You have to declare a class A, which will implement the interface Number. Note that the method findSqr(n) will return the square of the number n.

Select the Language for this assignment. 
File name for this program : 
1
import java.util.Scanner;
2
3
interface Number {
4
    int findSqr(int i);  // Returns the square of n
5
}
6
//Create a class A which implements the interface Number.
7
class A implements Number
8
   {
9
     public int findSqr(int i) {
10
     return i*i;
11
      }
12
  }
0
public class Question5_1{ 
1
        public static void main (String[] args){ 
2
          A a = new A();   //Create an object of class A
3
           // Read a number from the keyboard
4
           Scanner sc = new Scanner(System.in);  
5
           int i = sc.nextInt();
6
           System.out.print(a.findSqr(i)); 
7
    } 
8
}
Post a Comment (0)
Previous Question Next Question

You might like