Answer to Question #319821 in Java | JSP | JSF for shahriar

Question #319821

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.


1
Expert's answer
2022-03-28T17:44:17-0400
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;
    }

}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS