- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInterface.java
More file actions
Latest commit
43 lines (42 loc) · 1.29 KB
/
Copy pathInterface.java
File metadata and controls
43 lines (42 loc) · 1.29 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
43
importjava.util.*;
interfaceAdvancedArithmetic{
intdivisor_sum(intn);
}
//Write your code here
classMyCalculatorimplementsAdvancedArithmetic//class implementing the interface..
{
publicintdivisor_sum(intn)
{
inti;
intsum=0;
for(i=1;i<=n;i++) //calculating the divisors of a particular number....
{
if(n%i==0)
{
sum = sum+i; //calculating the sum of the divisors....
}
}
returnsum;
}
}
classSolution{
publicstaticvoidmain(String []args){
MyCalculatormy_calculator = newMyCalculator();
System.out.print("I implemented: ");
ImplementedInterfaceNames(my_calculator);
Scannersc = newScanner(System.in);
intn = 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
*/
staticvoidImplementedInterfaceNames(Objecto){
Class[] theInterfaces = o.getClass().getInterfaces();
for (inti = 0; i < theInterfaces.length; i++){
StringinterfaceName = theInterfaces[i].getName();
System.out.println(interfaceName);
}
}
}