Problem : Write a HackerRank Solution For Day 19 : Interfaces or Hacker Rank Solution Program In Python and java For ” Day 19 : Interfaces ” or Hacker Rank 30 days of code in Python and Java Solution: Day 19 : Interfaces or Hackerrank solution for 30 Days of Code Challenges or Hacker rank 30 days of code in Python and Java Solution, Day 19 : Interfaces , or Python and java Logic & Problem Solving: Day 19 : Interfaces

Hackerrank Day 19 problem talk’s about one of the best features of Java and also the mostly used in live projects. Interface is like a service requirement specification for client. It tell us him what services are provided thereby without exposing the implementation to him. The main advantage of Interface is security as implementation is hidden from the client and also easy enhancement of code is possible.

Lets check out how the magic happens : Day 19

Objective
Today, we’re learning about Interfaces. Check out the Tutorial tab for learning materials and an instructional video!

Task
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 .

Input Format

A single line containing an integer, .

Constraints

Output Format

You are not responsible for printing anything to stdout. The locked template code in the editor below will call your code and print the necessary output.

Sample Input

6

Sample Output

I implemented: AdvancedArithmetic
12

Solution Of the Question is :

More Hacker Rank Solutions : Hacker Rank solutions 30 day challenge

Get latest FREE projects : IT , Engineering projects for FREE

import java.io.*;
import java.util.*;

interface AdvancedArithmetic{
   int divisorSum(int n);
}


//Write your code here
class Calculator implements AdvancedArithmetic
{

@Override 
public int divisorSum(int n)
{
int sum = n;
for(int x=1;x<=n/2;x++)
{

if(n%x==0)
{
sum = sum + x;
}

}
return sum;
}

}

class Solution {

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int n = scan.nextInt();
        scan.close();
        
      	AdvancedArithmetic myCalculator = new Calculator(); 
        int sum = myCalculator.divisorSum(n);
        System.out.println("I implemented: " + myCalculator.getClass().getInterfaces()[0].getName() );
        System.out.println(sum);
    }
}

Result :

Hackerrank 30 days of code Solution: Day 19: Interfaces

Solution in Python

from abc import ABCMeta, abstractmethod

class AdvancedArithmetic(object, metaclass=ABCMeta):
    @abstractmethod
    def divisorSum(self, n):
        pass

class Calculator(AdvancedArithmetic):
    def divisorSum(self, n):
        result = 0
        for i in range(n):
            if n % i == 0:
                result += i
        return sum

n = int(input())

myCalculator = Calculator()
total = myCalculator.divisorSum(n)
print("I implemented: " + str(AdvancedArithmetic))
print(total)

Leave a Reply

Your email address will not be published.