├── .DS_Store ├── .gitignore ├── .vscode ├── java-formatter.xml ├── settings.json └── tasks.json ├── LeetCode_JAVA ├── 1.two-sum.java ├── 1094.car-pooling.java ├── 1291.sequential-digits.java ├── 134.gas-station.java ├── 142.linked-list-cycle-ii.java ├── 15.3-sum.java ├── 1672.richest-customer-wealth.java ├── 19.remove-nth-node-from-end-of-list.java ├── 202.happy-number.java ├── 206.reverse-linked-list.java ├── 21.merge-two-sorted-lists.java ├── 234.palindrome-linked-list.java ├── 283.move-zeroes.java ├── 287.find-the-duplicate-number.java ├── 344.reverse-string.java ├── 452.minimum-number-of-arrows-to-burst-balloons.java ├── 520.detect-capital.java ├── 605.can-place-flowers.java ├── 61.rotate-list.java ├── 849.maximize-distance-to-closest-person.java ├── 875.koko-eating-bananas.java ├── 876.middle-of-the-linked-list.java ├── 917.reverse-only-letters.java ├── 941.valid-mountain-array.java ├── 997.find-the-town-judge.java └── FUNCTIONS_JAVA.txt ├── java-practice ├── .DS_Store ├── Abstraction.java ├── Abstraction_gfg.java ├── Access_Modifier.java ├── Array_List.java ├── Autoboxing.java ├── ConstructorOverloading.java ├── Constructor_Chaining.java ├── Encapsulation_OOP.java ├── Enum.java ├── FUNCTIONS_JAVA.txt ├── FirstProgram.java ├── Hash_Map.java ├── Inheritance_Chaining.java ├── Inheritance_OOP.java ├── InterfaceTest.java ├── JAVA.txt ├── Method.java ├── Method_Overloading.java ├── Method_Overridding.java ├── Second_Largest_Ele.java ├── StringEquals.java ├── String_Function.java └── Test.java └── mypackage ├── .DS_Store ├── Origin.java ├── p1 ├── House.java └── Room.java └── tempCodeRunnerFile.java /.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/niharika2k00/Java/abe9fc7615a5ff3852a90efd2230f29d569dbc68/.DS_Store -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | 2 | # ignore a file type .class 3 | *.class 4 | 5 | -------------------------------------------------------------------------------- /.vscode/java-formatter.xml: -------------------------------------------------------------------------------- 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 | 301 | 302 | 303 | 304 | 305 | 306 | 307 | 308 | 309 | 310 | 311 | 312 | 313 | 314 | 315 | 316 | 317 | 318 | 319 | 320 | 321 | 322 | 323 | 324 | 325 | 326 | 327 | 328 | 329 | 330 | 331 | 332 | 333 | 334 | 335 | 336 | 337 | 338 | 339 | 340 | 341 | 342 | 343 | 344 | 345 | 346 | 347 | 348 | 349 | 350 | 351 | 352 | 353 | 354 | 355 | 356 | 357 | 358 | 359 | 360 | 361 | 362 | 363 | 364 | 365 | 366 | 367 | 368 | 369 | 370 | 371 | 372 | 373 | 374 | 375 | 376 | 377 | 378 | 379 | 380 | 381 | 382 | 383 | -------------------------------------------------------------------------------- /.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "java.format.settings.url": ".vscode/java-formatter.xml", 3 | "[java]": { 4 | "editor.insertSpaces": true, 5 | "editor.tabSize": 4, 6 | "editor.detectIndentation": true 7 | }, 8 | "files.insertFinalNewline": false, 9 | "files.trimFinalNewlines": true, 10 | "java.debug.settings.onBuildFailureProceed": true, 11 | "files.associations": { 12 | "*.spc": "terraform", 13 | "*.sp": "hcl", 14 | ".sql": "sql", 15 | "*.tf": "terraform", 16 | "ios": "cpp", 17 | "random": "cpp", 18 | "string": "cpp", 19 | "vector": "cpp" 20 | }, 21 | "java.format.onType.enabled": true, 22 | "java.completion.chain.enabled": true, 23 | "java.codeGeneration.toString.skipNullValues": true, 24 | "java.edit.smartSemicolonDetection.enabled": true, 25 | "java.implementationsCodeLens.enabled": true, 26 | "javascript.format.insertSpaceAfterConstructor": true 27 | } 28 | -------------------------------------------------------------------------------- /.vscode/tasks.json: -------------------------------------------------------------------------------- 1 | { 2 | "tasks": [ 3 | { 4 | "type": "cppbuild", 5 | "label": "C/C++: clang++ build active file", 6 | "command": "/usr/bin/clang++", 7 | "args": [ 8 | "-fcolor-diagnostics", 9 | "-fansi-escape-codes", 10 | "-g", 11 | "${file}", 12 | "-o", 13 | "${fileDirname}/${fileBasenameNoExtension}" 14 | ], 15 | "options": { 16 | "cwd": "${fileDirname}" 17 | }, 18 | "problemMatcher": [ 19 | "$gcc" 20 | ], 21 | "group": { 22 | "kind": "build", 23 | "isDefault": true 24 | }, 25 | "detail": "Task generated by Debugger." 26 | } 27 | ], 28 | "version": "2.0.0" 29 | } -------------------------------------------------------------------------------- /LeetCode_JAVA/1.two-sum.java: -------------------------------------------------------------------------------- 1 | import java.util.HashMap; 2 | 3 | /* 4 | * @lc app=leetcode id=1 lang=java 5 | * 6 | * [1] Two Sum 7 | */ 8 | // Any 2 number from the list having , whose addition is == TARGET 9 | // @lc code=start 10 | class Solution { 11 | public int[] twoSum(int[] nums, int target) { 12 | 13 | int i, len = nums.length, diff = 0; 14 | HashMap hashMap = new HashMap<>(); 15 | int arr[] = new int[2]; 16 | 17 | for (i = 0; i < len; i++) { 18 | diff = target - nums[i]; 19 | if (hashMap.containsKey(diff)) { 20 | arr[1] = i; 21 | arr[0] = hashMap.get(diff); 22 | break; 23 | } 24 | hashMap.put(nums[i], i); 25 | } 26 | 27 | return arr; 28 | } 29 | } 30 | // @lc code=end 31 | 32 | /* 33 | KEY --> Value of Array 34 | VALUE --> Index 35 | */ -------------------------------------------------------------------------------- /LeetCode_JAVA/1094.car-pooling.java: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode id=1094 lang=java 3 | * 4 | * [1094] Car Pooling 5 | */ 6 | 7 | // @lc code=start 8 | class Solution { 9 | public boolean carPooling(int[][] trips, int capacity) { 10 | 11 | // HashMap hashMap = new HashMap<>(); 12 | int lengthOfTrip[] = new int[1001]; 13 | int currPassenger = 0, i; 14 | 15 | // 2D - Array 16 | for (int arr[] : trips) { 17 | lengthOfTrip[arr[1]] += arr[0]; // add passsenger start time 18 | lengthOfTrip[arr[2]] -= arr[0]; // delete at end / drop when they reach 19 | } 20 | 21 | for (i = 0; i < lengthOfTrip.length; i++) { 22 | currPassenger = currPassenger + lengthOfTrip[i]; 23 | if (currPassenger > capacity) 24 | return false; 25 | } 26 | 27 | return true; 28 | } 29 | } 30 | // @lc code=end 31 | -------------------------------------------------------------------------------- /LeetCode_JAVA/1291.sequential-digits.java: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode id=1291 lang=java 3 | * 4 | * [1291] Sequential Digits 5 | */ 6 | 7 | // @lc code=start 8 | class Solution { 9 | 10 | List vec = new ArrayList(); 11 | 12 | public List sequentialDigits(int low, int high) { 13 | int i; 14 | // Range 1 to 9 15 | for (i = 1; i < 10; i++) { 16 | dfs(low, high, i, 0); 17 | } 18 | 19 | Collections.sort(vec); 20 | return vec; 21 | } 22 | 23 | void dfs(int low, int high, int i, int num) { 24 | 25 | if (low <= num && num <= high) 26 | vec.add(num); 27 | 28 | if (num > high || i > 9) // Out of Bounds 29 | return; 30 | 31 | dfs(low, high, i + 1, num * 10 + i); 32 | } 33 | } 34 | // @lc code=end 35 | -------------------------------------------------------------------------------- /LeetCode_JAVA/134.gas-station.java: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode id=134 lang=java 3 | * 4 | * [134] Gas Station 5 | */ 6 | 7 | // @lc code=start 8 | class Solution { 9 | public int canCompleteCircuit(int[] gas, int[] cost) { 10 | 11 | // GREEDY PROBLEM 12 | //totalGas -> stores the gas remaining after traverlling around the circuit, 13 | //currGas -> stores the gasRem after filling up at a stn & trvaelling to next station, if that's not possible, stores 0 14 | //station -> stores the index from which we can start and finish a round circuit 15 | 16 | int totalGas = 0, i, currGas = 0, station = 0, len = gas.length; 17 | 18 | for (i = 0; i < len; i++) { 19 | totalGas = totalGas + (gas[i] - cost[i]); 20 | currGas = currGas + (gas[i] - cost[i]); 21 | 22 | if (currGas < 0) // negative cost 23 | { 24 | currGas = 0; 25 | station = i + 1; 26 | } 27 | } 28 | 29 | // If the Total Gas in the NET JOURNEY is Negative then return -1 30 | return (totalGas < 0) ? -1 : station; 31 | } 32 | } 33 | // @lc code=end 34 | -------------------------------------------------------------------------------- /LeetCode_JAVA/142.linked-list-cycle-ii.java: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode id=142 lang=java 3 | * 4 | * [142] Linked List Cycle II 5 | */ 6 | 7 | // @lc code=start 8 | /** 9 | * Definition for singly-linked list. 10 | * class ListNode { 11 | * int val; 12 | * ListNode next; 13 | * ListNode(int x) { 14 | * val = x; 15 | * next = null; 16 | * } 17 | * } 18 | */ 19 | public class Solution { 20 | public ListNode detectCycle(ListNode head) { 21 | 22 | ListNode slow = head, fast = head; 23 | 24 | while (fast != null && fast.next != null) { 25 | slow = slow.next; 26 | fast = fast.next.next; 27 | 28 | if (slow == fast) 29 | break; 30 | } 31 | 32 | if (fast == null || fast.next == null) 33 | return null; 34 | 35 | // Meeting point of Slow & Head is the Starting of the cycle in Linked List 36 | while (head != slow) { 37 | head = head.next; 38 | slow = slow.next; 39 | } 40 | 41 | return head; 42 | } 43 | } 44 | // @lc code=end 45 | -------------------------------------------------------------------------------- /LeetCode_JAVA/15.3-sum.java: -------------------------------------------------------------------------------- 1 | 2 | /* 3 | * @lc app=leetcode id=15 lang=java 4 | * 5 | * [15] 3Sum 6 | */ 7 | 8 | // @lc code=start 9 | class Solution { 10 | public List> threeSum(int[] nums) { 11 | 12 | List> result = new LinkedList<>(); 13 | int i, high = nums.length - 1, low = 0, sum = 0; 14 | Arrays.sort(nums); 15 | 16 | for (i = 0; i < nums.length - 2; i++) { 17 | if (nums[i] <= 0) { 18 | if (i == 0 || nums[i] != nums[i - 1]) { 19 | low = i + 1; 20 | high = nums.length - 1; 21 | sum = -nums[i]; 22 | 23 | // Two Pointer Loop 24 | while (low < high) { 25 | if (nums[low] + nums[high] == sum) { 26 | result.add(Arrays.asList(nums[i], nums[low], nums[high])); 27 | 28 | //Avoiding duplicates. 29 | while (low < high && nums[low] == nums[low + 1]) 30 | low++; 31 | 32 | //Avoiding duplicates. 33 | while (low < high && nums[high] == nums[high - 1]) 34 | high--; 35 | 36 | low++; 37 | high--; 38 | } 39 | else if (nums[low] + nums[high] < sum) 40 | low++; 41 | 42 | else 43 | high--; 44 | } 45 | } 46 | } 47 | } 48 | 49 | return result; 50 | } 51 | } 52 | // @lc code=end 53 | -------------------------------------------------------------------------------- /LeetCode_JAVA/1672.richest-customer-wealth.java: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode id=1672 lang=java 3 | * 4 | * [1672] Richest Customer Wealth 5 | */ 6 | 7 | // @lc code=start 8 | class Solution { 9 | public int maximumWealth(int[][] accounts) { 10 | 11 | int len = accounts.length, maxWealth = 0, currWealth = 0; 12 | System.out.println(len); 13 | 14 | for (int[] cust : accounts) { 15 | currWealth = 0; // initialises0 16 | 17 | for (int money : cust) { 18 | currWealth = currWealth + money; // each row 's SUM ' 19 | } 20 | 21 | maxWealth = Math.max(currWealth, maxWealth); 22 | } 23 | 24 | return maxWealth; 25 | } 26 | } 27 | // @lc code=end 28 | -------------------------------------------------------------------------------- /LeetCode_JAVA/19.remove-nth-node-from-end-of-list.java: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode id=19 lang=java 3 | * 4 | * [19] Remove Nth Node From End of List 5 | */ 6 | 7 | // @lc code=start 8 | /** 9 | * Definition for singly-linked list. 10 | * public class ListNode { 11 | * int val; 12 | * ListNode next; 13 | * ListNode() {} 14 | * ListNode(int val) { this.val = val; } 15 | * ListNode(int val, ListNode next) { this.val = val; this.next = next; } 16 | * } 17 | */ 18 | class Solution { 19 | public ListNode removeNthFromEnd(ListNode head, int n) { 20 | 21 | if (head.next == null) 22 | return null; 23 | 24 | ListNode start = new ListNode(-1); 25 | start.next = head; 26 | ListNode slow = start, fast = start; 27 | int i; 28 | 29 | for (i = 0; i < n; i++) 30 | fast = fast.next; 31 | 32 | while (fast.next != null) { 33 | fast = fast.next; 34 | slow = slow.next; 35 | } 36 | 37 | slow.next = slow.next.next; 38 | 39 | return start.next; 40 | } 41 | } 42 | // @lc code=end 43 | 44 | 45 | /* APPROACHES 46 | --------------------- 47 | 48 | Method - 1 (Naive Approach) 49 | 1) Count the Len of the LinkedList O(n) 50 | 2) n = Len - n , EX- (5 - 2 = 3) , means 3rd NODE from the beginning need to be delete. 51 | 52 | Time Complexity : O(n) + O(n) = O(2n) 53 | 54 | Method - 2 (Two Pointer Approach) 55 | 1) Move fast pointer n - th times, 56 | 2) Then the Slow Pointer +1 step. 57 | Fast - Slow = N 58 | 59 | */ -------------------------------------------------------------------------------- /LeetCode_JAVA/202.happy-number.java: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode id=202 lang=java 3 | * 4 | * [202] Happy Number 5 | */ 6 | 7 | // @lc code=start 8 | 9 | /* 10 | let's try different n: 11 | true (1) -> 1 12 | false (2) -> 4 -> 16 -> 37 -> 58 -> 89 -> 145 -> 42 -> 20 -> 4 13 | false (3) -> 9 -> 81 -> 65 -> 61 -> 37 (look at 2) 14 | false (4) -> (look at 2) 15 | false (5) -> 25 -> 29 -> 85 -> 89 (look at 2) 16 | false (6) -> 36 -> 45 -> 41 -> 17 -> 50 -> 25 (look at 5) 17 | true (7) -> 49 -> 97 -> 10 18 | false (8) -> 64 -> 52 -> 29 (look at 5) 19 | false (9) -> 9 -> 81 -> 65 (look at 3) 20 | 21 | All other n >= 10, while computing will become [1-9], 22 | So there are two cases 1 and 7 which are true. 23 | 24 | Notice, that all falses has the same path as 2 (loop). 25 | And 7 at the end becomes 1. 26 | */ 27 | 28 | class Solution { 29 | public boolean isHappy(int n) { 30 | 31 | int nextNum = 0; 32 | 33 | while (true) { 34 | nextNum = 0; 35 | 36 | // Runs the Loop untill a single digit value coems 37 | while (n != 0) { 38 | int rem = n % 10; 39 | n = n / 10; 40 | 41 | nextNum = nextNum + (int) Math.pow(rem, 2); 42 | } 43 | 44 | // Range : 1 - 9 45 | if (nextNum >= 1 && nextNum <= 9) { 46 | if (nextNum == 1 || nextNum == 7) // means Happy Number 47 | return true; 48 | else 49 | return false; 50 | } 51 | 52 | else 53 | n = nextNum; 54 | } 55 | } 56 | } 57 | // @lc code=end 58 | -------------------------------------------------------------------------------- /LeetCode_JAVA/206.reverse-linked-list.java: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode id=206 lang=java 3 | * 4 | * https://leetcode.com/problems/reverse-linked-list/ 5 | * 6 | * [206] Reverse Linked List 7 | */ 8 | 9 | // @lc code=start 10 | /** 11 | * Definition for singly-linked list. 12 | * public class ListNode { 13 | * int val; 14 | * ListNode next; 15 | * ListNode() {} 16 | * ListNode(int val) { this.val = val; } 17 | * ListNode(int val, ListNode next) { this.val = val; this.next = next; } 18 | * } 19 | */ 20 | class Solution { 21 | public ListNode reverseList(ListNode head) { 22 | ListNode curr = head, temp, newHead = null; 23 | 24 | while (curr != null) { 25 | temp = curr.next; // address 26 | curr.next = newHead; 27 | newHead = curr; 28 | curr = temp; 29 | } 30 | 31 | return newHead; 32 | } 33 | } 34 | // @lc code=end 35 | -------------------------------------------------------------------------------- /LeetCode_JAVA/21.merge-two-sorted-lists.java: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode id=21 lang=java 3 | * 4 | * [21] Merge Two Sorted Lists 5 | */ 6 | 7 | // @lc code=start 8 | /** 9 | * Definition for singly-linked list. 10 | * public class ListNode { 11 | * int val; 12 | * ListNode next; 13 | * ListNode() {} 14 | * ListNode(int val) { this.val = val; } 15 | * ListNode(int val, ListNode next) { this.val = val; this.next = next; } 16 | * } 17 | */ 18 | class Solution { 19 | public ListNode mergeTwoLists(ListNode list1, ListNode list2) { 20 | { 21 | ListNode dummy = new ListNode(-1); // null assigned to the dummy node. 22 | ListNode newStart = dummy; 23 | 24 | // Base Case 25 | if (list1 == null && list2 == null) 26 | return list1; // null 27 | 28 | // if(!list1 || !list2) return list1 ? list1 : list2; 29 | if (list1 == null) 30 | return list2; 31 | 32 | if (list2 == null) 33 | return list1; 34 | 35 | while (list1 != null && list2 != null) { 36 | if (list1.val <= list2.val) { 37 | dummy.next = list1; 38 | list1 = list1.next; 39 | } 40 | 41 | else if (list1.val > list2.val) { 42 | dummy.next = list2; 43 | list2 = list2.next; 44 | } 45 | 46 | dummy = dummy.next; // dummy also shifts 47 | } 48 | 49 | // Now if only 1 List remaining , means if List1 NOT FULLY TRAVERSED 50 | if (list1 != null) 51 | dummy.next = list1; 52 | if (list2 != null) 53 | dummy.next = list2; 54 | 55 | return newStart.next; 56 | } 57 | } 58 | } 59 | // @lc code=end 60 | -------------------------------------------------------------------------------- /LeetCode_JAVA/234.palindrome-linked-list.java: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode id=234 lang=java 3 | * 4 | * [234] Palindrome Linked List 5 | */ 6 | 7 | // @lc code=start 8 | /** 9 | * Definition for singly-linked list. 10 | * public class ListNode { 11 | * int val; 12 | * ListNode next; 13 | * ListNode() {} 14 | * ListNode(int val) { this.val = val; } 15 | * ListNode(int val, ListNode next) { this.val = val; this.next = next; } 16 | * } 17 | * 18 | * https://leetcode.com/problems/palindrome-linked-list/ 19 | * 20 | */ 21 | class Solution { 22 | 23 | public ListNode midPoint(ListNode head) { 24 | ListNode slow = head, fast = head; 25 | 26 | while (fast != null && fast.next != null && fast.next.next != null) { 27 | slow = slow.next; 28 | fast = fast.next.next; // Jump 2 step 29 | } 30 | 31 | return slow; 32 | } 33 | 34 | public ListNode reverseList(ListNode curr) { 35 | ListNode temp, newPtr = null; 36 | 37 | while (curr != null) { 38 | temp = curr.next; 39 | curr.next = newPtr; 40 | newPtr = curr; 41 | curr = temp; 42 | } 43 | 44 | return newPtr; 45 | } 46 | 47 | public boolean isPalindrome(ListNode head) { 48 | 49 | ListNode curr, newPtr = null; 50 | boolean result = false; 51 | 52 | // Mid - Point of the LinkedList 53 | ListNode mid = midPoint(head); 54 | 55 | // Reverse the Part of the LinkedList after K 56 | ListNode slow = reverseList(mid.next); // slow -> next 57 | 58 | // Check Left & Right HALF EQUAL OR NOT 59 | // START/HEAD ---> Left part , SLOW ---> Right part 60 | while (slow != null) { 61 | if (head.val != slow.val) 62 | return false; 63 | 64 | head = head.next; 65 | slow = slow.next; 66 | } 67 | 68 | return true; 69 | } 70 | } 71 | // @lc code=end 72 | 73 | /* 74 | * For Algorithm refer to copy 75 | */ 76 | 77 | /* ***** We can also use STACK , but then SPACE COMPLEXITY : O(n) ***** 78 | 79 | bool isPalindrome(ListNode* head) { 80 | stack s; 81 | ListNode* temp = head; 82 | while(temp!=NULL){ 83 | s.push(temp->val); 84 | temp = temp->next; 85 | } 86 | temp = head; 87 | while(temp!=NULL || !s.empty()){ 88 | int val = s.top(); 89 | s.pop(); 90 | if(val!=temp->val) return false; 91 | temp = temp->next; 92 | } 93 | return true; 94 | } 95 | */ -------------------------------------------------------------------------------- /LeetCode_JAVA/283.move-zeroes.java: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode id=283 lang=java 3 | * 4 | * [283] Move Zeroes 5 | * 6 | */ 7 | 8 | // @lc code=start 9 | class Solution { 10 | public void moveZeroes(int[] nums) { 11 | 12 | int len = nums.length, i, j, store = 0; 13 | 14 | if (nums[0] == 1 && len == 1) 15 | return; 16 | 17 | for (j = 0; j < len; j++) { 18 | if (nums[j] != 0) { 19 | nums[store] = nums[j]; 20 | store++; 21 | } 22 | } 23 | 24 | for (i = store; i < len; i++) 25 | nums[i] = 0; 26 | } 27 | } 28 | // @lc code=end 29 | -------------------------------------------------------------------------------- /LeetCode_JAVA/287.find-the-duplicate-number.java: -------------------------------------------------------------------------------- 1 | import java.util.Arrays; 2 | 3 | /* 4 | * @lc app=leetcode id=287 lang=java 5 | * 6 | * https://leetcode.com/problems/find-the-duplicate-number/ 7 | * 8 | * [287] Find the Duplicate Number 9 | */ 10 | 11 | // @lc code=start 12 | class Solution { 13 | public int findDuplicate(int[] nums) { 14 | 15 | int len = nums.length, i; 16 | Arrays.sort(nums); 17 | 18 | for (i = 1; i < len; i++) 19 | if (nums[i - 1] == nums[i]) 20 | return nums[i]; 21 | 22 | return -1; 23 | } 24 | } 25 | // @lc code=end 26 | -------------------------------------------------------------------------------- /LeetCode_JAVA/344.reverse-string.java: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode id=344 lang=java 3 | * 4 | * [344] Reverse String 5 | */ 6 | 7 | // @lc code=start 8 | class Solution { 9 | public void reverseString(char[] s) { 10 | int right = s.length - 1, left = 0; 11 | if (left >= right) 12 | return; 13 | 14 | while (left < right) { 15 | char temp = s[left]; 16 | s[left] = s[right]; 17 | s[right] = temp; 18 | 19 | left++; 20 | right--; 21 | } 22 | } 23 | } 24 | // @lc code=end 25 | -------------------------------------------------------------------------------- /LeetCode_JAVA/452.minimum-number-of-arrows-to-burst-balloons.java: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode id=452 lang=java 3 | * 4 | * [452] Minimum Number of Arrows to Burst Balloons 5 | */ 6 | 7 | // @lc code=start 8 | class Solution { 9 | public int findMinArrowShots(int[][] points) { 10 | 11 | int len = points.length, i, arrows = 1, prevEnd; 12 | if (len == 0) 13 | return 0; 14 | if (len == 1) 15 | return 1; 16 | 17 | Arrays.sort(points, (a, b) -> a[1] - b[1]); // Sort with the Ending Diameter 18 | 19 | prevEnd = points[0][1]; 20 | 21 | for (i = 0; i < len; i++) { 22 | if (points[i][0] <= prevEnd) 23 | continue; 24 | prevEnd = points[i][1]; 25 | arrows++; 26 | } 27 | 28 | return arrows; 29 | } 30 | } 31 | // @lc code=end 32 | -------------------------------------------------------------------------------- /LeetCode_JAVA/520.detect-capital.java: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode id=520 lang=java 3 | * 4 | * https://leetcode.com/problems/detect-capital/ 5 | * 6 | * [520] Detect Capital 7 | */ 8 | 9 | // @lc code=start 10 | class Solution { 11 | public boolean detectCapitalUse(String word) { 12 | 13 | int len = word.length(), i, count = 0; 14 | int firstletter = (int) word.charAt(0); 15 | 16 | if (len == 1) 17 | return true; 18 | 19 | for (i = 0; i < len; i++) { 20 | if ((int) word.charAt(i) >= 97 && (int) word.charAt(i) <= 122) 21 | count++; 22 | } 23 | 24 | // All capital OR all small 25 | if (count == 0 || count == len) 26 | return true; 27 | 28 | // means first letter in Capital 29 | if ((firstletter >= 65 && firstletter <= 90) && count == len - 1) 30 | return true; 31 | 32 | return false; 33 | } 34 | } 35 | // @lc code=end 36 | -------------------------------------------------------------------------------- /LeetCode_JAVA/605.can-place-flowers.java: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode id=605 lang=java 3 | * 4 | * https://leetcode.com/problems/can-place-flowers/ 5 | * 6 | * [605] Can Place Flowers 7 | */ 8 | 9 | // @lc code=start 10 | class Solution { 11 | public boolean canPlaceFlowers(int[] flowerbed, int n) { 12 | 13 | int len = flowerbed.length, i, count = 0; 14 | 15 | for (i = 0; i < len; i++) { 16 | 17 | // Boundary Case + Condition 18 | if ((i == 0 || flowerbed[i - 1] == 0) && (i == len - 1 || flowerbed[i + 1] == 0) && flowerbed[i] == 0) { 19 | count++; 20 | flowerbed[i] = 1; 21 | i++; // Shifts by 2 22 | } 23 | } 24 | 25 | if (n <= count) 26 | return true; 27 | 28 | return false; 29 | } 30 | } 31 | // @lc code=end 32 | 33 | /* 34 | Test Cases Sample : 35 | 36 | 1) [0,0,1,0,1] , n = 1 true 37 | 2) [1,0,0,0,0,1] , n = 2 false 38 | */ -------------------------------------------------------------------------------- /LeetCode_JAVA/61.rotate-list.java: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode id=61 lang=java 3 | * 4 | * [61] Rotate List 5 | */ 6 | 7 | // @lc code=start 8 | /** 9 | * Definition for singly-linked list. 10 | * public class ListNode { 11 | * int val; 12 | * ListNode next; 13 | * ListNode() {} 14 | * ListNode(int val) { this.val = val; } 15 | * ListNode(int val, ListNode next) { this.val = val; this.next = next; } 16 | * } 17 | */ 18 | class Solution { 19 | public ListNode rotateRight(ListNode head, int k) { 20 | // edge case 21 | if (head == null || k == 0) 22 | return head; 23 | 24 | ListNode curr = head; 25 | int len = 1; 26 | 27 | // Lenght of the LinkedList 28 | while (curr.next != null) { 29 | curr = curr.next; 30 | len++; 31 | } 32 | 33 | // len++; 34 | k = k % len; 35 | k = len - k; 36 | 37 | System.out.println(len); 38 | 39 | // Circular Linkedlist 40 | curr.next = head; 41 | 42 | // Remaining K - times Shift 43 | while (k-- > 0) 44 | curr = curr.next; 45 | 46 | head = curr.next; 47 | curr.next = null; 48 | 49 | return head; 50 | } 51 | } 52 | // @lc code=end 53 | -------------------------------------------------------------------------------- /LeetCode_JAVA/849.maximize-distance-to-closest-person.java: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode id=849 lang=java 3 | * 4 | * [849] Maximize Distance to Closest Person 5 | */ 6 | 7 | // @lc code=start 8 | class Solution { 9 | public int maxDistToClosest(int[] seats) { 10 | 11 | int i, len = seats.length, prevFull = -1, futureFull = 0, result = 0; 12 | 13 | for (i = 0; i < len; i++) { 14 | if (seats[i] == 1) 15 | prevFull = i; 16 | 17 | else { 18 | // Get the NEXT Full seat 19 | while (futureFull < len && seats[futureFull] == 0 || futureFull < i) 20 | futureFull++; 21 | 22 | int left = prevFull == -1 ? len : i - prevFull; // 23 | int right = futureFull == len ? len : futureFull - i; // Right Gap/Distance to reach the NEXT filled chai 24 | result = Math.max(result, Math.min(left, right)); 25 | } 26 | } 27 | 28 | return result; 29 | } 30 | } 31 | // @lc code=end 32 | -------------------------------------------------------------------------------- /LeetCode_JAVA/875.koko-eating-bananas.java: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode id=875 lang=java 3 | * 4 | * [875] Koko Eating Bananas 5 | * 6 | * https://leetcode.com/problems/koko-eating-bananas/ 7 | * 8 | */ 9 | 10 | // @lc code=start 11 | class Solution { 12 | // BONARY SEARCH APPROACH 13 | public int minEatingSpeed(int[] piles, int h) { 14 | 15 | int max = Arrays.stream(piles).max().getAsInt(); 16 | // System.out.println(max); 17 | int end = max, start = 1, mid = 0; 18 | 19 | while (end >= start) { 20 | mid = (start + end) / 2; 21 | 22 | if (isPossibleToEat(piles, mid, h)) // If possible then try Checking with the less one 23 | end = mid - 1; 24 | else 25 | start = mid + 1; 26 | } 27 | return start; 28 | } 29 | 30 | public boolean isPossibleToEat(int[] piles, int mid, int hrs) { 31 | int i = 0, totalHrs = 0, len = piles.length, divide = 0; 32 | 33 | for (i = 0; i < len; i++) { 34 | divide = piles[i] / mid; // 7 / 3 = 2 35 | totalHrs = totalHrs + divide; 36 | 37 | if (piles[i] % mid != 0) // 7 % 3 = 1 (remainder) so 1 more totalHrs required to complete Rest. 38 | totalHrs++; 39 | } 40 | 41 | return totalHrs > hrs ? false : true; 42 | } 43 | } 44 | // @lc code=end 45 | -------------------------------------------------------------------------------- /LeetCode_JAVA/876.middle-of-the-linked-list.java: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode id=876 lang=java 3 | * 4 | * [876] Middle of the Linked List 5 | */ 6 | 7 | // @lc code=start 8 | /** 9 | * Definition for singly-linked list. 10 | * public class ListNode { 11 | * int val; 12 | * ListNode next; 13 | * ListNode() {} 14 | * ListNode(int val) { this.val = val; } 15 | * ListNode(int val, ListNode next) { this.val = val; this.next = next; } 16 | * } 17 | */ 18 | 19 | class Solution { 20 | public ListNode middleNode(ListNode head) { 21 | 22 | // Slow & Fast Pointer Approach 23 | ListNode slow = head, fast = head; 24 | 25 | while( fast != null && fast.next != null){ 26 | slow = slow.next; 27 | fast = fast.next.next; 28 | } 29 | 30 | return slow; 31 | } 32 | } 33 | // @lc code=end 34 | 35 | /* 36 | METHOD - 1 :: 37 | 38 | Time Complexity : O(n) + O(n/2) 39 | 40 | 1) Loop though the LinkedList , COUNT ++ , find the Len , (Len / 2 + 1) position. 41 | 2) Now again iterate and pauses when the Iterator reaches the above position 42 | 43 | 44 | METHOD - 2 :: ***** OPTIMIZED ***** 45 | 46 | Time Complexity : O(n/2) 47 | 48 | 1) Use 2 Pointer Approach , Hare & Tortoise / Slow & Fast Pointer 49 | 2) Hare moves +2 steps , and Tortoise moves +1 steps. 50 | */ -------------------------------------------------------------------------------- /LeetCode_JAVA/917.reverse-only-letters.java: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode id=917 lang=java 3 | * 4 | * [917] Reverse Only Letters 5 | * 6 | * https://leetcode.com/problems/reverse-only-letters/ 7 | * 8 | */ 9 | 10 | // @lc code=start 11 | class Solution { 12 | public String reverseOnlyLetters(String s) { 13 | 14 | char ch[] = s.toCharArray(); 15 | int right = s.length() - 1, left = 0; 16 | 17 | while (left < right) { 18 | if (Character.isAlphabetic(ch[left]) && Character.isAlphabetic(ch[right])) // Both ALPHABET 19 | { 20 | char temp = ch[left]; 21 | ch[left] = ch[right]; 22 | ch[right] = temp; 23 | 24 | right--; 25 | left++; 26 | } 27 | 28 | else if (!Character.isAlphabetic(ch[left])) // Both NOT ALPHABET 29 | left++; 30 | 31 | else if (!Character.isAlphabetic(ch[right])) // Left --> NOT ALPHABET , Right --> ALPHABET 32 | right--; 33 | } 34 | 35 | return new String(ch); 36 | } 37 | } 38 | // @lc code=end 39 | -------------------------------------------------------------------------------- /LeetCode_JAVA/941.valid-mountain-array.java: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode id=941 lang=java 3 | * 4 | * https://leetcode.com/problems/valid-mountain-array/ 5 | * 6 | * [941] Valid Mountain Array 7 | */ 8 | 9 | // @lc code=start 10 | class Solution { 11 | public boolean validMountainArray(int[] arr) { 12 | 13 | int len = arr.length, i = 0; 14 | int MaxElement = Arrays.stream(arr).max().getAsInt(); 15 | 16 | if (len < 3) 17 | return false; 18 | 19 | // Start can't be more 20 | if (arr[0] > arr[1]) 21 | return false; 22 | 23 | // Upward the slope 24 | while (i + 1 < len && arr[i] < arr[i + 1]) 25 | i++; 26 | 27 | if (i == 0 || i == len - 1) 28 | return false; 29 | 30 | // Down the slope 31 | while (i + 1 < len && arr[i] > arr[i + 1]) 32 | i++; 33 | 34 | return (i == len - 1) ? true : false; 35 | } 36 | } 37 | // @lc code=end 38 | -------------------------------------------------------------------------------- /LeetCode_JAVA/997.find-the-town-judge.java: -------------------------------------------------------------------------------- 1 | /* 2 | * @lc app=leetcode id=997 lang=java 3 | * 4 | * [997] Find the Town Judge 5 | */ 6 | 7 | // @lc code=start 8 | class Solution { 9 | 10 | // trust[i] = [ai, bi] representing that the person labeled ai TRUSTS the person labeled bi. 11 | public int findJudge(int n, int[][] trust) { 12 | 13 | int trustedCount[] = new int[n + 1]; 14 | int trustByCount[] = new int[n + 1]; 15 | int i, len = trust.length; 16 | 17 | if (len == 1) 18 | return trust[0][1]; 19 | 20 | for (i = 0; i < len; i++) { 21 | trustByCount[trust[i][0]]++; // person who trust others 22 | trustedCount[trust[i][1]]++; // person who is being Trusted 23 | } 24 | 25 | /* 26 | Check : the person who will be the town judge will NOT TRUST OTHERS trustedByCount[i] == 0 , 27 | but he is trusted by EVERYONE (n - 1) person. 28 | */ 29 | for (i = 1; i <= n; i++) { 30 | if (trustedCount[i] == (n - 1) && trustByCount[i] == 0) 31 | return i; 32 | } 33 | 34 | return -1; 35 | } 36 | } 37 | // @lc code=end 38 | -------------------------------------------------------------------------------- /LeetCode_JAVA/FUNCTIONS_JAVA.txt: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 1) ARRAYS =====> -------------------------------------------------------------------------------- /java-practice/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/niharika2k00/Java/abe9fc7615a5ff3852a90efd2230f29d569dbc68/java-practice/.DS_Store -------------------------------------------------------------------------------- /java-practice/Abstraction.java: -------------------------------------------------------------------------------- 1 | 2 | // Abstract Class can contains everything abstruct method(MUST) , non-abstruct method , constructor , etc. 3 | /* 4 | To access the abstract class, it must be inherited from another class. 5 | 6 | Abstract Class: is a RESTRICTED class object(Instances) cannot be created to access it, it must be INHERITED from another Class. 7 | Abstract Method: can only be used in an abstract class, and it DOES NOT have a body. The body is provided by the subclass (inherited class). 8 | */ 9 | abstract class Bike { 10 | Bike() { 11 | System.out.println("== Bike Created =="); 12 | } 13 | 14 | abstract void run(); // only method signature 15 | 16 | abstract String display(); 17 | 18 | void changeGear() { 19 | System.out.println("Gear changed"); 20 | } 21 | } 22 | 23 | //Creating a Child class which inherits Abstract class 24 | class Honda extends Bike { 25 | 26 | Honda() { 27 | System.out.println("honda constructor called"); 28 | } 29 | 30 | void run() { 31 | System.out.println("Running safely.."); 32 | } 33 | 34 | String display() { 35 | 36 | System.out.println("Inside the display method."); 37 | 38 | super.changeGear(); 39 | return "\n Finally everything working fine with response 200.\n"; 40 | } 41 | 42 | // void changeGear() { 43 | // System.out.println("Honda's Gear changed"); 44 | // } 45 | } 46 | 47 | public class Abstraction { 48 | 49 | public static void main(String args[]) { 50 | Bike obj = new Honda(); // ParentClass obj = new ChildClass 51 | 52 | // accessing the methods of the child class, if not present then access its Parent Class 53 | obj.run(); 54 | obj.changeGear(); 55 | System.out.println(obj.display()); 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /java-practice/Abstraction_gfg.java: -------------------------------------------------------------------------------- 1 | abstract class Shape { 2 | String color; 3 | 4 | // these are abstract methods 5 | abstract double calcArea(); 6 | 7 | public abstract String toString(); 8 | 9 | // abstract class can have the constructor 10 | public Shape(String color) { 11 | System.out.println("Shape constructor called"); 12 | this.color = color; 13 | } 14 | 15 | // non-abstract so contains body 16 | public String getColor() { 17 | return color; 18 | } 19 | } 20 | 21 | class Circle extends Shape { 22 | double radius; 23 | 24 | public Circle(String color, double radius) { 25 | super(color); // calling Shape constructor 26 | System.out.println("Circle constructor called"); 27 | this.radius = radius; 28 | } 29 | 30 | @Override 31 | double calcArea() { 32 | return Math.round((Math.PI * Math.pow(radius, 2)) * 100) / 100.0; // pi r^2 33 | } 34 | 35 | @Override 36 | public String toString() { 37 | return "Circle color is " + super.getColor() + " and area is : " + calcArea(); 38 | } 39 | } 40 | 41 | class Rectangle extends Shape { 42 | 43 | double length; 44 | double width; 45 | 46 | public Rectangle(String color, double length, double width) { 47 | // calling Shape constructor 48 | super(color); 49 | System.out.println("Rectangle constructor called"); 50 | this.length = length; 51 | this.width = width; 52 | } 53 | 54 | @Override 55 | private double calcArea() { 56 | return length * width; 57 | } 58 | 59 | @Override 60 | public String toString() { 61 | return "Rectangle color is " + super.getColor() + " and area is : " + calcArea(); 62 | } 63 | } 64 | 65 | public class Abstraction_gfg { 66 | public static void main(String[] args) { 67 | Shape s1 = new Circle("Red", 2.2); 68 | Shape s2 = new Rectangle("Yellow", 2, 4); 69 | 70 | System.out.println(s1.toString()); 71 | // System.out.println(s2.toString()); 72 | 73 | // PRIVATE method CANNOT be accessible from other class. But PROTECTED , FINAL , PUBLIC can be accessible. 74 | System.out.println("Area of the Rectangle : " + s2.calcArea()); 75 | } 76 | } 77 | -------------------------------------------------------------------------------- /java-practice/Access_Modifier.java: -------------------------------------------------------------------------------- 1 | 2 | class A { 3 | // Private : Can't possible to access from the Main fn., as its scope is RESTRICTED within the Class 4 | 5 | protected void log() { 6 | System.out.println("Log of Class-A"); 7 | } 8 | } 9 | 10 | class Check { 11 | int a = 0, b = 0; 12 | } 13 | 14 | public class Access_Modifier { 15 | 16 | public static void main(String[] args) { 17 | // A obj = new A(); 18 | // obj.log(); 19 | 20 | Check c1 = new Check(); 21 | Check c2 = new Check(); 22 | 23 | c2 = c1; 24 | c1.a = 12; 25 | c2.b = 34; 26 | System.out.println(" c1.a = " + c1.a + " c1.b = " + c1.b + " c2.a = " + c2.a + " c2.b = " + c2.b); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /java-practice/Array_List.java: -------------------------------------------------------------------------------- 1 | 2 | import java.io.*; 3 | import java.util.*; 4 | import java.util.ArrayList; 5 | 6 | import java.io.BufferedReader; 7 | import java.io.IOException; 8 | import java.io.InputStreamReader; 9 | import java.util.Scanner; 10 | 11 | public class Array_List { 12 | public static void main(String[] args) throws IOException { 13 | 14 | int n = 5, i; 15 | Manupulation Obj = new Manupulation(); 16 | Scanner sc = new Scanner(System.in); 17 | BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 18 | 19 | // --------------------------------------------------------------------- 20 | // ArrayList 21 | // --------------------------------------------------------------------- 22 | ArrayList arrList_A = new ArrayList(n); // similar to vector, ie its a dynamic array. 23 | 24 | for (i = 0; i < n; i++) // Append 25 | arrList_A.add(i); 26 | 27 | System.out.println(arrList_A); 28 | arrList_A.remove(3); 29 | System.out.println(arrList_A); 30 | 31 | Obj.printElement(arrList_A); 32 | 33 | // --------------------------------------------------------------------- 34 | // User Input uisng ArrayList 35 | // --------------------------------------------------------------------- 36 | ArrayList vec = new ArrayList(4); 37 | System.out.println("\n\nEnter elements for ArrayList : "); 38 | for (int j = 0; j <= 3; j++) { 39 | // int ele = sc.nextInt(); 40 | int ele = Integer.parseInt(br.readLine()); 41 | vec.add(ele); 42 | } 43 | 44 | Obj.printElement(vec); 45 | 46 | // --------------------------------------------------------------------- 47 | // String of Array using ArrayList 48 | // --------------------------------------------------------------------- 49 | ArrayList stringVec = new ArrayList<>(); 50 | stringVec.add("Computer"); 51 | stringVec.add("Engineering"); 52 | stringVec.add(1, "Science"); 53 | 54 | System.out.println(); 55 | for (String str : stringVec) 56 | System.out.print(str + " "); 57 | } 58 | } 59 | 60 | class Manupulation { 61 | 62 | // Printing all elements 63 | public void printElement(ArrayList arrList) { 64 | int i, len = arrList.size();; 65 | System.out.println("\nElements in the array : "); 66 | 67 | for (i = 0; i < len; i++) 68 | System.out.print(arrList.get(i) + " "); 69 | System.out.println(); 70 | 71 | for (int ele : arrList) { 72 | System.out.print(ele + " "); 73 | } 74 | } 75 | 76 | } -------------------------------------------------------------------------------- /java-practice/Autoboxing.java: -------------------------------------------------------------------------------- 1 | 2 | public class Autoboxing { 3 | 4 | public static void main(String[] args) { 5 | 6 | // Auto boxing --> Primitive data type converted to Wrapper Class. 7 | char ch = 'N'; 8 | Character a = ch; 9 | 10 | // Unboxing the Object 11 | Integer i = new Integer(10); 12 | int primitiveInt = i; 13 | 14 | System.out.println("Value of i = " + i + "\nValue of primitiveInt = " + primitiveInt); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /java-practice/ConstructorOverloading.java: -------------------------------------------------------------------------------- 1 | 2 | /* 3 | CONSTRUCTOR OVERLOADING 4 | _____________________________ 5 | 6 | - Same Name as Class Name 7 | - Different Parameters 8 | */ 9 | 10 | class ConstructorOverloading { 11 | 12 | ConstructorOverloading() { 13 | System.out.println("This is a Self - Constructor"); 14 | } 15 | 16 | public static void main(String[] args) { 17 | System.out.println("\nHello Im the Main Function"); 18 | 19 | // Constructor 20 | ConstructorOverloading Object0 = new ConstructorOverloading(); // Default Self Constructor 21 | House Object1 = new House(10); 22 | House Object2 = new House(8, "Crimson"); 23 | 24 | System.out.println(Object1); 25 | 26 | House ObjAppend = new House(); // Calls the default constructor 27 | String ans = ObjAppend.Join("Java ", "is an OOP Languages. "); 28 | System.out.println(ans); 29 | } 30 | } 31 | 32 | class House { 33 | // Non-Parameterized constructor 34 | House() { 35 | System.out.println("\nI'm the DEFAULT CONSTRUCTOR of Class House.\n"); 36 | } 37 | 38 | // Parameterized Constructor 39 | public House(int x) { 40 | System.out.println("Received " + x + " as props."); 41 | } 42 | 43 | House(int x, String color) { 44 | System.out.println(x + " " + color + " Curtains"); 45 | } 46 | 47 | public House(int i, String string, int j) {} 48 | 49 | public House(int i, String string, String string2) {} 50 | 51 | public String Join(String a, String b) { 52 | return a.concat(b); 53 | } 54 | 55 | public void getLocation() {} 56 | } 57 | -------------------------------------------------------------------------------- /java-practice/Constructor_Chaining.java: -------------------------------------------------------------------------------- 1 | 2 | // https://www.geeksforgeeks.org/constructor-chaining-java-examples/?ref=lbp 3 | 4 | // Constructor Chaining 5 | 6 | public class Constructor_Chaining { 7 | 8 | // default constructor 1 9 | // default constructor will call another constructor using this keyword from same class 10 | Constructor_Chaining() { 11 | this(5); // calls constructor 2 12 | System.out.println("The Default constructor"); 13 | } 14 | 15 | // parameterized constructor 2 16 | Constructor_Chaining(int x) { 17 | this(5, 15); // calls constructor 3 18 | System.out.println(x); 19 | } 20 | 21 | // parameterized constructor 3 22 | Constructor_Chaining(int x, int y) { 23 | System.out.println(x * y); 24 | } 25 | 26 | public static void main(String args[]) { 27 | // invokes default constructor first 28 | new Constructor_Chaining(); 29 | } 30 | } 31 | 32 | // super() -> keywords used to call the constructor of the Parent Class 33 | // this() -> keywords used to call the constructor of itSelf -------------------------------------------------------------------------------- /java-practice/Encapsulation_OOP.java: -------------------------------------------------------------------------------- 1 | class Encap { 2 | // private var can only be accessed by METHODS of the same class. 3 | private String geekName; 4 | private int geekRoll; 5 | private int geekAge; 6 | 7 | // get method for age to access 8 | // private variable geekAge 9 | public int getAge() { 10 | return geekAge; 11 | } 12 | 13 | // get method for name to access 14 | // private variable geekName 15 | public String getName() { 16 | return geekName; 17 | } 18 | 19 | // get method for roll to access 20 | // private variable geekRoll 21 | public int getRoll() { 22 | return geekRoll; 23 | } 24 | 25 | // set method for age to access 26 | // private variable geekage 27 | public void setAge(int newAge) { 28 | geekAge = newAge; 29 | } 30 | 31 | // set method for name to access 32 | // private variable geekName 33 | public void setName(String newName) { 34 | geekName = newName; 35 | } 36 | 37 | // set method for roll to access 38 | // private variable geekRoll 39 | public void setRoll(int newRoll) { 40 | geekRoll = newRoll; 41 | } 42 | } 43 | 44 | public class Encapsulation_OOP { 45 | 46 | public static void main(String[] args) { 47 | Encap obj = new Encap(); 48 | 49 | // setting values of the variables 50 | obj.setName("Ms. Mathew"); 51 | obj.setAge(69); 52 | obj.setRoll(12378562); 53 | 54 | // Displaying values of the variables 55 | System.out.println("Employee's name: " + obj.getName()); 56 | System.out.println("Employee's age: " + obj.getAge()); 57 | System.out.println("Employee's ID: " + obj.getRoll()); 58 | } 59 | } -------------------------------------------------------------------------------- /java-practice/Enum.java: -------------------------------------------------------------------------------- 1 | 2 | import java.util.Scanner; 3 | 4 | // An Enum class 5 | enum Day { 6 | SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY; 7 | } 8 | 9 | public class Enum { 10 | Day name; 11 | 12 | // Constructor 13 | public Enum(Day name) { 14 | this.name = name; 15 | } 16 | 17 | public void dayIsLike() { 18 | switch (name) { 19 | case MONDAY: 20 | System.out.println("Mondays are bad. \n"); 21 | break; 22 | 23 | case FRIDAY: 24 | System.out.println("Fridays are better. \n"); 25 | break; 26 | 27 | case SATURDAY: 28 | 29 | case SUNDAY: 30 | System.out.println("Weekends are best. \n"); 31 | break; 32 | 33 | default: 34 | System.out.println("Midweek days are so-so. \n"); 35 | break; 36 | } 37 | } 38 | 39 | public static void main(String[] args) { 40 | 41 | String str = "MONDAY"; 42 | Enum obj = new Enum(Day.valueOf(str)); 43 | obj.dayIsLike(); 44 | 45 | // all values 46 | Day arr[] = Day.values(); 47 | for (Day it : arr) 48 | System.out.println(it + " at index " + it.ordinal()); 49 | 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /java-practice/FUNCTIONS_JAVA.txt: -------------------------------------------------------------------------------- 1 | 2 | JAVA 3 | _____________ 4 | 5 | Collections class is a member of the Java Collections Framework. The java.util.Collections package is the package that contains the Collections class. 6 | 7 | 8 | ___________________________________ 9 | // Array Initialization 1D & 2D 10 | ----------------------------------- 11 | 12 | int arr[] = new int[n + 1]; 13 | For array and String ===> length() 14 | 15 | ___________________________________ 16 | // Sorting a 2D array 17 | ----------------------------------- 18 | 19 | Arrays.sort(contests, (a, b) -> Integer.compare(b[0],a[0])); //decreasing order 20 | 21 | Arrays.sort(contests, (a, b) -> Integer.compare(a[0],b[0]); //increasing order 22 | Arrays.sort(myArr, (a, b) -> a[0] - b[0]); 23 | 24 | 25 | int MaxElement = Arrays.stream(piles).max().getAsInt(); 26 | 27 | 28 | ___________________________________ 29 | // Iterating a 2D array 30 | ----------------------------------- 31 | 32 | for(int i=0; i 54 | Scanner sc = new Scanner(System.in); 55 | sc.nextInt(); 56 | sc.nextInt() 57 | nextBoolean() 58 | nextFloat() 59 | 60 | 61 | 2) BUFFER READER CLASS =====> 62 | BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 63 | String str = br.ReadLine(); 64 | int read() <- It is used for reading a single character. 65 | int ele = Integer.parseInt(br.readLine()); // take Integer Input 66 | 67 | 68 | (Unsynchronized) - fast 69 | 3) ARRAY_LIST =====> 70 | ArrayList arrLi = new ArrayList(n); 71 | 72 | .size() 73 | arrLi.add(ele) 74 | arrLi.add(index , element) 75 | .addAll( Collection c) // arguement having another arr that will append with the Original oen. 76 | addAll(Index , Collection) 77 | .remove(index) 78 | .remove(object) // Ex arrLi.remove("Summer") ; removing word from List 79 | .get(integer) 80 | .contains() 81 | 82 | 83 | Sort Ascending : Collections.sort(list); 84 | Sort Reverse (Descending) : Collections.sort(list, Collections.reverseOrder()); 85 | 86 | 87 | 88 | 4) VECTOR ======> 89 | 90 | 91 | (Unsynchronized) - FAST as multiple threads can access the code at a SAME TIME 92 | 5) HASH MAP ======> 93 | Syntax : Hashtable ht = new Hashtable(); 94 | 95 | HashMap map = new HashMap<>(); 96 | 97 | .size() 98 | .put(K key, V value) 99 | .containsValue(Object value) 100 | .containsKey(Object key) 101 | .elements() 102 | .get(Object key) 103 | .remove​(Object key, Object value) 104 | .replace(K key, V value) 105 | .values() 106 | 107 | 108 | for (i = 0; i < len; i++) { 109 | if (hashMap.containsValue(nums[i])) 110 | hashMap.put(i, nums[i]); // GET the previous Count + 1 111 | else 112 | hashMap.put(nums[i], 1); 113 | } 114 | 115 | 116 | ### Assigning array into HashMap 117 | ------------------------------------------ 118 | HashMap map = new HashMap<>(); 119 | for (i = 0; i < len; i++) 120 | map.put(nums[i], map.getOrDefault(nums[i], 0) + 1); // put(K key, V value) 121 | 122 | ### Iterate through the map 123 | --------------------------------- 124 | for (int it : map.keySet()) { 125 | if (k > 0 && map. (it + k) || k == 0 && map.get(it) > 1) 126 | 127 | 128 | for (Object value : map.values()) { 129 | // ... 130 | } 131 | 132 | 133 | 6) LINKED - LIST ========> Node new_node = new Node(new_data); 134 | 135 | 136 | 137 | 138 | 139 | 140 | 141 | -------------------------------------------------------------------------------- /java-practice/FirstProgram.java: -------------------------------------------------------------------------------- 1 | 2 | // import java.util.*; 3 | import java.util.Scanner; // Import the Scanner class 4 | 5 | class Main { 6 | 7 | // Function OR Static Method 8 | public static int Greater(int a, int b) { 9 | int result = a > b ? a : b; 10 | return result; 11 | } 12 | 13 | public static void main(String[] args) { 14 | 15 | Scanner scObj = new Scanner(System.in); // Create a Scanner object -- .in --> input from the keyboard 16 | System.out.println("Enter Name and Age : "); 17 | System.out.println("Boolean Result: " + scObj.hasNextInt()); // checks 18 | 19 | // Read User Input : String & Integer 20 | String userName = scObj.nextLine(); 21 | int age = scObj.nextInt(); 22 | 23 | System.out.println("Username is: " + userName + " , age: " + age); // Output 24 | 25 | int res = Greater(18, 100); 26 | System.out.println("Greater no. = " + res); 27 | 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /java-practice/Hash_Map.java: -------------------------------------------------------------------------------- 1 | 2 | import java.io.*; 3 | import java.util.*; 4 | 5 | public class Hash_Map { 6 | public static void main(String[] args) { 7 | 8 | // Create an empty hash map by declaring object of string and integer type without Generics 9 | HashMap map = new HashMap<>(); 10 | 11 | // using standard add() method 12 | map.put("Postman", 10); 13 | map.put("JUnit", 30); 14 | map.put("Swagger", 20); 15 | 16 | System.out.println("Size of map is => " + map.size()); 17 | System.out.println("Hash_Map => " + map.values()); 18 | 19 | // Enumeration enu = map.keys(); 20 | // System.out.println("\nThe enumeration of keys are:"); 21 | 22 | // while (enu.hasMoreElements()) 23 | // System.out.println(enu.nextElement()); 24 | 25 | if (map.containsKey("JUnit")) { 26 | Integer val = map.get("JUnit"); 27 | System.out.println("\nValue for key" + " \"JUnit\" => " + val); 28 | } 29 | 30 | // Iterating Hash Map in various ways 31 | for (String name : map.keySet()) // ✅ 32 | System.out.println("key: " + name); 33 | 34 | for (int view : map.values()) // ✅ 35 | System.out.println("value: " + view); 36 | 37 | for (Map.Entry e : map.entrySet()) 38 | System.out.println("Key: " + e.getKey() + "\t Value: " + e.getValue()); 39 | 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /java-practice/Inheritance_Chaining.java: -------------------------------------------------------------------------------- 1 | 2 | // filename Main.java 3 | class Grandparent { 4 | public void Print() { 5 | System.out.println("Grandparent's Print()"); 6 | } 7 | } 8 | 9 | class Parent extends Grandparent { 10 | public void Print() { 11 | super.Print(); 12 | System.out.println("Parent's Print()"); 13 | } 14 | } 15 | 16 | class Child extends Parent { 17 | public void Print() { 18 | super.Print(); // calls its parent class 19 | System.out.println("Child's Print()"); 20 | } 21 | } 22 | 23 | public class Inheritance_Chaining { 24 | public static void main(String[] args) { 25 | Child c = new Child(); 26 | c.Print(); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /java-practice/Inheritance_OOP.java: -------------------------------------------------------------------------------- 1 | // https://www.javatpoint.com/inheritance-in-java 2 | public class Inheritance_OOP { 3 | public static void main(String[] args) { 4 | B obj = new B(); // Create an Object of class B 5 | int len = obj.joinString("\nHappy ", "Coding !"); 6 | System.out.println("Concatenated String Length: " + len); 7 | 8 | B obj1 = new B(10); 9 | obj1.display(); 10 | } 11 | } 12 | 13 | // Parent / Super Class 14 | class A { 15 | protected int x = 7400; 16 | 17 | public A() { 18 | System.out.println("\nClass A constructor "); 19 | } 20 | 21 | public A(int num) { 22 | System.out.println("\nParameterized Constructor of Class A " + num); 23 | } 24 | 25 | public int joinString(String s1, String s2) { 26 | System.out.println(s1.concat(s2)); 27 | return s1.length() + s2.length(); 28 | } 29 | 30 | public void display() { 31 | System.out.println("Displaying in ** A **"); 32 | } 33 | 34 | public void method1() { 35 | System.out.println("Accessing the method of the Parent Class using Super Keyword"); 36 | } 37 | } 38 | 39 | // Child / Sub Class 40 | class B extends A { 41 | public B() { 42 | System.out.println("Class B constructor "); 43 | } 44 | 45 | public B(int num) { 46 | super(num); // calls the Super Class constructor 47 | super.method1(); 48 | System.out.println("Parameterized Constructor of Class B " + num); 49 | } 50 | 51 | public void display() { 52 | System.out.println("Displaying in ** B **"); 53 | System.out.println("Accessing Protected Variable " + x + " from Parent Class"); 54 | } 55 | } 56 | 57 | /* 58 | * -- when we are create & call the OBJ of SubClass ==> then it calls the (SuperClass + SubClass) Default Const. 59 | * -- use Super(parameter); to call the parameterized constructor for Super Class 60 | */ 61 | -------------------------------------------------------------------------------- /java-practice/InterfaceTest.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.lang.*; 3 | import java.util.*; 4 | 5 | // Level 1 6 | interface Bank { 7 | void deposit(); 8 | 9 | void withdraw(); 10 | 11 | void loan(); 12 | 13 | void account(); 14 | } 15 | 16 | // Level 2 17 | abstract class Dev1 implements Bank { 18 | public void deposit() { 19 | System.out.println("Your deposit Amount :" + 100); 20 | } 21 | } 22 | 23 | abstract class Dev2 extends Dev1 { 24 | public void withdraw() { 25 | System.out.println("Your withdraw Amount :" + 50); 26 | } 27 | } 28 | 29 | // Level 3 30 | class Dev3 extends Dev2 { 31 | public void loan() {} 32 | 33 | public void account() {} 34 | } 35 | 36 | public class InterfaceTest { 37 | public static void main(String[] args) { 38 | Dev3 d = new Dev3(); 39 | d.account(); 40 | d.loan(); 41 | d.deposit(); 42 | d.withdraw(); 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /java-practice/JAVA.txt: -------------------------------------------------------------------------------- 1 | 2 | ---------------------- JAVA --------------------- 3 | 4 | 1) Class Name should be same as File name 5 | 2) If we use 6 | class ABC (that contains psvm) then file name can be either ABC.java or anything.java 7 | 8 | 3) If we use 9 | public class ABC (that contains psvm) then file name MUST BE ABC.java 10 | 11 | 4) class ABC <---- present, its a Default Class(means having "default" access modifier) 12 | 13 | 14 | Modifier Package Subclass World 15 | 16 | public Yes Yes Yes 17 | 18 | protected Yes Yes No 19 | 20 | Default (no 21 | modifier) Yes No No 22 | 23 | private No No No 24 | 25 | 26 | 5) Constructor 27 | - Special Methods Used to allocate/initialise memory for OBJECT 28 | - JVM provides a Default constructor of class ABC always present 29 | - Constructor has Same Name as the Class Name 30 | - Doesn't Return Anything 31 | - Called only ONCE at the time of Obj Creation 32 | - Creates an instance(Obj) in the Heap Memory 33 | 34 | While Creating OBJECT of a certain CLASS using new() keyword a default constructor is always CALLED.(below ex) 35 | Student obj = new Student(); 36 | 37 | While making an Obj of Class B that extends A then it will call the constructor of Class A + Class B both. 38 | Calling Child Class - Constructor of Parent and Constructor of Child executes. 39 | runs sequence: ConstChild + ConstParent 40 | 41 | 7) If 2 Class Present then you have to use public keyboard before the class having MAIN Method, then name the file with that 42 | name only(otherwise it will get confuse which class is having the MAIN Method). 43 | 44 | 45 | 8) FEATURES :: 46 | - Platform-Independent ----> is that the Java Compiled Code(byte code) can run on all operating systems . 47 | - Multi Threaded ----> Ability to run multiple threads concurrently at the same time . 48 | - Portable ----> to carry the Java bytecode to any platform. 49 | - Encapsulation ----> Wrapping up of data and information under a single unit 50 | - Polymorphism ----> Method Overriding( RunTime Time Polymorphism ) + Method OverLoading( CompileTime Time Polymorphism ) 51 | - Inheritance ----> Child/SubClass inherits the METHODS of the Parent Class 52 | 53 | 54 | 9) Static Method in Java --> * Access static method() and fields using Class Name [ ClassName.methodName(args) ] 55 | * NO NEED to Create Object/Instance of the Class 56 | * We cannot access/print Non-Static Variables inside Static Methods 57 | 58 | https://www.geeksforgeeks.org/static-keyword-java/?ref=lbp 59 | 60 | https://www.geeksforgeeks.org/can-we-overload-or-override-static-methods-in-java/ 61 | 62 | 63 | 64 | 10) Aggregation ---> HAS A Relationship 65 | Composition ---> IS A PART OF Relationship (both dependent) 66 | Interface ---> IS A Relationship 67 | 68 | 69 | 11) STEPS :: javac -> will be original file name, but after conpiling successfully it will create a .class file 70 | with the same name as that of the Main Class that contains the MAIN FUNCTION in Java. 71 | 72 | 73 | Compile : javac 74 | java 75 | 76 | 77 | 12) The access level MODIFIERS in Java are: 78 | 79 | public - accessible everywhere in the application 80 | protected - accessible in the same package and to subclasses regardless of package 81 | nothing (default) - accessible in the same package 82 | private - visible only to code in the same class (including nested classes) 83 | 84 | 85 | 13) Main Method : Can be OVERRIDDEN , OVERLOADED , RECURSE 86 | https://www.geeksforgeeks.org/gfact-48-overloading-main-in-java/ 87 | 88 | 89 | 90 | 14) super() -> keywords used to call the constructor of the Parent Class 91 | this() -> keywords used to call the constructor of itself 92 | 93 | 94 | 95 | $$ :: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 96 | Final Keyword : https://www.javatpoint.com/final-keyword 97 | 98 | https://www.javatpoint.com/difference-between-stringbuffer-and-stringbuilder 99 | 100 | 101 | Method Overriding OR RunTime Time Polymorphism 102 | -- Subclass (child class) has the same method as declared in the parent class 103 | 104 | 105 | 15) 106 | Vector calss is thread safe (single thread) - LOW performance than ArrayList 107 | 108 | 🚨🚨 Volatile Variable: https://www.javatpoint.com/volatile-keyword-in-java 109 | 110 | 111 | 16) In Java, String values are stored in a special memory area known as the String Pool, which is part of the Heap memory. 112 | String str = "Hello!" 113 | 114 | When string is created with new keyword, then new memory is allocate in the Heap 115 | String str = new String("Hello!"); 116 | 117 | And in order to save the string in the string pool use .intern() func 118 | 119 | https://www.javatpoint.com/java-string-intern 120 | https://www.geeksforgeeks.org/storage-of-string-in-java/ 121 | https://www.javatpoint.com/string-pool-in-java#:~:text=All%20Strings%20are%20stored%20in,stored%20in%20the%20String%20pool. 122 | 123 | 124 | https://www.javatpoint.com/difference-between-stringbuffer-and-stringbuilder 125 | 126 | 127 | 128 | 129 | 130 | 131 | 132 | https://www.geeksforgeeks.org/java-and-multiple-inheritance/ 133 | -------------------------------------------------------------------------------- /java-practice/Method.java: -------------------------------------------------------------------------------- 1 | 2 | public class Method { 3 | 4 | public static void main(String[] args) { 5 | 6 | // Static Method in java --> Access static method() and fields using Class Name (without creating OBJECT) 7 | Bunglow.Country(" New Zealand "); 8 | System.out.println(" Coming from the Static Method ==> " + Bunglow.place); 9 | 10 | Bunglow objA = new Bunglow(); 11 | int res = objA.totalPopulation(210, 300); 12 | System.out.println(" \nTotal Members ==> " + res); 13 | 14 | // Values Assign 15 | objA.balcony = 4; 16 | objA.room = 8; 17 | objA.color = "Crimson"; 18 | objA.design(); 19 | 20 | System.out.println("Accessing PROTECTED VARIABLE = " + objA.buildin); 21 | 22 | System.out.println("\n---------------------------------------------------------"); 23 | System.out.println("---------------------------------------------------------"); 24 | Student s1 = new Student("John Doe", 85426, "Coder"); 25 | Student s2 = new Student("Genelia", 12537, "Dancer"); 26 | 27 | s1.createStudent(); 28 | s2.createStudent(); 29 | 30 | System.out.println(s1); 31 | } 32 | 33 | } 34 | 35 | class Bunglow { 36 | 37 | // Instance Variables 38 | public static String place = ""; 39 | int balcony; 40 | int room; 41 | String color; 42 | protected String buildin = "6 years ago"; 43 | 44 | // Static Method - always contains static variables 45 | public static void Country(String name) { 46 | place = name; 47 | } 48 | 49 | public int totalPopulation(int p1, int p2) { 50 | return p1 + p2; 51 | } 52 | 53 | public void design() { 54 | System.out.println("\n It contains " + balcony + " Balcony And " + room + " Rooms of " + color + " color. "); 55 | } 56 | 57 | } 58 | 59 | class Student { 60 | String name; 61 | int roll; 62 | String passion; 63 | 64 | // Constructor 65 | Student(String name, int roll, String passion) { 66 | this.name = name; 67 | this.roll = roll; 68 | this.passion = passion; 69 | } 70 | 71 | public void createStudent() { 72 | System.out.println("New Student created : \t" + name + "\t" + roll + "\t" + passion); 73 | } 74 | 75 | @Override 76 | //overriding the toString() method of JVM 77 | public String toString() { 78 | System.out.println("$" + this.roll + this.name); 79 | return ("$" + this.roll + this.name); 80 | } 81 | } -------------------------------------------------------------------------------- /java-practice/Method_Overloading.java: -------------------------------------------------------------------------------- 1 | 2 | public class Method_Overloading { 3 | public static void main(String[] args) { 4 | 5 | A objA = new A(); 6 | objA.Laptop("1 TB", 256); 7 | objA.Laptop(4, 180); 8 | } 9 | } 10 | 11 | class A { 12 | public void Laptop(int ram, int ssd) { 13 | System.out.println("Laptop Congfig => RAM : " + ram + " , SSD : " + ssd); 14 | } 15 | 16 | public void Laptop(String hdd, int ssd) { 17 | System.out.println("Laptop Congfig => HDD : " + hdd + " , SSD : " + ssd); 18 | } 19 | } 20 | /* 21 | Method Overloading OR Compile Time Polymorphism 22 | -- Function/Method with SAME NAME but different parameters and data types 23 | */ -------------------------------------------------------------------------------- /java-practice/Method_Overridding.java: -------------------------------------------------------------------------------- 1 | public class Method_Overridding { 2 | public static void main(String[] args) { 3 | classB objfirst = new classB(); 4 | objfirst.LaptopConfig("1 TB", 256); 5 | 6 | classA objsec = new classC(); 7 | } 8 | } 9 | 10 | class classA { 11 | classA() { 12 | System.out.println("constructor of classA"); 13 | } 14 | 15 | public void LaptopConfig(String hdd, int ssd) { 16 | System.out.println("LaptopConfig Congfig in ClassA => HDD : " + hdd + " , SSD : " + ssd); 17 | } 18 | } 19 | 20 | class classB extends classA { 21 | classB() { 22 | System.out.println("constructor of classB"); 23 | } 24 | 25 | public void LaptopConfig(String hdd, int ssd) { 26 | System.out.println("LaptopConfig Congfig in ClassB => HDD : " + hdd + " , SSD : " + ssd); 27 | } 28 | } 29 | 30 | class classC extends classB { 31 | classC() { 32 | System.out.println("constructor of classC"); 33 | } 34 | 35 | public void LaptopConfig(String hdd, int ssd) { 36 | System.out.println("LaptopConfig Congfig in classC => HDD : " + hdd + " , SSD : " + ssd); 37 | } 38 | } -------------------------------------------------------------------------------- /java-practice/Second_Largest_Ele.java: -------------------------------------------------------------------------------- 1 | 2 | public class Second_Largest_Ele { 3 | 4 | // Time Complexity : O(n) 5 | public static int findSecondLarge(int[] arr, int len) { 6 | System.out.println(len); 7 | int i, large = 0, secondLarge = -1; // stores index 8 | for (i = 1; i < len; i++) { 9 | if (arr[i] > arr[large]) { 10 | secondLarge = large; 11 | large = i; 12 | } 13 | else { 14 | if (secondLarge == -1 || arr[secondLarge] < arr[i]) 15 | secondLarge = i; 16 | } 17 | } 18 | return secondLarge; 19 | } 20 | 21 | public static void main(String[] args) { 22 | int arr[] = { 10, 66, 4, 22 }; 23 | int len = arr.length, ans = findSecondLarge(arr, len); 24 | System.out.println("Second Largest Element in the array : " + arr[ans]); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /java-practice/StringEquals.java: -------------------------------------------------------------------------------- 1 | 2 | import java.io.*; 3 | import java.util.HashMap; 4 | import java.util.*; 5 | /* 6 | Reference :: https://www.geeksforgeeks.org/difference-between-and-equals-method-in-java/#:~:text=In%20simple%20words%2C%20%3D%3D%20checks,that%20has%20overridden%20this%20method. 7 | */ 8 | 9 | public class StringEquals { 10 | 11 | public static void main(String[] args) { 12 | 13 | String s3 = "This is prepinsta Material"; 14 | // String s4 = "This is prepinsta Material"; 15 | String s5 = "This is "; 16 | String s6 = s5 + "prepinsta Material"; 17 | 18 | System.out.println(s3 == s6); 19 | 20 | String s1 = "prepinsta"; 21 | String s2 = new String("prepinsta"); 22 | 23 | System.out.println(s1 == s2); // false -- address comparison 24 | System.out.println(s1.equals(s2)); // true -- value comparison not the reference 25 | 26 | Map student = new HashMap(); 27 | 28 | student.put(101, "Rahit"); 29 | student.put(102, "Sudhir"); 30 | student.put(103, "Adithya"); 31 | student.put(104, "Sakshi"); 32 | System.out.println(student.remove(102)); 33 | 34 | String X = new String("HELLO"); 35 | String Y = new String("HELLO"); 36 | 37 | String A = "HELLO"; 38 | String B = "HELLO"; 39 | 40 | System.out.println(A == B); // true 41 | System.out.println(A.equals(B)); // true 42 | System.out.println(X == Y); // false 43 | System.out.println(X.equals(Y)); // true --> content comparison 44 | 45 | System.out.println(A.equals(X)); // true 46 | System.out.println(A == X); // false 47 | 48 | System.out.println("******************************************************"); 49 | char character = 'a'; 50 | int num = (int) character; 51 | System.out.println(num); 52 | System.out.println((char) 65); 53 | 54 | char x = 65; 55 | System.out.println(x); 56 | 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /java-practice/String_Function.java: -------------------------------------------------------------------------------- 1 | 2 | import java.util.*; 3 | 4 | public class String_Function { 5 | public static void main(String[] args) { 6 | 7 | Scanner sc = new Scanner(System.in); 8 | String strA = "Hello World"; 9 | String strB = new String("Happy_Coding!"); // Str formed using Constructor 10 | 11 | System.out.println(strA); 12 | System.out.println(strA.compareTo(strB)); // Returns the difference between the 2 that differs 13 | System.out.println(strA.contains("Wor")); 14 | System.out.println(strB.endsWith("ing!")); 15 | System.out.println(strA.equals(strB)); 16 | System.out.println(strA.length()); 17 | System.out.println(strA.replaceAll("o", "OOOO")); 18 | System.out.println(strA.substring(6)); 19 | System.out.println(strA.concat(strB)); 20 | System.out.println(" Bye ".trim()); // Removes white spaces 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /java-practice/Test.java: -------------------------------------------------------------------------------- 1 | 2 | import java.io.*; 3 | // import java.lang.*; 4 | import java.util.*; 5 | import java.util.ArrayList; 6 | 7 | public class Test { 8 | public static void main(String[] args) { 9 | ArrayList al = new ArrayList(); 10 | 11 | al.add("Sachin"); 12 | al.add("Rahul"); 13 | // al.add(10); // Compiler allows this 14 | 15 | String s1 = (String) al.get(0); 16 | String s2 = (String) al.get(1); 17 | 18 | // Causes Runtime Exception 19 | // String s3 = (String) al.get(2); 20 | System.out.println(s2); 21 | 22 | // interface_name var = new class_name<>(); 23 | List l2 = new ArrayList<>(); 24 | l2.add(44); 25 | l2.add(98); 26 | System.out.println(l2); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /mypackage/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/niharika2k00/Java/abe9fc7615a5ff3852a90efd2230f29d569dbc68/mypackage/.DS_Store -------------------------------------------------------------------------------- /mypackage/Origin.java: -------------------------------------------------------------------------------- 1 | import p1.*; 2 | 3 | public class Origin { 4 | public static void main(String[] args) { 5 | System.out.println("helo"); 6 | 7 | House house1 = new House(1968, "Kolkata", "Light Yellow"); 8 | house1.getLocation(); 9 | 10 | Room room1 = new Room(20, "Kolkata", "Pink"); 11 | Room.setRoomArea(8); 12 | System.out.println("Get room area => " + room1.getRoomArea()); 13 | room1.getLocation(); 14 | 15 | System.out.println("Child Class can access Parent Class properties directly :" + Room.area); 16 | } 17 | } 18 | 19 | // Only Public Class can be accessed outside the package 20 | -------------------------------------------------------------------------------- /mypackage/p1/House.java: -------------------------------------------------------------------------------- 1 | 2 | package p1; 3 | 4 | public class House { 5 | // private Room nonPublicRoomClass; // Private Instance Variable of a Non-public class usable within the same package 6 | public String location, color; 7 | public int builtin = 1899, totalRooms; 8 | 9 | public House(int totalRooms, String location, String color) { 10 | // nonPublicRoomClass = new Room(totalRooms, location, color); 11 | this.totalRooms = totalRooms; 12 | this.location = location; 13 | this.color = color; 14 | } 15 | 16 | public void getLocation() { 17 | System.out.println("House situated at " + location); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /mypackage/p1/Room.java: -------------------------------------------------------------------------------- 1 | 2 | package p1; 3 | 4 | public class Room extends House { 5 | // public int roomNo; 6 | // public int area; 7 | public static int area, roomNo; 8 | 9 | public Room(int roomNo, String location, String color) { 10 | super(roomNo, location, color); 11 | // roomNo = Math.random() * ( roomNo - 0 ) 12 | this.roomNo = totalRooms; 13 | this.color = color; 14 | } 15 | 16 | public static void setRoomArea(int areaValue) { 17 | System.out.println("value received = " + areaValue); 18 | area = (areaValue + (int) (Math.random() * (roomNo - 1 + 1)) + 1); 19 | } 20 | 21 | public int getRoomArea() { 22 | return area; 23 | } 24 | 25 | public void describeRoom() { 26 | System.out.println("Room with area " + area + " and color " + color); 27 | } 28 | } -------------------------------------------------------------------------------- /mypackage/tempCodeRunnerFile.java: -------------------------------------------------------------------------------- 1 | 2 | System.out.println("Child Class can access Parent Class properties directly :" + room1.location); --------------------------------------------------------------------------------