Can You Access?

Sort by

recency

|

137 Discussions

|

  • + 0 comments

    I came to the solution by adding the following lines to use Java reflection and access the private powerof2 method in Solution.Inner.Private:

    Solution.Inner inner = new Solution.Inner();
    Solution.Inner.Private innerPrivate = inner.new Private();
    o = innerPrivate;
    Method method = innerPrivate.getClass().getDeclaredMethod("powerof2", int.class);
    method.setAccessible(true); 
    String result = (String) method.invoke(o, num); 
    System.out.println(num + " is " + result);
    

    `

  • + 0 comments
    o = new Inner().new Private();
    String powerOf2 = ((Inner.Private)o).powerof2(num);
    System.out.println(num + " is " + powerOf2);
    
  • + 0 comments

    The task description is very vague. Only by reading Editorial section I learned that we are supposed to use reflection to call the method of inner class.

  • + 0 comments

    For JAVA 8

    Inner x = new Inner();
      o = x.new Private();
        Inner.Private y = (Inner.Private) o;
    System.out.println(num+" is "+ y.powerof2(num)); 
    

    Just write it to the area that is shown us.

  • + 0 comments

    My solution:

    public class Solution {
    
        public static void main(String[] args) {
            /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
            Scanner sc = new Scanner(System.in);
            int num = sc.nextInt();
            
            Inner i = new Inner();
            Inner.Private ip = i.new Private();
            String assume = ip.powerof2(num) ? "is" : "is not a";
            
            System.out.printf("%d %s power of 2\n", num, assume);
            System.out.println("An instance of class: Solution.Inner.Private has been created");
        }
    }
    
    class Inner{
        class Private{
            boolean powerof2(int num){
                
                while(num > 1){
                    if(num % 2 != 0) return false; //n = 2k+1
                    num /= 2;
                }
                return true;
            }
        }
    }