In this HackerRank Java Interface problem in a java programming language, You are given an interface AdvancedArithmetic which contains a method signature int divisor_sum(int n). You need to write a class called MyCalculator which implements the interface.
HackerRank Java Interface problem solution.
import java.util.*;
interface AdvancedArithmetic{
int divisor_sum(int n);
}
//Write your code here
class MyCalculator implements AdvancedArithmetic {
public int divisor_sum(int n) {
if (n <= 1) { return n; }
int res = n + 1;
for (int i = 2; i < n; i++) {
if (n % i == 0) {
res += i;
}
}
return res;
}
}
class Solution{
public static void main(String []args){
MyCalculator my_calculator = new MyCalculator();
System.out.print("I implemented: ");
ImplementedInterfaceNames(my_calculator);
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
System.out.print(my_calculator.divisor_sum(n) + "n");
sc.close();
}
/*
* ImplementedInterfaceNames method takes an object and prints the name of the interfaces it implemented
*/
static void ImplementedInterfaceNames(Object o){
Class[] theInterfaces = o.getClass().getInterfaces();
for (int i = 0; i < theInterfaces.length; i++){
String interfaceName = theInterfaces[i].getName();
System.out.println(interfaceName);
}
}
}
Second solution
import java.util.*;
interface AdvancedArithmetic{
int divisor_sum(int n);
}
class MyCalculator implements AdvancedArithmetic{
int sum = 0;
public int divisor_sum(int n){
for(int i=1;i<=(n+1)/2;i++){
if(n%i==0){
sum+=i;
}
}
if(n==1){
return 1;
}
return (sum+n);
}
}
class Solution{
public static void main(String []args){
MyCalculator my_calculator = new MyCalculator();
System.out.print("I implemented: ");
ImplementedInterfaceNames(my_calculator);
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
System.out.print(my_calculator.divisor_sum(n) + "n");
sc.close();
}
/*
* ImplementedInterfaceNames method takes an object and prints the name of the interfaces it implemented
*/
static void ImplementedInterfaceNames(Object o){
Class[] theInterfaces = o.getClass().getInterfaces();
for (int i = 0; i < theInterfaces.length; i++){
String interfaceName = theInterfaces[i].getName();
System.out.println(interfaceName);
}
}
}
Third solution
import java.util.*;
interface AdvancedArithmetic {
public abstract int divisorSum(int n);
}
//Solution starts
class MyCalculator implements AdvancedArithmetic {
public int divisor_sum(int n) {
int ans = 0;
for (int i = 1; i <= n; i++) {
if (n % i == 0) ans += i;
}
return ans;
}
}
//Solution ends
class Solution {
public static void main(String[] argh) {
MyCalculator my_calculator = new MyCalculator();
System.out.print("I implemented: ");
ImplementedInterfaceNames(my_calculator);
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
System.out.print(my_calculator.divisorSum(n) + "n");
}
/*
* ImplementedInterfaceNames method takes an object and prints the name of the interfaces it implemented
*/
static void ImplementedInterfaceNames(Object o) {
Class[] theInterfaces = o.getClass().getInterfaces();
for (int i = 0; i < theInterfaces.length; i++) {
String interfaceName = theInterfaces[i].getName();
System.out.println(interfaceName);
}
}
}