Click here to Skip to main content
15,903,388 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Java
import java.util.*;
public class TestClass {
  public static String interfaceName;
	 public static void main(String[] args) { 
		
        MyCalculator my_calculator=new MyCalculator();
        Scanner nw=new Scanner(System.in);
        int no=nw.nextInt();

        System.out.println("I implemented "+interfaceName);
        System.out.print(my_calculator.divisor_sum(no));

    }

    static void ImplementedInterfaceNames(Object o){
        Class[] theInterfaces = o.getClass().getInterfaces();
        for (int i = 0; i < theInterfaces.length; i++){
            interfaceName = theInterfaces[i].getName();}
    }
}

interface AdvancedArithmatic{
    int divisor_sum(int n);
}

class MyCalculator implements AdvancedArithmatic{

    @Override
    public int divisor_sum(int n) {
        int mySum=0;
        for(int i=1;i<=n;i++){
            if(n%i==0) mySum+=i;
        }
        return mySum;
    }
}


What I have tried:

Java
import java.util.*;
public class TestClass {
  public static String interfaceName;
	 public static void main(String[] args) { 
		
        MyCalculator my_calculator=new MyCalculator();
        Scanner nw=new Scanner(System.in);
        int no=nw.nextInt();

        System.out.println("I implemented "+interfaceName);
        System.out.print(my_calculator.divisor_sum(no));

    }

    static void ImplementedInterfaceNames(Object o){
        Class[] theInterfaces = o.getClass().getInterfaces();
        for (int i = 0; i < theInterfaces.length; i++){
            interfaceName = theInterfaces[i].getName();}
    }
}

interface AdvancedArithmatic{
    int divisor_sum(int n);
}

class MyCalculator implements AdvancedArithmatic{

    @Override
    public int divisor_sum(int n) {
        int mySum=0;
        for(int i=1;i<=n;i++){
            if(n%i==0) mySum+=i;
        }
        return mySum;
    }
}
Posted
Updated 29-Jan-18 8:03am

1 solution

It shows null because interfaceName never gets a value: it would get a value of ImplementedInterfaceNames would get called, but it never gets called.

Also, instead of using a static variable for a method to give its output values, it's better practice to use a return statement in your method: Returning a Value from a Method (The Java™ Tutorials > Learning the Java Language > Classes and Objects)[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900