We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
importjava.util.*;interfaceAdvancedArithmetic{intdivisor_sum(intn);}//Write your code hereclassMyCalculatorimplementsAdvancedArithmetic{@Overridepublicintdivisor_sum(intn){intsum=0;for(inti=1;i<=n;i++){if(n%i==0){sum+=i;}}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);}}}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Java Interface
You are viewing a single comment's thread. Return to all comments →