Skip to content

Commit 137346d

Browse files
committed
initial commit
1 parent f256492 commit 137346d

File tree

95 files changed

+4062
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

95 files changed

+4062
-0
lines changed

src/Tester.java

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
public class Tester {
2+
public static void main(String[] args) {
3+
// printToConsoleWithoutSemiColon();
4+
Tester tester = new Tester();
5+
tester.test(10);
6+
tester.test(10L);
7+
tester.test((Integer) 10);
8+
tester.test(10, 20);
9+
tester.test((byte) 10);
10+
tester.test((short) 10);
11+
12+
13+
label:
14+
for (int i = 0; i < 10; i++) {
15+
if (i == 5) {
16+
continue label;
17+
}
18+
System.out.println("" + i);
19+
}
20+
}
21+
22+
private static void printToConsoleWithoutSemiColon() {
23+
// Print Hello world without semi-colon
24+
if (System.out.printf("Hello World \n") == null) {
25+
26+
}
27+
if (System.out.append("Hello World \n") == null) {
28+
29+
}
30+
}
31+
32+
static boolean printString() {
33+
System.out.println("Hola!");
34+
return true;
35+
}
36+
37+
static boolean printStatus = printString();
38+
39+
public void test(int i) {
40+
System.out.println("int");
41+
}
42+
43+
public void test(int... i) {
44+
System.out.println("int...");
45+
}
46+
47+
public void test(Integer i) {
48+
System.out.println("Integer");
49+
}
50+
51+
public void test(long i) {
52+
System.out.println("long");
53+
}
54+
55+
public void test(byte i) {
56+
System.out.println("byte");
57+
}
58+
59+
public void test(short i) {
60+
System.out.println("short");
61+
}
62+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package annotationSample;
2+
3+
@XMLSerializable(tag = "acct_details")
4+
public class BankAccount {
5+
private int id;
6+
@XMLElement
7+
private String name;
8+
@XMLElement(tag = "bal")
9+
private double balance;
10+
11+
public BankAccount() {
12+
}
13+
14+
public BankAccount(int id, String name, double balance) {
15+
this.id = id;
16+
this.name = name;
17+
this.balance = balance;
18+
}
19+
20+
public int getId() {
21+
return id;
22+
}
23+
24+
public void setId(int id) {
25+
this.id = id;
26+
}
27+
28+
public String getName() {
29+
return name;
30+
}
31+
32+
public void setName(String name) {
33+
this.name = name;
34+
}
35+
36+
public double getBalance() {
37+
return balance;
38+
}
39+
40+
public void setBalance(double balance) {
41+
this.balance = balance;
42+
}
43+
}

src/annotationSample/Tester.java

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package annotationSample;
2+
3+
import java.lang.reflect.Field;
4+
5+
public class Tester {
6+
public static void main(String[] args) {
7+
BankAccount account = new BankAccount(1, "Dev", 160790.37);
8+
try {
9+
System.out.println(getXMLString(account));
10+
} catch (Exception e) {
11+
e.printStackTrace();
12+
}
13+
}
14+
15+
public static String getXMLString(Object object) throws Exception {
16+
Class<?> aClass = object.getClass();
17+
if (!aClass.isAnnotationPresent(XMLSerializable.class)) {
18+
throw new RuntimeException("XMLSerializable annotation not present");
19+
}
20+
StringBuilder strBuilder = new StringBuilder();
21+
22+
for (Field field : aClass.getDeclaredFields()) {
23+
field.setAccessible(true);
24+
if (field.isAnnotationPresent(XMLElement.class)) {
25+
String fieldTag = getTagFor(field);
26+
strBuilder.append("\t<").append(fieldTag).append(">")
27+
.append(field.get(object).toString())
28+
.append("</").append(fieldTag).append(">\n");
29+
}
30+
}
31+
32+
String classTag = getTagFor(aClass);
33+
34+
return "<" + classTag + ">\n" + strBuilder + "</" + classTag + ">";
35+
}
36+
37+
public static String getTagFor(Class<?> aClass) {
38+
String tag = aClass.getAnnotation(XMLSerializable.class).tag();
39+
return tag.isEmpty() ? aClass.getSimpleName() : tag;
40+
}
41+
42+
public static String getTagFor(Field field) {
43+
String tag = field.getAnnotation(XMLElement.class).tag();
44+
return tag.isEmpty() ? field.getName() : tag;
45+
}
46+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package annotationSample;
2+
3+
import java.lang.annotation.ElementType;
4+
import java.lang.annotation.Retention;
5+
import java.lang.annotation.RetentionPolicy;
6+
import java.lang.annotation.Target;
7+
8+
@Target(ElementType.FIELD)
9+
@Retention(RetentionPolicy.RUNTIME)
10+
public @interface XMLElement {
11+
String tag() default "";
12+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package annotationSample;
2+
3+
import java.lang.annotation.ElementType;
4+
import java.lang.annotation.Retention;
5+
import java.lang.annotation.RetentionPolicy;
6+
import java.lang.annotation.Target;
7+
8+
@Target(ElementType.TYPE)
9+
@Retention(RetentionPolicy.RUNTIME)
10+
public @interface XMLSerializable {
11+
String tag() default "";
12+
}
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
package arrayPrograms;
2+
3+
import java.util.HashSet;
4+
import java.util.Set;
5+
6+
public class ArrayPrograms {
7+
// Find leaders in an array
8+
public static void findLeaders(int[] arr) {
9+
if (arr.length == 0) {
10+
return;
11+
}
12+
13+
int length = arr.length;
14+
15+
if (length == 1) {
16+
System.out.print(arr[0]);
17+
return;
18+
}
19+
20+
int max = arr[length - 1];
21+
System.out.print(max + " ");
22+
23+
for (int i = length - 2; i >= 0; i--) {
24+
if (arr[i] > max) {
25+
System.out.print(arr[i] + " ");
26+
max = arr[i];
27+
}
28+
}
29+
}
30+
31+
// Find intersection of two arrays
32+
public static void findIntersection(int[] arr1, int[] arr2) {
33+
Set<Integer> set1 = new HashSet<>();
34+
Set<Integer> set2 = new HashSet<>();
35+
for (int e : arr1) {
36+
set1.add(e);
37+
}
38+
for (int e : arr2) {
39+
set2.add(e);
40+
}
41+
42+
set1.retainAll(set2);
43+
44+
System.out.println(set1);
45+
}
46+
47+
// Find intersection of two arrays
48+
public static void findIntersection1(int[] arr1, int[] arr2) {
49+
Set<Integer> set1 = new HashSet<>();
50+
for (int e : arr1) {
51+
set1.add(e);
52+
}
53+
54+
// Using contains
55+
for (int e : arr2) {
56+
if (set1.contains(e)) {
57+
System.out.print(e + " ");
58+
}
59+
}
60+
61+
// Using add
62+
/*for (int e : arr2) {
63+
if (!set1.add(e)) {
64+
System.out.print(e + " ");
65+
}
66+
}*/
67+
}
68+
69+
public static String[] sortArrayAscending(String[] arr) {
70+
int length = arr.length;
71+
72+
String temp;
73+
for (int i = 0; i < length; i++) {
74+
for (int j = i + 1; j < length; j++) {
75+
if (arr[i].compareToIgnoreCase(arr[j]) > 0) {
76+
temp = arr[i];
77+
arr[i] = arr[j];
78+
arr[j] = temp;
79+
}
80+
}
81+
}
82+
83+
return arr;
84+
}
85+
86+
public static String[] sortArrayDescending(String[] arr) {
87+
int length = arr.length;
88+
89+
String temp;
90+
for (int i = 0; i < length; i++) {
91+
for (int j = i + 1; j < length; j++) {
92+
if (arr[i].compareToIgnoreCase(arr[j]) < 0) {
93+
temp = arr[i];
94+
arr[i] = arr[j];
95+
arr[j] = temp;
96+
}
97+
}
98+
}
99+
100+
return arr;
101+
}
102+
103+
public static int[] getOddNumbers(int[] arr) {
104+
int oddNoCount = 0;
105+
for (int i = 0; i < arr.length; i++) {
106+
if (arr[i] % 2 != 0) {
107+
oddNoCount++;
108+
}
109+
}
110+
111+
int[] result = new int[oddNoCount];
112+
int resultIndex = 0;
113+
114+
for (int i = 0; i < arr.length; i++) {
115+
if (arr[i] % 2 != 0) {
116+
result[resultIndex] = arr[i];
117+
resultIndex += 1;
118+
}
119+
}
120+
121+
return result;
122+
}
123+
124+
public static int[] removeArrayElement(int[] arr, int element) {
125+
int length = arr.length;
126+
int[] newArr = new int[length - 1];
127+
128+
int newArrIndex = 0;
129+
for (int j : arr) {
130+
if (j != element) {
131+
newArr[newArrIndex++] = j;
132+
}
133+
}
134+
135+
return newArr;
136+
}
137+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package arrayPrograms;
2+
3+
public class MaxKConsecutiveSum {
4+
public static int bruteForce(int[] arr, int k) {
5+
int n = arr.length;
6+
7+
if (n < k) {
8+
System.out.println("Invalid input. K must be less than or equal to n");
9+
return -1;
10+
}
11+
12+
int maxSum = Integer.MIN_VALUE;
13+
14+
for (int i = 0; i < n - k + 1; i++) {
15+
int windowSum = 0;
16+
for (int j = 0; j < k; j++) {
17+
windowSum += arr[i + j];
18+
}
19+
maxSum = Math.max(windowSum, maxSum);
20+
}
21+
22+
return maxSum;
23+
}
24+
25+
public static int optimized(int[] arr, int k) {
26+
int n = arr.length;
27+
28+
if (n < k) {
29+
System.out.println("Invalid input. K must be less than or equal to n");
30+
return -1;
31+
}
32+
33+
int maxSum = Integer.MIN_VALUE;
34+
35+
// Calculate first K elements sum
36+
for (int i = 0; i < k; i++) {
37+
maxSum += arr[i];
38+
}
39+
40+
// Start from k
41+
int windowSum = maxSum;
42+
for (int j = k; j < n; j++) {
43+
windowSum += arr[j] - arr[j - 2];
44+
maxSum = Math.max(windowSum, maxSum);
45+
}
46+
47+
return maxSum;
48+
}
49+
50+
public static void main(String[] args) {
51+
System.out.println(bruteForce(new int[]{100, 200, 300, 400}, 1));
52+
System.out.println(bruteForce(new int[]{1, 4, 2, 10, 2, 3, 1, 0, 20}, 4));
53+
}
54+
}

0 commit comments

Comments
 (0)