Today is going to be the warmest day of the year so far. Forecasts are calling for a high in the mid to upper 60’s. Hopefully the warmer weather will help the remaining snow melt. I would guess that most people in this part of the country had enough snow for the year.
This morning I took a look at the Can You Access? HackerRank problem. The requirements are quite simple. You need to call a method with a parameter in a class that is two levels deep.
The test case illustrates the behavior of the function which is provided in the problem. We just need to call it.
8 8 is power of 2 An instance of class: Solution.Inner.Private has been created
If we use an odd number the results would be something like:
7 7 is not a power of 2 An instance of class: Solution.Inner.Private has been created
Let’s take a look at the provided code which you have to complete:
import java.io.*; import java.lang.reflect.*; import java.util.*; import java.util.regex.*; import java.security.*; public class Solution { public static void main(String[] args) throws Exception { DoNotTerminate.forbidExit(); try{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int num = Integer.parseInt(br.readLine().trim()); Object o;// Must be used to hold the reference of the instance of the class Solution.Inner.Private //Write your code here System.out.println("An instance of class: " + o.getClass().getCanonicalName() + " has been created"); }//end of try catch (DoNotTerminate.ExitTrappedException e) { System.out.println("Unsuccessful Termination!!"); } }//end of main static class Inner{ private class Private{ private String powerof2(int num){ return ((num&num-1)==0)?"power of 2":"not a power of 2"; } } }//end of Inner }//end of Solution class DoNotTerminate { //This class prevents exit(0) public static class ExitTrappedException extends SecurityException { private static final long serialVersionUID = 1L; } public static void forbidExit() { final SecurityManager securityManager = new SecurityManager() { @Override public void checkPermission(Permission permission) { if (permission.getName().contains("exitVM")) { throw new ExitTrappedException(); } } }; System.setSecurityManager(securityManager); } }
The DoNotTerminate class does not appear to have any association with the requirements. Not sure why it was included. Perhaps it was added to make things harder to follow.
The Solution class contains the static Inner class. The Inner class contains a class named Private. This class seems to have been named Private to confuse the reader with the private attribute.
What we need to do is call a method with an argument named Solutions.Inner.Private.powerof2() with the num argument. The method should return a string which we need to display. To make it more complicated we need to prep end the string “is” to make it match what is expected. And yes, the powerof2() method could have return a string starting with “is”.
Before you dive into the solution, you might want to read about Java Nested Classes here. The article is good at describing the topic at hand.
Following is my Java solution written using the Eclipse IDE.
import java.io.*; import java.lang.reflect.*; import java.util.*; import java.util.regex.*; import java.security.*; public class Solution { public static void main(String[] args) throws Exception { // // ???? ???? // DoNotTerminate.forbidExit(); try { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int num = Integer.parseInt(br.readLine().trim()); Object o;// Must be used to hold the reference of the instance of the class Solution.Inner.Private // ???? ???? o = new Inner(); String str = ((Solution.Inner)o).methodInInner(); System.out.println("str ==>" + str + "<==\n"); // **** instantiate an object of Solution.Inner.Private class **** o = new Inner().new Private(); // ???? ???? str = ((Solution.Inner.Private)o).powerof2(num); System.out.println("str ==>" + str + "<==\n"); // **** cast to access the classes **** Solution.Inner.Private forEaseOfUse = (Solution.Inner.Private)o; // **** invoke the method (returns a string) **** String answer = forEaseOfUse.powerof2(num); // **** format the output as required **** System.out.println(num + " is " + answer); // **** **** System.out.println("An instance of class: " + o.getClass().getCanonicalName() + " has been created"); }//end of try // // ???? ???? // catch (DoNotTerminate.ExitTrappedException e) { // System.out.println("Unsuccessful Termination!!"); // } // ???? ???? catch (Exception e) { System.out.println(e.getMessage()); } }//end of main /* * Inner class */ static class Inner { /* * the class is called Private * it could have been called Public or InnerInner */ private class Private { /* * method in Private class * method in Solution.Inner.Private class */ private String powerof2(int num){ return ((num & num - 1) == 0) ? "power of 2" : "not a power of 2"; } } /* * method in Inner class * method in Solution.Inner class */ private String methodInInner() { return "method in Inner class"; } }//end of Inner }//end of Solution ///* // * // */ //class DoNotTerminate { //This class prevents exit(0) // // public static class ExitTrappedException extends SecurityException { // // private static final long serialVersionUID = 1L; // } // // public static void forbidExit() { // final SecurityManager securityManager = new SecurityManager() { // @Override // public void checkPermission(Permission permission) { // if (permission.getName().contains("exitVM")) { // throw new ExitTrappedException(); // } // } // }; // System.setSecurityManager(securityManager); // } //}
Note that I have commented out the DoNotTerminate class and the reference to the method forbidExit(). I also replaced the catch statement so the code would compile.
As usual, I added some code that is not part of the solution. Such code is preceded by comments with the string “// ???? ????” which you can comment or uncomment to get to the solution accepted by HackerRank.
All is based on an object instantiation and a cast to get to the method in question. Try different objects with and without cast and see how the powerof2() method is displayed in the IDE.
In interested, my solution is in my GitHub repository.
If you have comments or questions regarding this or any other post in this blog, or if you need help with a software development project, please leave me a note bellow. Requests for help will not be made public.
Keep on reading and experimenting. It is the best way of learning.
John
Follow me on Twitter: @john_canessa