From 8ab4deb1030b4d863f8d8048b892d34f18dfaebe Mon Sep 17 00:00:00 2001 From: Srinjoy Pal <144480997+srinjoy07pal@users.noreply.github.com> Date: Mon, 25 Dec 2023 20:37:51 +0530 Subject: [PATCH 1/5] Update BinarySearch.java binary search explained in a better way using functions --- core-java/basics/BinarySearch.java | 62 ++++++++++++++++++++++-------- 1 file changed, 45 insertions(+), 17 deletions(-) diff --git a/core-java/basics/BinarySearch.java b/core-java/basics/BinarySearch.java index d30ba02..c8d6694 100644 --- a/core-java/basics/BinarySearch.java +++ b/core-java/basics/BinarySearch.java @@ -1,19 +1,47 @@ -package basics; -import java.util.Arrays; - -public class BinarySearch { - - public static void main(String[] args) { - - int arr[] = {10, 20, 15, 22, 35}; - Arrays.sort(arr); - - int key = 35; - int res = Arrays.binarySearch(arr, key); - if(res >= 0){ - System.out.println(key + " found at index = "+ res); - } else { - System.out.println(key + " not found"); - } +// Java implementation of iterative Binary Search + +import java.io.*; + +class BinarySearch { + + // Returns index of x if it is present in arr[]. + int binarySearch(int arr[], int x) + { + int l = 0, r = arr.length - 1; + while (l <= r) { + int m = l + (r - l) / 2; + + // Check if x is present at mid + if (arr[m] == x) + return m; + + // If x greater, ignore left half + if (arr[m] < x) + l = m + 1; + + // If x is smaller, ignore right half + else + r = m - 1; + } + + // If we reach here, then element was + // not present + return -1; + } + + // Driver code + public static void main(String args[]) + { + BinarySearch ob = new BinarySearch(); + int arr[] = { 2, 3, 4, 10, 40 }; + int n = arr.length; + int x = 10; + int result = ob.binarySearch(arr, x); + if (result == -1) + System.out.println( + "Element is not present in array"); + else + System.out.println("Element is present at " + + "index " + result); } } From b9b06afc2c6014e8cbff007992d34216c7e3ea3f Mon Sep 17 00:00:00 2001 From: NischalKothari Date: Thu, 28 Dec 2023 17:42:48 +0530 Subject: [PATCH 2/5] More Efficient Bubble Sort Code --- java-programs/BubbleSort.java | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/java-programs/BubbleSort.java b/java-programs/BubbleSort.java index 2f1ee8f..15b1d88 100644 --- a/java-programs/BubbleSort.java +++ b/java-programs/BubbleSort.java @@ -7,16 +7,22 @@ public class BubbleSort { public void bubbleSort(int[] arr) { + boolean swap; int n = arr.length; int tmp = 0; for (int i = 0; i < n ; i++) { - for (int j = 1; j < n-1; j++) { + swap = false; + for (int j = 1; j < n-i; j++) { if (arr[j-1] > arr[j]) { - tmp = arr[j - 1]; - arr[j-1] = arr[j]; - arr[j] = tmp; + tmp = arr[j]; + arr[j] = arr[j-1]; + arr[j-1] = tmp; + swap = true; } } + if (!swap){ + break; + } } } From 4ccb582c7204ca58bdbd0683a5ab463204fe2508 Mon Sep 17 00:00:00 2001 From: Anup Maharjan <52039155+Anup6452@users.noreply.github.com> Date: Tue, 6 Aug 2024 18:23:11 +0545 Subject: [PATCH 3/5] Remove duplicate question --- java-multiple-choice-questions-answers.md | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/java-multiple-choice-questions-answers.md b/java-multiple-choice-questions-answers.md index 9bb173c..33e86f6 100644 --- a/java-multiple-choice-questions-answers.md +++ b/java-multiple-choice-questions-answers.md @@ -41,6 +41,8 @@ D. int num1 = 0, num2 = 0; ``` A. double num1, int num2 = 0; ``` +**Explanation**: A. Option A does not compile because Java does not allow declaring different types as part of the same declaration. + ## Q. What is the output of following program? ```java public class Test { @@ -214,15 +216,6 @@ Output a : 2 J : 2 ``` -## Q. Which of the following declarations does not compile? -A. double num1, int num2 = 0; -B. int num1, num2; -C. int num1, num2 = 0; -D. int num1 = 0, num2 = 0; -``` -A. double num1, int num2 = 0; -``` -**Explanation**: A. Option A does not compile because Java does not allow declaring different types as part of the same declaration. ## Q. What is the output of the following? ```java From cd7bbb61f8f8a96de9710c6687f120c91d8275e7 Mon Sep 17 00:00:00 2001 From: Kaustubh Pandey <146510603+kautubh01@users.noreply.github.com> Date: Tue, 27 Aug 2024 22:35:11 +0530 Subject: [PATCH 4/5] Update BoundedTypesGenerics02.java --- core-java/basics/BoundedTypesGenerics02.java | 39 ++++++++++---------- 1 file changed, 19 insertions(+), 20 deletions(-) diff --git a/core-java/basics/BoundedTypesGenerics02.java b/core-java/basics/BoundedTypesGenerics02.java index 7657f86..a1ad4e4 100644 --- a/core-java/basics/BoundedTypesGenerics02.java +++ b/core-java/basics/BoundedTypesGenerics02.java @@ -1,29 +1,28 @@ -package basics; - interface SampleInterface { - public void displayClass(); + void displayClass(); } + class BoundTest { - private T objRef; - public BoundTest(T obj) { - this.objRef = obj; - } - - public void doRunTest(){ - this.objRef.displayClass(); - } -} + private T objRef; + public BoundTest(T obj) { + this.objRef = obj; + } + + public void doRunTest() { + objRef.displayClass(); + } +} class SampleClass implements SampleInterface { - public void displayClass() { - System.out.println("Inside supper Class A"); - } + public void displayClass() { + System.out.println("Inside super Class A"); + } } -class BoundedTypesGenerics02 { - public static void main(String a[]) { - BoundTest b = new BoundTest<>(new SampleClass()); - b.doRunTest(); - } +public class BoundedTypesGenerics02 { + public static void main(String[] args) { + BoundTest b = new BoundTest<>(new SampleClass()); + b.doRunTest(); + } } From 968a729f2577549397f7ad75f0f3bb5ef79bf1c4 Mon Sep 17 00:00:00 2001 From: Kaustubh Pandey <146510603+kautubh01@users.noreply.github.com> Date: Tue, 27 Aug 2024 22:42:13 +0530 Subject: [PATCH 5/5] Update EnumConstructorExample.java --- core-java/basics/EnumConstructorExample.java | 25 +++++++++----------- 1 file changed, 11 insertions(+), 14 deletions(-) diff --git a/core-java/basics/EnumConstructorExample.java b/core-java/basics/EnumConstructorExample.java index b3e0cfe..6600380 100644 --- a/core-java/basics/EnumConstructorExample.java +++ b/core-java/basics/EnumConstructorExample.java @@ -1,24 +1,21 @@ -package basics; - enum TrafficSignal { RED("STOP"), GREEN("GO"), ORANGE("SLOW DOWN"); - - private String action; - public String getAction() { - return this.action; - } - private TrafficSignal(String action) { + + private final String action; + + TrafficSignal(String action) { this.action = action; } + + public String getAction() { + return action; + } } -public class EnumConstructorExample { +public class EnumConstructorExample { public static void main(String[] args) { - - TrafficSignal[] signals = TrafficSignal.values(); - - for(TrafficSignal signal: signals){ - System.out.println("name: "+ signal.name() + " action: " + signal.getAction()); + for (TrafficSignal signal : TrafficSignal.values()) { + System.out.println("name: " + signal.name() + " action: " + signal.getAction()); } } }