- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHackerrankJava0056.java
More file actions
Latest commit
47 lines (36 loc) · 1.43 KB
/
Copy pathHackerrankJava0056.java
File metadata and controls
47 lines (36 loc) · 1.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
importjava.util.Scanner;
publicclassHackerrankJava0056 {
publicstaticvoidmain(String[] args) {
// Java Exception Handling
// https://www.hackerrank.com/challenges/java-exception-handling/problem?isFullScreen=true
MyCalculator0056my_calculator = newMyCalculator0056();
Scannerin = newScanner(System.in);
while (in.hasNextInt()) {
intn = in.nextInt();
intp = in.nextInt();
try {
System.out.println(my_calculator.power(n, p));
} catch (Exceptione) {
System.out.println(e);
}
}
in.close();
}
}
classMyCalculator0056 {
// -------------------------------------------------------------------------------------------------
// --- You need to add this lines for solution hackerrank provides all other lines for challange ---
// -------------------------------------------------------------------------------------------------
publicintpower(intn, intp) throwsException {
if (n < 0 || p < 0) {
thrownewException("n or p should not be negative.");
} elseif (n == 0 && p == 0) {
thrownewException("n and p should not be zero.");
} else {
return (int) Math.pow(n, p);
}
}
// -------------------------------------------------------------------------------------------------
// -------------------------------------------------------------------------------------------------
// -------------------------------------------------------------------------------------------------
}