Algorithm
Problem Name:
In this HackerRank Functions in Java programming problem solution,
You 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 and p , as parameters and finds n**p If either n or p is negative, then the method must throw an exception which says "
n or p should not be negative". Also, if both n and b are zero, then the method must throw an exception which says "n or p should not be negative."
For example, -4 and -5 would result in
java.lang.Exception: n or p should not be negative
Complete the function power
in class MyCalculator and return the appropriate result after the power operation or an appropriate exception as detailed above.
Input Format
Each line of the input contains two integers, n and p The locked stub code in the editor reads the input and sends the values to the method as parameters.
Constraints
- -10 <= n <= 10
- -10 <= p <= 10
Output Format
Sample Input 0
3 5
2 4
0 0
-1 -2
-1 3
Sample Output 0
243
16
java.lang.Exception: n and p should not be zero.
java.lang.Exception: n or p should not be negative.
java.lang.Exception: n or p should not be negative.
Explanation 0
- In the first two cases, both n and p are postive. So, the power function returns the answer correctly.
- In the third case, both n and p are zero. So, the exception, "n and p should not be zero.", is printed.
- In the last two cases, at least one out of n and p is negative. So, the exception, "n or p should not be negative.", is printed for these two cases.
Code Examples
#1 Code Example with Java Programming
Code -
Java Programming
import java.util.*;
import java.util.Scanner;
class MyCalculator {
long power(int n, int p) throws Exception {
if (n < 0 || p < 0) {
throw new Exception("n or p should not be negative.");
} else if (n == 0 && p == 0) {
throw new Exception("n and p should not be zero.");
} else {
return (long) Math.pow(n, p);
}
}
}
class Solution {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while (in .hasNextInt()) {
int n = in .nextInt();
int p = in .nextInt();
MyCalculator my_calculator = new MyCalculator();
try {
System.out.println(my_calculator.power(n, p));
} catch (Exception e) {
System.out.println(e);
}
}
}
}
Copy The Code &
Try With Live Editor