<\/span><\/h2>\n\n\n\nYou are required to compute the power of a number by implementing a calculator. Create a class MyCalculator which consists of a single method long power(int, int). This method takes two integers, n <\/strong>and p<\/strong>, as parameters and finds np<\/sup><\/strong>. If either n <\/strong>or p <\/strong>is negative, then the method must throw an exception which says “ n or p should not be negative <\/strong>“. Also, if both n <\/strong>and p <\/strong>are zero, then the method must throw an exception which says “n or p should not be zero .<\/strong>“
For example, -4 and -5 would result in java.lang.Exception : n or p should not be negative<\/strong>.<\/p>\n\n\n\nComplete the function power in class MyCalculator and return the appropriate result after the power operation or an appropriate exception as detailed above.<\/p>\n\n\n\n
<\/span>Input Format<\/strong><\/span><\/h2>\n\n\n\nEach line of the input contains two integers, n <\/strong>and p<\/strong>. The locked stub code in the editor reads the input and sends the values to the method as parameters.<\/p>\n\n\n\n<\/span>Constraint<\/strong><\/span><\/h4>\n\n\n\n- -10 <= n <= 10<\/strong><\/li>
- -10 <= p <= 10<\/strong><\/li><\/ul>\n\n\n\n
<\/span>Output Format<\/strong><\/span><\/h2>\n\n\n\nEach line of the output contains the result np<\/sup><\/strong>, if both n <\/strong>and p <\/strong>are positive. If either n <\/strong>or p <\/strong>is negative, the output contains “n and p should be non-negative”. If both n <\/strong>and p <\/strong>are zero, the output contains “n and p should not be zero.”. This is printed by the locked stub code in the editor.<\/p>\n\n\n\n<\/span>Sample Input<\/strong><\/span><\/h4>\n\n\n\n3 5\n2 4\n0 0\n-1 -2\n-1 3<\/code><\/pre>\n\n\n\n<\/span>Sample Output<\/strong><\/span><\/h4>\n\n\n\n 243\n 16\n java.lang.Exception: n and p should not be zero.\n java.lang.Exception: n or p should not be negative.\n java.lang.Exception: n or p should not be negative.<\/code><\/pre>\n\n\n\n<\/span>Explanation<\/strong><\/span><\/h4>\n\n\n\n- In the first two cases, both n <\/strong>and p <\/strong>are postive. So, the power function returns the answer correctly.<\/li>
- In the third case, both n <\/strong>and p <\/strong>are zero. So, the exception, “n and p should not be zero.”, is printed.<\/li>
- In the last two cases, at least one out of n <\/strong>and p <\/strong>is negative. So, the exception, “n or p should not be negative.”, is printed for these two cases.<\/li><\/ul>\n\n\n\n
<\/span>Solution – Java Exception Handling<\/strong><\/span><\/h2>\n\n\n\nimport java.util.Scanner;\nimport java.math.*;\nclass MyCalculator {\n \/*\n * Create the method long power(int, int) here.\n *\/\n \n public static long power(int a,int b) throws Exception{\n long la = a;\n long lb = b;\n long c = (long)Math.pow(la,lb);\n if(la==0 && lb==0) {\n throw new Exception(\"n and p should not be zero.\");\n\n }\n else if(la<0 || lb <0) {\n throw new Exception(\"n or p should not be negative.\");\n \n }\n else\n return c;\n \n }\n \n \n}\n\npublic class Solution {\n public static final MyCalculator my_calculator = new MyCalculator();\n public static final Scanner in = new Scanner(System.in);\n \n public static void main(String[] args) {\n while (in .hasNextInt()) {\n int n = in .nextInt();\n int p = in .nextInt();\n \n try {\n System.out.println(my_calculator.power(n, p));\n } catch (Exception e) {\n System.out.println(e);\n }\n }\n }\n}<\/pre>\n\n\n\n