5
\$\begingroup\$

Given an integer, check if it's the power of two.

class Main {
  static class Solution {
    private final boolean answer;

    Solution(int num) {
      if (num <= 0) {
        answer = false;
      }
      else if ((num & (num - 1)) == 0) {
        answer = true;
      }
      else {
        answer = false;
      }
    }

    public boolean isPowerOfTwo() {
      return answer;
    }
  }

  public static void runTests() {
    // Falsy
    System.out.println(new Solution(-1).isPowerOfTwo());
    System.out.println(new Solution(0).isPowerOfTwo());
    System.out.println(new Solution(Integer.MAX_VALUE).isPowerOfTwo());
    System.out.println(new Solution(Integer.MIN_VALUE).isPowerOfTwo());

    //Truthy
    System.out.println(new Solution(1).isPowerOfTwo());
    System.out.println(new Solution(128).isPowerOfTwo());
    System.out.println(new Solution(1024).isPowerOfTwo());
  }

  public static void main(String[] args) {
    runTests();
  }
}
\$\endgroup\$

1 Answer 1

11
\$\begingroup\$

Code organization

There is nothing in the description and the posted code to justify storing the result of the "is power of 2" calculation in a class. It would be better to simply return it. The code is also simple enough that you could collapse the if-else chain to a single line:

public static boolean isPowerOfTwo(int num) {
    return num > 0 && (num & (num - 1)) == 0;
}

Testing

I suggest to look into proper unit testing methods, for example JUnit4 which comes bundled in most IDEs. The tests you wrote in main would look like this using JUnit4:

private boolean isPowerOfTwo(int num) {
    return Solution.isPowerOfTwo(num);
}

@Test
public void minus1_should_not_be_power_of_2() {
    assertFalse(isPowerOfTwo(-1));
}

@Test
public void zero_should_not_be_power_of_2() {
    assertFalse(isPowerOfTwo(0));
}

// ... and so on
\$\endgroup\$
5
  • \$\begingroup\$ Most of the questions I post here is under the assumption that there is a interview going on and I am doing some online coding hence I cannot have the testing framework, having said that I was just experimenting with creating a class for each algorithm something which I read in the Sedgwick's book. \$\endgroup\$
    – CodeYogi
    Commented Dec 25, 2016 at 6:46
  • \$\begingroup\$ You can write proper unit tests on a whiteboard just as you can write a main method with tests. You cannot run either of them. But you will get plus points for the good habit of proper unit testing. \$\endgroup\$
    – janos
    Commented Dec 25, 2016 at 6:49
  • \$\begingroup\$ I typically use online editors such as https://repl.it/languages/java and I don't think I can run tests on them. I really believe in testing but what to do when environment doesn't support it? \$\endgroup\$
    – CodeYogi
    Commented Dec 25, 2016 at 6:55
  • 1
    \$\begingroup\$ In environments that don't support it, you could imitate it. You could for example implement simple assert* methods that will throw an exception on failure. This is theoretically speaking. In an online editor as you mentioned, indeed I wouldn't bother. You should have mentioned this in your question and I wouldn't have given a different comment. A simple way to do that is to add a comment in the main method right above your printing statements, for example // I would write these as proper unit tests, if I wasn't limited by the online editor \$\endgroup\$
    – janos
    Commented Dec 25, 2016 at 7:03
  • 1
    \$\begingroup\$ Makes sense, I will now remember to mention in the question. \$\endgroup\$
    – CodeYogi
    Commented Dec 25, 2016 at 7:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.