Java Day 19: Interfaces




The AdvancedArithmetic interface and the method declaration for the abstract divisorSum(n) method are provided for you in the editor below.

Complete the implementation of Calculator class, which implements the AdvancedArithmetic interface. The implementation for the divisorSum(n) method must return the sum of all divisors of n.

-HackerRank solution



  1. import java.io.*;
  2. import java.util.*;
  3. interface AdvancedArithmetic{
  4. int divisorSum(int n);
  5. }
  6. class Calculator implements AdvancedArithmetic {
  7. @Override
  8. public int divisorSum(int n) {
  9. int sum = 0;
  10. for (int i = 1; i <= n; i++) {
  11. if (n % i == 0) sum += i;
  12. }
  13. return sum;
  14. }
  15. }
  16. class Solution {
  17. public static void main(String[] args) {
  18. Scanner scan = new Scanner(System.in);
  19. int n = scan.nextInt();
  20. scan.close();
  21. AdvancedArithmetic myCalculator = new Calculator();
  22. int sum = myCalculator.divisorSum(n);
  23. System.out.println("I implemented: " + myCalculator.getClass().getInterfaces()[0].getName() );
  24. System.out.println(sum);
  25. }
  26. }
Please click on the like button if it worked

Solution not working or have any suggestions? Please send an email to [email protected]


donate a cup of tea :)


Join Our Facebook Group

Share this solution


download android app

Popular HackerRank Java Solutions








codesadda.com

Codesadda.com is your home of programming solutions, tutorials, video tutorials and much more. Sign Up for our weekly newsletter to get update about new content.

Like us on Facebook | Connect with us on LinkedIn | Subscribe our Channel on Youtube