Interface Implementation - Calculator
The interface Calculator.java is given below. Define the class SimpleCalculator.java 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(System.in); 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.
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.
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
1 <= N1 <= 9999
1 <= N2 <= 9999
Example Input/Output 1:
Input:5
2
Output:7
3
10
2
3
10
2
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;
}
}
No comments:
Post a Comment