diff --git a/Collections/.project b/Collections/.project index c461daf..5ea0f58 100644 --- a/Collections/.project +++ b/Collections/.project @@ -14,4 +14,15 @@ org.eclipse.jdt.core.javanature + + + 1647954731126 + + 30 + + org.eclipse.core.resources.regexFilterMatcher + node_modules|.git|__CREATED_BY_JAVA_LANGUAGE_SERVER__ + + + diff --git a/Collections/src/com/cdac/collections/collectionsutils/CollectionsDemo.java b/Collections/src/com/cdac/collections/collectionsutils/CollectionsDemo.java index 954db7a..8b5f045 100644 --- a/Collections/src/com/cdac/collections/collectionsutils/CollectionsDemo.java +++ b/Collections/src/com/cdac/collections/collectionsutils/CollectionsDemo.java @@ -8,8 +8,8 @@ import java.util.Map; import java.util.Set; -public class CollectionsDemo { - +public class CollectionsDemo +{ public static void main(String[] args) { ArrayList list = new ArrayList<>(); list.add("One"); @@ -51,14 +51,14 @@ public static void main(String[] args) { String min = Collections.min(list); System.out.println("Min Element in the List : "+min); - List iList = new ArrayList<>(); - iList.add(10); - iList.add(20); - iList.add(40); - iList.add(30); + List indexList = new ArrayList<>(); //iList name is not understadable can be converted to List 2 or IndexList + indexList.add(10); + indexList.add(20); + indexList.add(40); + indexList.add(30); //Binary Search in List Collection - int index = Collections.binarySearch(iList, 190); + int index = Collections.binarySearch(indexList, 190); System.out.println("Binary Search of 190 index is : "+index); //Copy one list to another list @@ -71,36 +71,60 @@ public static void main(String[] args) { newList.add(14); newList.add(15); //newList should have minimum size as iList - Collections.copy(newList, iList); + Collections.copy(newList, indexList); System.out.println("Copying the list from another list : "+newList); - //Creating Immutable Collection - Set emptySet = Collections.emptySet(); - List emptyList = Collections.emptyList(); - Map emptyMap = Collections.emptyMap(); - System.out.println("Creating immutable collection (list, set, map)"); - System.out.println("Empty Set : "+emptySet.size()); - //emptySet.add("Try"); //not allowed its immutable - - //replacing all the elements with the new value - Collections.replaceAll(iList, 10, 100); - System.out.println("Replacing all the 10 in the list with 100 :"); - System.out.println(iList); - - //shuffling the list - Collections.shuffle(iList); - System.out.println("Shuffle the Elements in the List : "); - System.out.println(iList); + + + methodCalling(indexList); //Calling all methods from one place - //Creating singleton Collection Set, List, Map - Set singletonSet = Collections.singleton("Java"); - System.out.println("Creating singleton Collection : "); - System.out.println(singletonSet); -// singletonSet.add("Hello"); //not supported its immutable + } + + static void methodCalling(List indexList) + { + immutableCollection(); // Extracted Method + replaceAndShuffle(indexList); // Extracted Method + singletonCollection(); // Extracted Method + synchronizedCollection(); // Extracted Method + } + static void immutableCollection() + { + //Creating Immutable Collection + Set emptySet = Collections.emptySet(); + List emptyList = Collections.emptyList(); + Map emptyMap = Collections.emptyMap(); + System.out.println("Creating immutable collection (list, set, map)"); + System.out.println("Empty Set : "+emptySet.size()); + //emptySet.add("Try"); //not allowed its immutable + } + + static void replaceAndShuffle(List indexList) + { + //replacing all the elements with the new value + Collections.replaceAll(indexList, 10, 100); + System.out.println("Replacing all the 10 in the list with 100 :"); + System.out.println(indexList); + + //shuffling the list + Collections.shuffle(indexList); + System.out.println("Shuffle the Elements in the List : "); + System.out.println(indexList); + } + + static void singletonCollection() + { + //Creating singleton Collection Set, List, Map + Set singletonSet = Collections.singleton("Java"); + //singletonSet.add("Hello"); //not supported its immutable + System.out.println("Creating singleton Collection : "); + System.out.println(singletonSet); + } - //Creating synchronized Collection List, Set, Map - Map map = new HashMap<>(); - map = Collections.synchronizedMap(map); - System.out.println("Creating synchronizing Collection : "); + static void synchronizedCollection() + { + //Creating synchronized Collection List, Set, Map + Map map = new HashMap<>(); + map = Collections.synchronizedMap(map); + System.out.println("Creating synchronizing Collection : "); } } \ No newline at end of file diff --git a/Collections/src/com/cdac/collections/threadsafe/EmployeeRunnable.java b/Collections/src/com/cdac/collections/threadsafe/EmployeeRunnable.java index 4670969..b528193 100644 --- a/Collections/src/com/cdac/collections/threadsafe/EmployeeRunnable.java +++ b/Collections/src/com/cdac/collections/threadsafe/EmployeeRunnable.java @@ -2,7 +2,8 @@ import java.util.ArrayList; -public class EmployeeRunnable implements Runnable{ +public class EmployeeRunnable +{ ArrayList names; @@ -10,9 +11,6 @@ public EmployeeRunnable(ArrayList list) { this.names = list; } - @Override - public void run() { - for(int i=0;i<100;i++) - names.add("Employee :: "+i); - } -} \ No newline at end of file + +} +//run method moved to MainProgram.java \ No newline at end of file diff --git a/Collections/src/com/cdac/collections/threadsafe/MainProgram.java b/Collections/src/com/cdac/collections/threadsafe/MainProgram.java index 29775ed..583212f 100644 --- a/Collections/src/com/cdac/collections/threadsafe/MainProgram.java +++ b/Collections/src/com/cdac/collections/threadsafe/MainProgram.java @@ -1,22 +1,65 @@ package com.cdac.collections.threadsafe; - import java.util.ArrayList; +// import com.cdac.collections.bean.Student; +// Applying move method run to main program, decreases the complexity of tread running +public class MainProgram implements Runnable +{ + public static void main(String[] args) throws CloneNotSupportedException { -import com.cdac.collections.bean.Student; - -public class MainProgram { - public static void main(String[] args) throws CloneNotSupportedException { - ArrayList list = new ArrayList<>(); - EmployeeRunnable employeeRunnable = new EmployeeRunnable(list); - StudentRunnable studentRunnable = new StudentRunnable(list); - - Thread t1 = new Thread(employeeRunnable); - Thread t2 = new Thread(studentRunnable); + Thread t1 = new Thread(); + Thread t2 = new Thread(); t1.start(); t2.start(); + ArrayList list = new ArrayList<>(); + EmployeeRunnable employeeRunnable = new EmployeeRunnable(list); + StudentRunnable studentRunnable = new StudentRunnable(list); - + + } + public void run() + { + ArrayList Employeenames = null; + ArrayList Studentnames = null; + for(int i=0;i<100;i++) + { + Employeenames.add("Employee :: "+i); + } + for(int j =100; j<200;j++) + { + Studentnames.add("Student :: "+j); + } } -} \ No newline at end of file + +} +// class EmployeeRunnable implements Runnable +// { + +// ArrayList names; + +// public EmployeeRunnable(ArrayList list) { +// this.names = list; +// } + +// @Override +// public void run() { +// for(int i=0;i<100;i++) +// names.add("Employee :: "+i); +// } +// } + +// class StudentRunnable implements Runnable +// { + +// ArrayList names; + +// public StudentRunnable(ArrayList list) { +// this.names = list; +// } +// @Override +// public void run() { +// for(int i=100;i<200;i++) +// names.add("Student :: "+i); +// } + diff --git a/Collections/src/com/cdac/collections/threadsafe/StudentRunnable.java b/Collections/src/com/cdac/collections/threadsafe/StudentRunnable.java index 918a0c5..192b510 100644 --- a/Collections/src/com/cdac/collections/threadsafe/StudentRunnable.java +++ b/Collections/src/com/cdac/collections/threadsafe/StudentRunnable.java @@ -2,17 +2,12 @@ import java.util.ArrayList; -public class StudentRunnable implements Runnable{ - +public class StudentRunnable +{ ArrayList names; - - public StudentRunnable(ArrayList list) { + public StudentRunnable(ArrayList list) + { this.names = list; } - @Override - public void run() { - for(int i=100;i<200;i++) - names.add("Student :: "+i); - } - -} \ No newline at end of file +} +//run method moved to MainProgram.java \ No newline at end of file diff --git a/EncryptionDecryption/.project b/EncryptionDecryption/.project index 2197d2e..18368b6 100644 --- a/EncryptionDecryption/.project +++ b/EncryptionDecryption/.project @@ -14,4 +14,15 @@ org.eclipse.jdt.core.javanature + + + 1647954731142 + + 30 + + org.eclipse.core.resources.regexFilterMatcher + node_modules|.git|__CREATED_BY_JAVA_LANGUAGE_SERVER__ + + + diff --git a/EncryptionDecryption/bin/.gitignore b/EncryptionDecryption/bin/.gitignore deleted file mode 100644 index d728ce5..0000000 --- a/EncryptionDecryption/bin/.gitignore +++ /dev/null @@ -1 +0,0 @@ -/snippet/ diff --git a/EncryptionDecryption/bin/com/cdac/encrypt/aes/AESEncryption.class b/EncryptionDecryption/bin/com/cdac/encrypt/aes/AESEncryption.class index 86148c9..b328de9 100644 Binary files a/EncryptionDecryption/bin/com/cdac/encrypt/aes/AESEncryption.class and b/EncryptionDecryption/bin/com/cdac/encrypt/aes/AESEncryption.class differ diff --git a/EncryptionDecryption/bin/com/cdac/encrypt/aes/encryptdecrypt.class b/EncryptionDecryption/bin/com/cdac/encrypt/aes/encryptdecrypt.class new file mode 100644 index 0000000..ba1137f Binary files /dev/null and b/EncryptionDecryption/bin/com/cdac/encrypt/aes/encryptdecrypt.class differ diff --git a/EncryptionDecryption/bin/snippet/Snippet.class b/EncryptionDecryption/bin/snippet/Snippet.class new file mode 100644 index 0000000..d3e0080 Binary files /dev/null and b/EncryptionDecryption/bin/snippet/Snippet.class differ diff --git a/EncryptionDecryption/src/com/cdac/encrypt/aes/AESEncryption.java b/EncryptionDecryption/src/com/cdac/encrypt/aes/AESEncryption.java index b0db7ce..7169df9 100644 --- a/EncryptionDecryption/src/com/cdac/encrypt/aes/AESEncryption.java +++ b/EncryptionDecryption/src/com/cdac/encrypt/aes/AESEncryption.java @@ -14,25 +14,33 @@ */ public class AESEncryption { - public static void main(String[] args) { - final String strToEncrypt = "My text to encrypt"; + public static void main(String[] args) + { + final String strToEncrypt = "My text to encrypt"; final String strPssword = "f2fc2007-b24b-4ab5-b62f-8dba873d0341"; - AESEncryption.setKey(strPssword); - AESEncryption.encrypt(strToEncrypt.trim()); + encryptdecrypt.setKey(strPssword); + //encryptdecrypt is called from main funcation + encryptdecrypt.encrypt(strToEncrypt.trim()); System.out.println("String to Encrypt: " + strToEncrypt); - System.out.println("Encrypted: " + AESEncryption.getEncryptedString()); - final String strToDecrypt = AESEncryption.getEncryptedString(); - AESEncryption.decrypt(strToDecrypt.trim()); + System.out.println("Encrypted: " + encryptdecrypt.getEncryptedString()); + final String strToDecrypt = encryptdecrypt.getEncryptedString(); + encryptdecrypt.decrypt(strToDecrypt.trim()); System.out.println("String To Decrypt : " + strToDecrypt); - System.out.println("Decrypted : " + AESEncryption.getDecryptedString()); + System.out.println("Decrypted : " + encryptdecrypt.getDecryptedString()); } +} +//class encryptdecrypt increases the readability of the code. So, it is extracted to another new class. +//funcationality remains the same besides having refactoring +class encryptdecrypt +{ private static SecretKeySpec secretKey; private static byte[] key; private static String decryptedString; private static String encryptedString; - public static void setKey(String myKey) { + public static void setKey(String myKey) + { MessageDigest sha = null; try { key = myKey.getBytes("UTF-8"); diff --git a/EncryptionDecryption/src/snippet/Snippet.java b/EncryptionDecryption/src/snippet/Snippet.java index 7aaeb24..9ed82b4 100644 --- a/EncryptionDecryption/src/snippet/Snippet.java +++ b/EncryptionDecryption/src/snippet/Snippet.java @@ -1,6 +1,8 @@ package snippet; -public class Snippet { - Divisors are +public class Snippet +{ + //Divisors are } +//changed snippet.java as code is giving error diff --git a/ExceptionHandling/.project b/ExceptionHandling/.project index d1a8803..d585cd8 100644 --- a/ExceptionHandling/.project +++ b/ExceptionHandling/.project @@ -14,4 +14,15 @@ org.eclipse.jdt.core.javanature + + + 1647954731166 + + 30 + + org.eclipse.core.resources.regexFilterMatcher + node_modules|.git|__CREATED_BY_JAVA_LANGUAGE_SERVER__ + + + diff --git a/FileIO/.project b/FileIO/.project index 178ff18..46e10c7 100644 --- a/FileIO/.project +++ b/FileIO/.project @@ -14,4 +14,15 @@ org.eclipse.jdt.core.javanature + + + 1647954731181 + + 30 + + org.eclipse.core.resources.regexFilterMatcher + node_modules|.git|__CREATED_BY_JAVA_LANGUAGE_SERVER__ + + + diff --git a/Generics/.project b/Generics/.project index 35f7874..9b430bc 100644 --- a/Generics/.project +++ b/Generics/.project @@ -14,4 +14,15 @@ org.eclipse.jdt.core.javanature + + + 1647954731191 + + 30 + + org.eclipse.core.resources.regexFilterMatcher + node_modules|.git|__CREATED_BY_JAVA_LANGUAGE_SERVER__ + + + diff --git a/InterviewPrograms/.project b/InterviewPrograms/.project index faaf7d1..a765cf4 100644 --- a/InterviewPrograms/.project +++ b/InterviewPrograms/.project @@ -14,4 +14,15 @@ org.eclipse.jdt.core.javanature + + + 1647954731205 + + 30 + + org.eclipse.core.resources.regexFilterMatcher + node_modules|.git|__CREATED_BY_JAVA_LANGUAGE_SERVER__ + + + diff --git a/InterviewPrograms/src/com/java/strings/CountVowels.java b/InterviewPrograms/src/com/java/strings/CountVowels.java index 278b85f..a71d3c3 100644 --- a/InterviewPrograms/src/com/java/strings/CountVowels.java +++ b/InterviewPrograms/src/com/java/strings/CountVowels.java @@ -31,29 +31,69 @@ * by, cry, crypt, fry, gym, psych, spy */ -public class CountVowels { - public static void main(String[] args) { +public class CountVowels extends countVow { + public static void main(String[] args) + { // String line = "Java Interview Programs"; -// String line = "Hello World!"; + // String line = "Hello World!"; String line = "Rhythm"; - int count = 0; - System.out.println("Given String is :"+line); line = line.toLowerCase(); - for(char ch : line.toCharArray()){ - switch (ch) { - case 'a': - case 'e': - case 'i': - case 'o': - case 'u': - count++; - break; - default: - break; - } - } - System.out.println("Number of Vowels are :"+count); + countVow c = new CountVowels(); + c.count(line); + } + @Override + int count(String line) + { + System.out.println("Number of Vowels are :"+count); + return 0; + } +} + +//extract class +abstract class countVow +{ + static int count = 0; + abstract int count(String line); +} +class a extends countVow +{ + @Override + public int count(String line) + { + return count++; // refactored with Replace conditional with polymorphism + } +} +class e extends countVow +{ + @Override + public int count(String line) + { + return count++; // refactored with Replace conditional with polymorphism + } +} +class i extends countVow +{ + @Override + public int count(String line) + { + return count++; // refactored with Replace conditional with polymorphism + } +} +class o extends countVow +{ + @Override + public int count(String line) + { + return count++; // refactored with Replace conditional with polymorphism + } +} +class u extends countVow +{ + @Override + public int count(String line) + { + return count++; // refactored with Replace conditional with polymorphism } } /* diff --git a/JavaEnums/.project b/JavaEnums/.project index 36a5aea..3e26146 100644 --- a/JavaEnums/.project +++ b/JavaEnums/.project @@ -14,4 +14,15 @@ org.eclipse.jdt.core.javanature + + + 1647954731215 + + 30 + + org.eclipse.core.resources.regexFilterMatcher + node_modules|.git|__CREATED_BY_JAVA_LANGUAGE_SERVER__ + + + diff --git a/Networking/.project b/Networking/.project index bf949aa..2ddd01c 100644 --- a/Networking/.project +++ b/Networking/.project @@ -14,4 +14,15 @@ org.eclipse.jdt.core.javanature + + + 1647954731223 + + 30 + + org.eclipse.core.resources.regexFilterMatcher + node_modules|.git|__CREATED_BY_JAVA_LANGUAGE_SERVER__ + + + diff --git a/Reflections/.project b/Reflections/.project index d5dae35..92518f1 100644 --- a/Reflections/.project +++ b/Reflections/.project @@ -14,4 +14,15 @@ org.eclipse.jdt.core.javanature + + + 1647954731243 + + 30 + + org.eclipse.core.resources.regexFilterMatcher + node_modules|.git|__CREATED_BY_JAVA_LANGUAGE_SERVER__ + + + diff --git a/Threads/.project b/Threads/.project index 35e7365..bf2e10d 100644 --- a/Threads/.project +++ b/Threads/.project @@ -14,4 +14,15 @@ org.eclipse.jdt.core.javanature + + + 1647954731264 + + 30 + + org.eclipse.core.resources.regexFilterMatcher + node_modules|.git|__CREATED_BY_JAVA_LANGUAGE_SERVER__ + + +