You are given an interface which contains an abstract method and a default method void show()to simply output a statement "This is a default method's output". You need to write a class called MyCalculator which implements the interface. While you will override divisorSum method in MyCalculator class, you need to consider that method just takes an integer as input and return the sum of all its divisors. For example divisors of 6 are 1, 2, 3 and 6, so divisorSum should return 12. The value of n will be at most 1000. You have to create a main class ( any name can be given to main class and in the main class you have to create an object of MyCalculator class using a interface variable of AdvancedArithmetic interface. Then from the main method call the divisorSum method inside a println method using the created object. Then also call the show method using the created object.
public class Main {
public static void main(String[] args) {
MyCalculator calc = new MyCalculator();
calc.show();
System.out.println(calc.divisorSum(26));
}
}
interface AdvancedArithmetic{
abstract void show();
}
class MyCalculator implements AdvancedArithmetic {
@Override
public void show() {
System.out.println("This is a default method's output");
}
public int divisorSum(int num){
int sum = 0;
for(int i = 1; i < num; i++){
if(0 == (num % i)){
sum += i;
}
}
return sum;
}
}
Comments
Leave a comment