-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExceptionCreation.java
More file actions
70 lines (55 loc) · 1015 Bytes
/
ExceptionCreation.java
File metadata and controls
70 lines (55 loc) · 1015 Bytes
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import java.util.*;
import java.io.*;
class negativeException extends Exception
{
public String getMessage()
{
return "n and p should be non-negative";
}
}
class Calculator
{
int power(int n,int p) throws negativeException
{
if(n<0||p<0)
{
throw new negativeException();
}
else if(n==0)
return 0;
else if(p==0)
return 1;
else
{
int r=1;
for(int i=1;i<=p;i++)
r=r*n;
return r;
}
}
}
public class ExceptionCreation
{
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("Enter no of test cases");
int t = in.nextInt();
while (t-- > 0) {
System.out.println("Enter n,p");
int n = in.nextInt();
int p = in.nextInt();
Calculator myCalculator = new Calculator();
try
{
int ans = myCalculator.power(n, p);
System.out.println("n to the power p is:");
System.out.println(ans);
}
catch (Exception e)
{
System.out.println(e.getMessage());
}
}
in.close();
}
}