forked from int28h/JavaTasks
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path19_Interfaces.java
More file actions
42 lines (37 loc) · 1.27 KB
/
19_Interfaces.java
File metadata and controls
42 lines (37 loc) · 1.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
/**
* The AdvancedArithmetic interface and the method declaration for the abstract int divisorSum(int n) method
* are provided for you in the editor below. Write the Calculator class, which implements the AdvancedArithmetic interface.
* The implementation for the divisorSum method must be public and take an integer parameter, n, and return
* the sum of all its divisors.
*/
import java.io.*;
import java.util.*;
interface AdvancedArithmetic{
int divisorSum(int n);
}
class Calculator implements AdvancedArithmetic {
public int divisorSum(int n) {
List<Integer> divisors = new LinkedList<>();
for(int i = n; i > 0; i--){
if(n % i == 0){
divisors.add(i);
}
}
int result = 0;
for(Integer entry : divisors){
result += entry;
}
return result;
}
}
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);
}
}