Cookies Consent

This website uses cookies to ensure you get the best experience on our website.

Learn More

The Adder Class - Java (Basic) certification Solution | HackerRank

0 min read

Note: This solution is only for reference purpose. Feel free to use this solution as inspiration and enhance your knowledge but please don't literally copy and paste the code.


Solution of the problem:



 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import java.util.Scanner;

abstract class Calculator {
	abstract int add(int a, int b);
}

class Adder extends Calculator{
	int add(int a, int b) {
		return a+b;
	}	
}
public class Solution {
	public static void main(String[] args) {
		int a, b;
		try (Scanner scan = new Scanner(System.in)) {
			a = scan.nextInt();
			b = scan.nextInt();
		}

		Calculator adderObject = new Adder();
		System.out.println("The sum is: " + adderObject.add(a, b));
	}
}



Labels : #hackerrank certification ,#java (basic) ,

Post a Comment