- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHackerrankJava0062.java
More file actions
Latest commit
62 lines (50 loc) · 2.15 KB
/
Copy pathHackerrankJava0062.java
File metadata and controls
62 lines (50 loc) · 2.15 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
importjava.io.BufferedReader;
importjava.io.InputStreamReader;
importjava.security.Permission;
publicclassHackerrankJava0062 {
publicstaticvoidmain(String[] args) throwsException {
// Can You Access?
// https://www.hackerrank.com/challenges/can-you-access/problem?isFullScreen=true
DoNotTerminate.forbidExit();
try {
BufferedReaderbr = newBufferedReader(newInputStreamReader(System.in));
intnum = Integer.parseInt(br.readLine().trim());
Objecto;// Must be used to hold the reference of the instance of the class Solution.Inner.Private
// -------------------------------------------------------------------------------------------------
// --- You need to add this lines for solution hackerrank provides all other lines for challange ---
// -------------------------------------------------------------------------------------------------
o = newInner().newPrivate();
System.out.println(num + " is " + ((Inner.Private) o).powerof2(num));
// -------------------------------------------------------------------------------------------------
// -------------------------------------------------------------------------------------------------
// -------------------------------------------------------------------------------------------------
System.out.println("An instance of class: " + o.getClass().getCanonicalName() + " has been created");
}
catch (DoNotTerminate.ExitTrappedExceptione) {
System.out.println("Unsuccessful Termination!!");
}
}
staticclassInner {
privateclassPrivate {
privateStringpowerof2(intnum) {
return ((num & num - 1) == 0) ? "power of 2" : "not a power of 2";
}
}
}
}
classDoNotTerminate { // This class prevents exit(0)
publicstaticclassExitTrappedExceptionextendsSecurityException {
privatestaticfinallongserialVersionUID = 1L;
}
publicstaticvoidforbidExit() {
finalSecurityManagersecurityManager = newSecurityManager() {
@Override
publicvoidcheckPermission(Permissionpermission) {
if (permission.getName().contains("exitVM")) {
thrownewExitTrappedException();
}
}
};
System.setSecurityManager(securityManager);
}
}