Interface Implementation – Calculator

Interface Implementation – Calculator

The interface is given below. Define the class so that it implements the interface Calculator and the program prints the desired output.
public interface Calculator {
   int add(int x, int y);   
   int subtract(int x, int y);
   int multiply(int x, int y);
   int divide(int x, int y);

The invoking class with the main method is given below.
import java.util.*;
public class Hello {
    public static void main(String[] args) {
        Scanner sc = new Scanner(;
        int x = sc.nextInt();
        int y = sc.nextInt();
        Calculator calc = getCalculator();
        System.out.println(calc.add(x, y));
        System.out.println(calc.subtract(x, y));
        System.out.println(calc.multiply(x, y));
        System.out.println(calc.divide(x, y));
    private static Calculator getCalculator() {
        return new SimpleCalculator();
Input Format:
The first line contains x.
The second line contains y.
Output Format:
The first line contains the sum of x and y.
The second line contains the difference between x and y.
The third line contains the product of x and y.
The fourth line contains the quotient when x is divided by y.
Boundary Conditions:
1 <= N1 <= 9999
1 <= N2 <= 9999

Example Input/Output 1:


public class SimpleCalculator implements Calculator {
    public int add(int x,int y)
        return x+y;
    public int  subtract(int x,int y)
        return x-y;
public int multiply(int x,int y)
    return x*y;
public int divide(int x,int y)
    return x/y;

Add a Comment

Your email address will not be published. Required fields are marked *