Skip to content

Commit 00c7846

Browse files
committed
Solutions to Java Advanced section
1 parent 6a42083 commit 00c7846

File tree

8 files changed

+659
-0
lines changed

8 files changed

+659
-0
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import java.io.*;
2+
import java.security.*;
3+
4+
public class Solution {
5+
6+
public static void main(String[] args) throws Exception {
7+
DoNotTerminate.forbidExit();
8+
9+
try {
10+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
11+
int num = Integer.parseInt(br.readLine().trim());
12+
Object o;// Must be used to hold the reference of the instance of the class Solution.Inner.Private
13+
o = new Inner().new Private();
14+
15+
System.out.println(num + " is " + ((Solution.Inner.Private) o).powerof2(num));
16+
System.out.println("An instance of class: " + o.getClass().getCanonicalName() + " has been created");
17+
18+
}//end of try
19+
20+
catch (DoNotTerminate.ExitTrappedException e) {
21+
System.out.println("Unsuccessful Termination!!");
22+
}
23+
}//end of main
24+
25+
static class Inner {
26+
private class Private {
27+
private String powerof2(int num) {
28+
return ((num & num - 1) == 0) ? "power of 2" : "not a power of 2";
29+
}
30+
}
31+
}//end of Inner
32+
33+
}//end of Solution
34+
35+
class DoNotTerminate { //This class prevents exit(0)
36+
37+
public static class ExitTrappedException extends SecurityException {
38+
39+
private static final long serialVersionUID = 1L;
40+
}
41+
42+
public static void forbidExit() {
43+
final SecurityManager securityManager = new SecurityManager() {
44+
@Override
45+
public void checkPermission(Permission permission) {
46+
if (permission.getName().contains("exitVM")) {
47+
throw new ExitTrappedException();
48+
}
49+
}
50+
};
51+
System.setSecurityManager(securityManager);
52+
}
53+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import java.io.BufferedReader;
2+
import java.io.IOException;
3+
import java.io.InputStreamReader;
4+
5+
class Flower {
6+
String whatsYourName() {
7+
return "I have many names and types.";
8+
}
9+
}
10+
11+
class Jasmine extends Flower {
12+
@Override
13+
String whatsYourName() {
14+
return "Jasmine";
15+
}
16+
}
17+
18+
class Lily extends Flower {
19+
@Override
20+
String whatsYourName() {
21+
return "Lily";
22+
}
23+
}
24+
25+
class Region {
26+
Flower yourNationalFlower() {
27+
return new Flower();
28+
}
29+
}
30+
31+
class WestBengal extends Region {
32+
@Override
33+
Jasmine yourNationalFlower() {
34+
return new Jasmine();
35+
}
36+
}
37+
38+
class AndhraPradesh extends Region {
39+
@Override
40+
Lily yourNationalFlower() {
41+
return new Lily();
42+
}
43+
}
44+
45+
public class Solution {
46+
47+
public static void main(String[] args) throws IOException {
48+
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
49+
String s = reader.readLine().trim();
50+
Region region = null;
51+
switch (s) {
52+
case "WestBengal":
53+
region = new WestBengal();
54+
break;
55+
case "AndhraPradesh":
56+
region = new AndhraPradesh();
57+
break;
58+
}
59+
Flower flower = region.yourNationalFlower();
60+
System.out.println(flower.whatsYourName());
61+
}
62+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import java.lang.annotation.*;
2+
import java.lang.reflect.*;
3+
import java.util.*;
4+
5+
@Target(ElementType.METHOD)
6+
@Retention(RetentionPolicy.RUNTIME)
7+
@interface FamilyBudget {
8+
String userRole() default "GUEST";
9+
10+
int budgetLimit() default 0;
11+
}
12+
13+
class FamilyMember {
14+
@FamilyBudget(userRole = "SENIOR", budgetLimit = 100)
15+
public void seniorMember(int budget, int moneySpend) {
16+
System.out.println("Senior Member");
17+
System.out.println("Spend: " + moneySpend);
18+
System.out.println("Budget Left: " + (budget - moneySpend));
19+
}
20+
21+
@FamilyBudget(userRole = "JUNIOR", budgetLimit = 50)
22+
public void juniorUser(int budget, int moneySpend) {
23+
System.out.println("Junior Member");
24+
System.out.println("Spend: " + moneySpend);
25+
System.out.println("Budget Left: " + (budget - moneySpend));
26+
}
27+
}
28+
29+
public class Solution {
30+
31+
public static void main(String[] args) {
32+
Scanner in = new Scanner(System.in);
33+
int testCases = Integer.parseInt(in.nextLine());
34+
while (testCases-- > 0) {
35+
String role = in.next();
36+
int spend = in.nextInt();
37+
try {
38+
Class annotatedClass = FamilyMember.class;
39+
Method[] methods = annotatedClass.getMethods();
40+
for (Method method : methods) {
41+
if (method.isAnnotationPresent(FamilyBudget.class)) {
42+
FamilyBudget family = method.getAnnotation(FamilyBudget.class);
43+
String userRole = family.userRole();
44+
int budgetLimit = family.budgetLimit();
45+
if (userRole.equals(role)) {
46+
if (spend <= budgetLimit) {
47+
method.invoke(FamilyMember.class.newInstance(), budgetLimit, spend);
48+
} else {
49+
System.out.println("Budget Limit Over");
50+
}
51+
}
52+
}
53+
}
54+
} catch (Exception e) {
55+
e.printStackTrace();
56+
}
57+
}
58+
}
59+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import java.util.*;
2+
import java.security.*;
3+
4+
interface Food {
5+
public String getType();
6+
}
7+
8+
class Pizza implements Food {
9+
public String getType() {
10+
return "Someone ordered a Fast Food!";
11+
}
12+
}
13+
14+
class Cake implements Food {
15+
public String getType() {
16+
return "Someone ordered a Dessert!";
17+
}
18+
}
19+
20+
class FoodFactory {
21+
public Food getFood(String order) {
22+
switch (order.toLowerCase()) {
23+
case "pizza":
24+
return new Pizza();
25+
case "cake":
26+
return new Cake();
27+
default:
28+
return null;
29+
}
30+
}//End of getFood method
31+
}//End of factory class
32+
33+
public class Solution {
34+
35+
public static void main(String args[]) {
36+
Do_Not_Terminate.forbidExit();
37+
38+
try {
39+
Scanner sc = new Scanner(System.in);
40+
//creating the factory
41+
FoodFactory foodFactory = new FoodFactory();
42+
43+
//factory instantiates an object
44+
Food food = foodFactory.getFood(sc.nextLine());
45+
46+
System.out.println("The factory returned " + food.getClass());
47+
System.out.println(food.getType());
48+
} catch (Do_Not_Terminate.ExitTrappedException e) {
49+
System.out.println("Unsuccessful Termination!!");
50+
}
51+
}
52+
}
53+
54+
class Do_Not_Terminate {
55+
public static class ExitTrappedException extends SecurityException {
56+
private static final long serialVersionUID = 1L;
57+
}
58+
59+
public static void forbidExit() {
60+
final SecurityManager securityManager = new SecurityManager() {
61+
@Override
62+
public void checkPermission(Permission permission) {
63+
if (permission.getName().contains("exitVM")) {
64+
throw new ExitTrappedException();
65+
}
66+
}
67+
};
68+
System.setSecurityManager(securityManager);
69+
}
70+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
import java.io.*;
2+
import java.util.*;
3+
4+
interface PerformOperation {
5+
boolean check(int a);
6+
}
7+
8+
class MyMath {
9+
public static boolean checker(PerformOperation p, int num) {
10+
return p.check(num);
11+
}
12+
13+
public static PerformOperation isOdd() {
14+
return n -> (n & 1) == 1;
15+
}
16+
17+
public static PerformOperation isPrime() {
18+
return n -> {
19+
if (n < 2) {
20+
return false;
21+
} else if (n == 2) {
22+
return true;
23+
} else if (n % 2 == 0) {
24+
return false;
25+
}
26+
int sqrt = (int) Math.sqrt(n);
27+
for (int i = 3; i <= sqrt; i += 2) {
28+
if (n % i == 0) {
29+
return false;
30+
}
31+
}
32+
return true;
33+
};
34+
}
35+
36+
public static PerformOperation isPalindrome() {
37+
return n -> {
38+
String original = Integer.toString(n);
39+
String reversed = new StringBuilder(Integer.toString(n)).reverse().toString();
40+
return original.equals(reversed);
41+
};
42+
}
43+
}
44+
45+
public class Solution {
46+
47+
public static void main(String[] args) throws IOException {
48+
MyMath ob = new MyMath();
49+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
50+
int T = Integer.parseInt(br.readLine());
51+
PerformOperation op;
52+
boolean ret = false;
53+
String ans = null;
54+
while (T-- > 0) {
55+
String s = br.readLine().trim();
56+
StringTokenizer st = new StringTokenizer(s);
57+
int ch = Integer.parseInt(st.nextToken());
58+
int num = Integer.parseInt(st.nextToken());
59+
if (ch == 1) {
60+
op = ob.isOdd();
61+
ret = ob.checker(op, num);
62+
ans = (ret) ? "ODD" : "EVEN";
63+
} else if (ch == 2) {
64+
op = ob.isPrime();
65+
ret = ob.checker(op, num);
66+
ans = (ret) ? "PRIME" : "COMPOSITE";
67+
} else if (ch == 3) {
68+
op = ob.isPalindrome();
69+
ret = ob.checker(op, num);
70+
ans = (ret) ? "PALINDROME" : "NOT PALINDROME";
71+
72+
}
73+
System.out.println(ans);
74+
}
75+
}
76+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import java.io.*;
2+
import java.lang.reflect.*;
3+
import java.util.*;
4+
5+
class Add {
6+
void add(int... vars) {
7+
StringBuffer sb = new StringBuffer();
8+
int sum = 0;
9+
10+
for (int var : vars) {
11+
sb.append(var + "+");
12+
sum += var;
13+
}
14+
15+
sb.setCharAt(sb.length() - 1, '=');
16+
sb.append(sum);
17+
System.out.println(sb);
18+
}
19+
}
20+
21+
public class Solution {
22+
23+
public static void main(String[] args) {
24+
try {
25+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
26+
int n1 = Integer.parseInt(br.readLine());
27+
int n2 = Integer.parseInt(br.readLine());
28+
int n3 = Integer.parseInt(br.readLine());
29+
int n4 = Integer.parseInt(br.readLine());
30+
int n5 = Integer.parseInt(br.readLine());
31+
int n6 = Integer.parseInt(br.readLine());
32+
Add ob = new Add();
33+
ob.add(n1, n2);
34+
ob.add(n1, n2, n3);
35+
ob.add(n1, n2, n3, n4, n5);
36+
ob.add(n1, n2, n3, n4, n5, n6);
37+
Method[] methods = Add.class.getDeclaredMethods();
38+
Set<String> set = new HashSet<>();
39+
boolean overload = false;
40+
for (int i = 0; i < methods.length; i++) {
41+
if (set.contains(methods[i].getName())) {
42+
overload = true;
43+
break;
44+
}
45+
set.add(methods[i].getName());
46+
}
47+
if (overload) {
48+
throw new Exception("Overloading not allowed");
49+
}
50+
} catch (Exception e) {
51+
e.printStackTrace();
52+
}
53+
}
54+
}

0 commit comments

Comments
 (0)