Repository navigation
Expand file tree
/
Copy pathJavaPractice.java
More file actions
300 lines (242 loc) · 8.7 KB
/
Copy pathJavaPractice.java
File metadata and controls
300 lines (242 loc) · 8.7 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
import java.util.*;
public class JavaPractice {
public static class LinkedList {
class Node {
int data;
Node next;
Node(int data) {
this.data = data;
this.next = null;
}
}
Node head;
public void insert(int data) {
Node newNode = new Node(data);
if (head == null) {
head = newNode;
return;
}
Node current = head;
while (current.next != null) {
current = current.next;
}
current.next = newNode;
}
public void reverse() {
Node prev = null;
Node current = head;
Node next = null;
while (current != null) {
next = current.next;
current.next = prev;
prev = current;
current = next;
}
head = prev;
}
public void printList() {
Node current = head;
while (current != null) {
System.out.print(current.data + " ");
current = current.next;
}
System.out.println();
}
}
public static void wordFrequencyCounter(String sentence) {
String cleanedSentence = sentence.replaceAll("[^a-zA-Z\\s]", "").toLowerCase();
String[] words = cleanedSentence.split("\\s+");
Map<String, Integer> frequencyMap = new HashMap<>();
for (String word : words) {
frequencyMap.put(word, frequencyMap.getOrDefault(word, 0) + 1);
}
frequencyMap.forEach((word, count) -> System.out.println(word + ": " + count));
}
public static boolean isPrime(int number) {
if (number <= 1) return false;
for (int i = 2; i <= Math.sqrt(number); i++) {
if (number % i == 0) return false;
}
return true;
}
public static int factorial(int n) {
if (n == 0 || n == 1) {
return 1;
}
else if (n < 0) {
System.out.println("Invalid");
return -1;
}
return n * factorial(n - 1);
}
public static boolean anagramChecker(String str1, String str2) {
if (str1 == null || str2 == null) return false;
char[] charArray1 = str1.replaceAll("\\s", "").toLowerCase().toCharArray();
char[] charArray2 = str2.replaceAll("\\s", "").toLowerCase().toCharArray();
Arrays.sort(charArray1);
Arrays.sort(charArray2);
return Arrays.equals(charArray1, charArray2);
}
public static void bubbleSort(int[] arr) {
if (arr == null || arr.length <= 1) return;
int arrLength = arr.length;
for (int i = 0; i < arrLength - 1; i++) {
for (int j = 0; j < arrLength - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
public static void countVowelsAndConsonants(String str) {
if (str == null || str.isEmpty()) {
System.out.println("Invalid input");
return;
}
str = str.toLowerCase();
int vowels = 0, consonants = 0;
for (char ch : str.toCharArray()) {
if (Character.isLetter(ch)) {
if ("aeiou".indexOf(ch) != -1) {
vowels++;
} else {
consonants++;
}
}
}
System.out.println("Vowels: " + vowels + ", Consonants: " + consonants);
}
public static int gcd(int a, int b) {
a = Math.abs(a);
b = Math.abs(b);
if (a == 0) return b;
if (b == 0) return a;
while (b != 0) {
int temp = b;
b = a % b;
a = temp;
}
return a;
}
public static int lcm(int a, int b) {
return (a * b) / gcd(a, b);
}
public static void multiplyMatrices(int[][] A, int[][] B) {
if (A == null || B == null || A.length == 0 || B.length == 0) {
throw new IllegalArgumentException("Matrices cannot be null or empty");
}
int rowsALength = A.length;
int colsALength = A[0].length;
int colsBLength = B[0].length;
int[][] result = new int[rowsALength][colsBLength];
for (int i = 0; i < rowsALength; i++) {
for (int j = 0; j < colsBLength; j++) {
for (int k = 0; k < colsALength; k++) {
result[i][j] += A[i][k] * B[k][j];
}
}
}
for (int[] row : result) {
System.out.println(Arrays.toString(row));
}
}
public static boolean isArmstrong(int number) {
if (number < 0) return false;
if (number < 10) return true;
int originalNumber = number;
int sum = 0;
int digits = String.valueOf(number).length();
while (number > 0) {
int digit = number % 10;
sum += Math.pow(digit, digits);
number /= 10;
}
return sum == originalNumber;
}
public static int[] removeDuplicates(int[] arr) {
if (arr == null || arr.length <= 1) return arr;
return Arrays.stream(arr).distinct().toArray();
}
static class BankAccount {
private double balance;
private String accountNumber;
public BankAccount(String accountNumber, double initialBalance) {
this.accountNumber = accountNumber;
this.balance = initialBalance;
}
public void deposit(double amount) {
if (amount > 0) {
balance += amount;
System.out.println("Deposited: " + amount);
}
}
public void withdraw(double amount) {
if (amount > 0 && amount <= balance) {
balance -= amount;
System.out.println("Withdrawn: " + amount);
} else {
System.out.println("Insufficient funds");
}
}
public double getBalance() {
return balance;
}
}
// Main Function
public static void main(String[] args){
System.out.println("1. Linked List Reversal:");
LinkedList list = new LinkedList();
list.insert(2);
list.insert(1);
list.insert(3);
list.insert(4);
list.insert(8);
list.insert(6);
list.insert(7);
list.insert(7);
list.insert(6);
System.out.println("Original List:");
list.printList();
list.reverse();
System.out.println("Reversed List:");
list.printList();
System.out.println("\n2. Word Frequency Counter:");
String sentence = "Hello, hello world! This is a test. Test, test.";
wordFrequencyCounter(sentence);
System.out.println("\n3. Prime Number Checker:");
System.out.println("is 17 prime?: " + isPrime(17));
System.out.println("is 20 prime?: " + isPrime(20));
System.out.println("\n4. Factorial:");
System.out.println("Factorial of 5: " + factorial(5));
System.out.println("\n5. Anagram Checker:");
System.out.println("listen silent: " + anagramChecker("listen", "silent"));
System.out.println("\n6. Bubble Sort:");
int[] arr = {1,6,2,3,8,4,7,5};
System.out.println("Before sorting: " + Arrays.toString(arr));
bubbleSort(arr);
System.out.println("After sorting: " + Arrays.toString(arr));
System.out.println("\n7. Vowel and Consonant Counter:");
countVowelsAndConsonants("Hello World");
System.out.println("\n8. GCD and LCM:");
System.out.println("GCD: " + gcd(50, 10));
System.out.println("LCM: " + lcm(50, 10));
System.out.println("\n9. Matrix Multiplication:");
int[][] matrix1 = {{1, 2}, {3, 4}};
int[][] matrix2 = {{5, 6}, {7, 8}};
multiplyMatrices(matrix1, matrix2);
System.out.println("\n10. Armstrong Number:");
System.out.println("153 is Armstrong: " + isArmstrong(153));
System.out.println("\n11. Remove Duplicates:");
int[] duplicateArr = {5, 2, 2, 3, 4, 4, 5};
System.out.println("Without Duplicates: " + Arrays.toString(removeDuplicates(duplicateArr)));
System.out.println("\n12. Banking System:");
double initialBalance = 1000;
BankAccount account = new BankAccount("123456", initialBalance);
System.out.println("Initial Balance: " + account.getBalance());
account.deposit(500);
account.withdraw(200);
System.out.println("Current Balance: " + account.getBalance());
}
}