├── .gitignore ├── Contributors.md ├── Facts.md ├── LICENSE ├── Programs ├── Algorithms │ ├── BubbleSort.java │ ├── BucketSort.java │ ├── Dikstra.java │ ├── FIFO.java │ ├── FloyadsAlgorithm.java │ ├── Heap_Sort.java │ ├── InsertionSort.java │ ├── LinearSearchExample.java │ ├── MergeSort.java │ ├── MergeSort.md │ ├── Prims.java │ ├── Prims.md │ ├── RecursiveBubbleSort.java │ └── kruskal.java ├── ArrayList Example │ └── ReplaceOddSequenceWithCount.java ├── ArraysExample │ ├── ArrayAndFiles.java │ ├── ArrayReverser.java │ ├── ArrayToArrayList.java │ ├── Arrays.java │ ├── CompareArrays.java │ ├── CopyArrays.java │ ├── HighestAndLowest.java │ ├── HowManyNumbersAreSmallerThantheCurrentNumber.java │ ├── Insert.java │ ├── LeftRotation.java │ ├── MergeArray.java │ ├── PasswordValidator.java │ ├── RecursiveBinarySearch.java │ ├── RemoveDuplicatesInArray.java │ ├── SortArrays.java │ ├── StringArray.java │ ├── SumAndAverage.java │ ├── TheArrayList.java │ ├── Two_Sum.java │ └── Values.txt ├── Backtracking │ ├── Mcolor.java │ └── Nqueenproblem.java ├── Basic-java-program │ ├── Age Calculator.java │ ├── Array.java │ ├── Ascii.java │ ├── Average.java │ ├── AverageandPercentage.java │ ├── BankWithDraw.java │ ├── Bitwise.java │ ├── By.java │ ├── Calculator.java │ ├── Camera.java │ ├── Car.java │ ├── Cast.java │ ├── Charyo.java │ ├── CheckAlphabet.java │ ├── CheckAnagram.java │ ├── CheckLeapYear.java │ ├── CheckPrimeNumber.java │ ├── CheckReversal.java │ ├── Chips.java │ ├── Circle.java │ ├── Coin.java │ ├── Demo.java │ ├── Demo4.java │ ├── Demo5.java │ ├── Demo6.java │ ├── Demo7.java │ ├── Demo8.java │ ├── Demo90.java │ ├── Demoo.java │ ├── Demos.java │ ├── Diomond.java │ ├── Employee.java │ ├── Even.java │ ├── ExceptionTask1.java │ ├── Fabbonaccie.java │ ├── Face.java │ ├── Factorial.java │ ├── Glass.java │ ├── Google.java │ ├── Hello.java │ ├── Hellos.java │ ├── Homework.java │ ├── Human.java │ ├── Input.java │ ├── Logical.java │ ├── Main.java │ ├── Main2.java │ ├── Main21.java │ ├── Main4.java │ ├── MyClass.java │ ├── Name.java │ ├── OOP_Task_3rd.java │ ├── Operator.java │ ├── Presentation1github.pptx │ ├── Prime.java │ ├── Run.java │ ├── Runs.java │ ├── Runtime.java │ ├── Shaikh.java │ ├── Soor.java │ ├── Start.java │ ├── Starts.java │ ├── String.java │ ├── Strings.java │ ├── Studen.java │ ├── Student.java │ ├── StudentInform.java │ ├── Students.java │ ├── Sub.java │ ├── Table.java │ ├── Task.java │ ├── Task1.java │ ├── Task2.java │ ├── Task3.java │ ├── Task5.java │ ├── Taskt.java │ ├── Test.java │ ├── Vowel.java │ ├── nested class.txt │ └── txt.java ├── CSVFileImportAndConversionFromUTF8BOMtoUTF8 │ ├── File.xhtml │ └── FileUploadBean.java ├── Classes and Object Example │ ├── DriverClass.java │ ├── House.java │ └── README.md ├── Comments │ └── Comments.java ├── CompareStrings │ └── CompareStrings.java ├── DataTypes │ ├── Salary.java │ └── StudentRecord.java ├── Decision Structures │ └── DecisionStructures.java ├── DecisionMaking │ └── Demo1.java ├── Dynamic_programming(DP) │ ├── Knapsack.java │ ├── Longest_palindromic_subsequence.java │ ├── MinimumCoinProblem.java │ ├── count_Square.java │ ├── editdistance.java │ ├── longest_common_subsequence.java │ ├── maximum_product_subarray.java │ └── stairCase.java ├── HTTPRequest │ └── GET │ │ └── App.java ├── InputOutput │ ├── Example1.java │ └── TernaryExample.java ├── JDBC │ └── Example │ │ └── VaccineReport.java ├── JavaNIOAPI │ ├── CSVFile.java │ ├── Files │ │ ├── Data.csv │ │ └── SomeText.txt │ ├── ReadingFiles.java │ ├── WalkFileTreePattern.java │ ├── WalkPattern.java │ ├── WritingFiles.java │ ├── WritingTextFiles.java │ └── model │ │ └── Person.java ├── JavaReflectionAPI │ └── src │ │ └── com │ │ └── anushcodergirl │ │ └── reflection │ │ ├── PlayWithReflection.java │ │ └── model │ │ └── Person.java ├── Loops │ ├── Loops.java │ └── WhileLoops │ │ └── Main.java ├── Methods │ ├── Methods.java │ └── TestCustomException1.java ├── Miscellaneous │ ├── Age_Calculator.java │ ├── date.java │ ├── fibonaccisearch.java │ ├── taximeter.java │ └── test.java ├── MoreClasses │ ├── Driver.java │ ├── ExamPaper.java │ ├── Student.java │ └── Subject.java ├── Operators │ ├── AssignmentOperators.java │ ├── LogicalOperators.java │ └── RelationalOperators.java ├── Pattern Programs │ ├── Alpahabetical_pattern │ │ └── a_pattern.java │ ├── Diamond_star_pattern.java │ ├── KShapeUsingAlgorithms │ │ └── KShapeUsingAlphabets.java │ ├── PatternOne.java │ ├── ReversedPyramidStarPattern.java │ ├── Triangle_num_pattern1.java │ ├── diamondpattern.java │ ├── n_pattern.java │ └── pyramid patterns │ │ ├── PascalTrianglePattern.java │ │ ├── Pattern_1.java │ │ ├── Pattern_2.java │ │ ├── Pattern_3.java │ │ ├── Pattern_4.java │ │ └── Pattern_5.java ├── ReadingInputUsingJOptionPane │ └── ReadJOptionPane.java ├── ReadingInputUsingScanner │ └── ReadingInput.java ├── Stack Implementation │ ├── BalanceParaenthesis.java │ ├── Implementation.java │ └── reverseString.java ├── StringConcatenation │ └── Example1.java ├── Strings │ └── Pangram.java ├── TextFiles │ ├── OutputFile.txt │ ├── ReadingFromFiles.java │ └── WritingToFiles.java ├── TheSwitchStatement │ ├── Java15NewSwitchStatement.java │ └── TheSwitchStatement.java ├── TypeConversion │ └── TypeConversion.java ├── UsefulLoopCalculations │ ├── InputValidation.java │ ├── RunningTotals.java │ ├── SentinelValues.java │ └── UserControlledLoop.java └── Variables, Literals and Constants │ └── Variables_Literals_Constants.java ├── README.md └── RESOURCES.md /.gitignore: -------------------------------------------------------------------------------- 1 | # Compiled class file 2 | *.class 3 | 4 | # Log file 5 | *.log 6 | 7 | # BlueJ files 8 | *.ctxt 9 | 10 | # Mobile Tools for Java (J2ME) 11 | .mtj.tmp/ 12 | 13 | # Package Files # 14 | *.jar 15 | *.war 16 | *.nar 17 | *.ear 18 | *.zip 19 | *.tar.gz 20 | *.rar 21 | 22 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml 23 | hs_err_pid* 24 | -------------------------------------------------------------------------------- /Contributors.md: -------------------------------------------------------------------------------- 1 | # Contributors 2 | 3 | 4 | 5 | - [**Anushka Raj**](https://www.linkedin.com/in/anushka-raj/) 6 | - [**Manav Bahl**](https://www.linkedin.com/in/manavbahl/) 7 | - [**Shubham Kashyap**](https://www.linkedin.com/in/shubhkasyap1/) 8 | - [**Silky Kumari**](https://www.linkedin.com/in/silky-kumari-953180199/) 9 | - [**Yugandhar Surya**](https://www.linkedin.com/in/yugandharsurya/) 10 | - [**P Bhavishya**](https://www.linkedin.com/in/p-bhavishya) 11 | 12 | ![contributors](https://contrib.rocks/image?repo=Anushka-codergirl/Java) 13 | -------------------------------------------------------------------------------- /Facts.md: -------------------------------------------------------------------------------- 1 | **5 Interesting Facts about Java** 2 | 3 | 1. Java was developed by James Gosling in 1995 at Sun Microsystems (later 4 | acquired by Oracle). 5 | 2. It was initially called Oak. Later it was renamed to Green and was finally 6 | renamed to Java inspired by Java coffee. 7 | 3. Java has close to 9 million developers worldwide. 8 | 4. About 3 billion mobile phones run Java, as well as 125 million TV sets and 9 | every Blu-Ray player. 10 | 5. According to indeed.com, the average salary of a Java developer is just over 11 | $100,000 per year in the US. 12 | 6. James and his team were working on setup box by modifying C++ and ended up with new programming language JAVA. 13 | 7. It only supports OPPS concept. 14 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2022 Anushka Raj 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /Programs/Algorithms/BubbleSort.java: -------------------------------------------------------------------------------- 1 | // Java program for implementation of Bubble Sort 2 | class BubbleSort 3 | { 4 | void bubbleSort(int arr[]) 5 | { 6 | int n = arr.length; 7 | for (int i = 0; i < n-1; i++) 8 | for (int j = 0; j < n-i-1; j++) 9 | if (arr[j] > arr[j+1]) 10 | { 11 | // swap arr[j+1] and arr[j] 12 | int temp = arr[j]; 13 | arr[j] = arr[j+1]; 14 | arr[j+1] = temp; 15 | } 16 | } 17 | 18 | /* Prints the array */ 19 | void printArray(int arr[]) 20 | { 21 | int n = arr.length; 22 | for (int i=0; i[] bucket = new ArrayList[n]; 12 | 13 | // Create empty buckets 14 | for (int i = 0; i < n; i++) 15 | bucket[i] = new ArrayList(); 16 | 17 | // Add elements into the buckets 18 | for (int i = 0; i < n; i++) { 19 | int bucketIndex = (int) arr[i] * n; 20 | bucket[bucketIndex].add(arr[i]); 21 | } 22 | 23 | // Sort the elements of each bucket 24 | for (int i = 0; i < n; i++) { 25 | Collections.sort((bucket[i])); 26 | } 27 | 28 | // Get the sorted array 29 | int index = 0; 30 | for (int i = 0; i < n; i++) { 31 | for (int j = 0, size = bucket[i].size(); j < size; j++) { 32 | arr[index++] = bucket[i].get(j); 33 | } 34 | } 35 | } 36 | 37 | // Driver code 38 | public static void main(String[] args) { 39 | BucketSort b = new BucketSort(); 40 | float[] arr = { (float) 0.42, (float) 0.32, (float) 0.33, (float) 0.52, (float) 0.37, (float) 0.47, 41 | (float) 0.51 }; 42 | b.bucketSort(arr, 7); 43 | 44 | for (float i : arr) 45 | System.out.print(i + " "); 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /Programs/Algorithms/Dikstra.java: -------------------------------------------------------------------------------- 1 | 2 | /* 3 | Write a program to implement Dijkstra's Algorithm. 4 | */ 5 | 6 | import java.util.*; 7 | public class Dikstra { 8 | final static int MAX = 20; 9 | final static int infinity = 9999; 10 | static int n; //No. of vertices of G 11 | static int G[][] = new int[MAX][MAX]; //Adjacency matrix of G 12 | static Scanner sc = new Scanner(System.in); 13 | 14 | public static void main(String [] args) 15 | { 16 | readMatrix(); 17 | int source = 0; //source vertex 18 | System.out.println("Enter the source vertex: "); 19 | source = sc.nextInt(); 20 | dijkstraAlgorithm(source); 21 | } 22 | 23 | static void readMatrix() 24 | { 25 | System.out.println("Enter the number of vertices: "); 26 | n = sc.nextInt(); 27 | System.out.println("Enter the adjacency matrix: "); 28 | for(int i=1; i<=n; i++) 29 | for(int j=1; j<=n; j++) 30 | G[i][j] = sc.nextInt(); 31 | } 32 | 33 | static void dijkstraAlgorithm(int source) 34 | { 35 | int S[] = new int[MAX]; //Set of vertices whose shortest path from source is found 36 | int dist[] = new int[MAX]; //Distance of vertices from source 37 | 38 | int u = 1,v=0; 39 | int i; 40 | for(i = 1; i<=n; i++) 41 | { 42 | S[i] = 0; //Initially no vertex is included in S 43 | dist[i] = G[source][i]; //Distance of vertices from source 44 | } 45 | S[source] = 1; //Source vertex is included in S 46 | dist[source] = 0; //Distance of source vertex from itself is 0 47 | 48 | i = 2; 49 | while(i<=n) 50 | { 51 | int min = infinity; 52 | for(int w=1; w<=n; w++) 53 | { 54 | if(S[w] == 0 && dist[w] < min) 55 | { 56 | min = dist[w]; 57 | u = w; 58 | } 59 | } 60 | S[u] = 1; 61 | i++; 62 | for(v=1; v<=n; v++) 63 | { 64 | if(S[v] == 0 && G[u][v] != infinity && dist[u] + G[u][v] < dist[v]) 65 | dist[v] = dist[u] + G[u][v]; 66 | } 67 | } 68 | for(i=1;i<=n;i++) 69 | if(i!=source) 70 | System.out.println(i+":"+dist[i]); 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /Programs/Algorithms/FIFO.java: -------------------------------------------------------------------------------- 1 | // Java program to demonstrate 2 | // working of FIFO 3 | // using Queue interface in Java 4 | 5 | import java.util.LinkedList; 6 | import java.util.Queue; 7 | 8 | public class QueueExample { 9 | public static void main(String[] args) 10 | { 11 | Queue q = new LinkedList<>(); 12 | 13 | // Adds elements {0, 1, 2, 3, 4} to queue 14 | for (int i = 0; i < 5; i++) 15 | q.add(i); 16 | 17 | // Display contents of the queue. 18 | System.out.println("Elements of queue-" + q); 19 | 20 | // To remove the head of queue. 21 | // In this the oldest element '0' will be removed 22 | int removedele = q.remove(); 23 | System.out.println("removed element-" + removedele); 24 | 25 | System.out.println(q); 26 | 27 | // To view the head of queue 28 | int head = q.peek(); 29 | System.out.println("head of queue-" + head); 30 | 31 | // Rest all methods of collection interface, 32 | // Like size and contains can be used with this 33 | // implementation. 34 | int size = q.size(); 35 | System.out.println("Size of queue-" + size); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /Programs/Algorithms/FloyadsAlgorithm.java: -------------------------------------------------------------------------------- 1 | /* 2 | Write a program to implement All-Pairs Shortest Paths problem using Floyd's algorithm. 3 | */ 4 | 5 | import java.util.*; 6 | 7 | public class FloyadsAlgorithm { 8 | 9 | static final int MAX = 20; 10 | static int n; //No. of vertices of G 11 | static int G[][] = new int[MAX][MAX]; //Adjacency matrix of G 12 | static Scanner sc = new Scanner(System.in); 13 | 14 | public static void main(String [] args) 15 | { 16 | readMatrix(); 17 | floydsAlgorithm(); 18 | printMatrix(); 19 | } 20 | 21 | static void readMatrix() 22 | { 23 | System.out.println("Enter the number of vertices: "); 24 | n = sc.nextInt(); 25 | System.out.println("Enter the adjacency matrix: "); 26 | for(int i=1; i<=n; i++) 27 | for(int j=1; j<=n; j++) 28 | G[i][j] = sc.nextInt(); 29 | } 30 | 31 | static void floydsAlgorithm() 32 | { 33 | for(int k=1; k<=n; k++) 34 | for(int i=1; i<=n; i++) 35 | for(int j=1; j<=n; j++) 36 | if(G[i][j] > G[i][k] + G[k][j]) 37 | G[i][j] = G[i][k] + G[k][j]; 38 | } 39 | 40 | static void printMatrix() 41 | { 42 | System.out.println("The shortest paths are: "); 43 | for(int i=1; i<=n; i++) 44 | { 45 | for(int j=1; j<=n; j++) 46 | System.out.print(G[i][j] + " "); 47 | System.out.println(); 48 | } 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /Programs/Algorithms/Heap_Sort.java: -------------------------------------------------------------------------------- 1 | // Java program for implementation of Heap Sort 2 | 3 | public class HeapSort { 4 | public void sort(int arr[]) 5 | { 6 | int N = arr.length; 7 | 8 | // Build heap (rearrange array) 9 | for (int i = N / 2 - 1; i >= 0; i--) 10 | heapify(arr, N, i); 11 | 12 | // One by one extract an element from heap 13 | for (int i = N - 1; i > 0; i--) { 14 | // Move current root to end 15 | int temp = arr[0]; 16 | arr[0] = arr[i]; 17 | arr[i] = temp; 18 | 19 | // call max heapify on the reduced heap 20 | heapify(arr, i, 0); 21 | } 22 | } 23 | 24 | // To heapify a subtree rooted with node i which is 25 | // an index in arr[]. n is size of heap 26 | void heapify(int arr[], int N, int i) 27 | { 28 | int largest = i; // Initialize largest as root 29 | int l = 2 * i + 1; // left = 2*i + 1 30 | int r = 2 * i + 2; // right = 2*i + 2 31 | 32 | // If left child is larger than root 33 | if (l < N && arr[l] > arr[largest]) 34 | largest = l; 35 | 36 | // If right child is larger than largest so far 37 | if (r < N && arr[r] > arr[largest]) 38 | largest = r; 39 | 40 | // If largest is not root 41 | if (largest != i) { 42 | int swap = arr[i]; 43 | arr[i] = arr[largest]; 44 | arr[largest] = swap; 45 | 46 | // Recursively heapify the affected sub-tree 47 | heapify(arr, N, largest); 48 | } 49 | } 50 | 51 | /* A utility function to print array of size n */ 52 | static void printArray(int arr[]) 53 | { 54 | int N = arr.length; 55 | 56 | for (int i = 0; i < N; ++i) 57 | System.out.print(arr[i] + " "); 58 | System.out.println(); 59 | } 60 | 61 | // Driver's code 62 | public static void main(String args[]) 63 | { 64 | int arr[] = { 12, 11, 13, 5, 6, 7 }; 65 | int N = arr.length; 66 | 67 | // Function call 68 | HeapSort ob = new HeapSort(); 69 | ob.sort(arr); 70 | 71 | System.out.println("Sorted array is"); 72 | printArray(arr); 73 | } 74 | } 75 | -------------------------------------------------------------------------------- /Programs/Algorithms/InsertionSort.java: -------------------------------------------------------------------------------- 1 | // Insertion sort in Java 2 | 3 | import java.util.Arrays; 4 | 5 | class InsertionSort { 6 | 7 | void insertionSort(int array[]) { 8 | int size = array.length; 9 | 10 | for (int step = 1; step < size; step++) { 11 | int key = array[step]; 12 | int j = step - 1; 13 | 14 | // Compare key with each element on the left of it until an element smaller than 15 | // it is found. 16 | // For descending order, change keyarray[j]. 17 | while (j >= 0 && key < array[j]) { 18 | array[j + 1] = array[j]; 19 | --j; 20 | } 21 | 22 | // Place key at after the element just smaller than it. 23 | array[j + 1] = key; 24 | } 25 | } 26 | 27 | // Driver code 28 | public static void main(String args[]) { 29 | int[] data = { 9, 5, 1, 4, 3 }; 30 | InsertionSort is = new InsertionSort(); 31 | is.insertionSort(data); 32 | System.out.println("Sorted Array in Ascending Order: "); 33 | System.out.println(Arrays.toString(data)); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /Programs/Algorithms/LinearSearchExample.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | class LinearSearchExample 3 | { 4 | public static void main(String args[]) 5 | { 6 | int counter, num, item, array[]; 7 | 8 | // To capture user input 9 | Scanner input = new Scanner(System.in); 10 | System.out.println("Enter number of elements:"); 11 | num = input.nextInt(); 12 | 13 | // Creating array to store the all the numbers 14 | array = new int[num]; 15 | System.out.printf("Enter %d integers\n", num); 16 | 17 | 18 | // Loop to store each numbers in array 19 | for (counter = 0; counter < num; counter++) 20 | array[counter] = input.nextInt(); 21 | 22 | System.out.println("Enter the search value:"); 23 | item = input.nextInt(); 24 | 25 | for (counter = 0; counter < num; counter++) { 26 | if (array[counter] == item) { 27 | System.out.printf("%d is present at location %d.\n", item, (counter + 1)); 28 | /* 29 | * Item is found so to stop the search and to come out of the loop use break 30 | * statement. 31 | */ 32 | break; 33 | } 34 | } 35 | if (counter == num) 36 | System.out.println(item + " doesn't exist in array."); 37 | 38 | input.close(); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /Programs/Algorithms/MergeSort.java: -------------------------------------------------------------------------------- 1 | import java.util.Arrays; 2 | import java.util.Scanner; 3 | 4 | public class Solution { 5 | 6 | void merge(int a[], int l, int mid, int r) { 7 | 8 | int n1 = mid - l + 1; 9 | int n2 = r - mid; 10 | 11 | int left[] = new int[n1]; 12 | int right[] = new int[n2]; 13 | 14 | for (int i = 0; i < n1; ++i) 15 | left[i] = a[l + i]; 16 | 17 | for (int j = 0; j < n2; ++j) 18 | right[j] = a[mid + 1 + j]; 19 | 20 | int i = 0, j = 0; 21 | 22 | int k = l; 23 | while (i < n1 && j < n2) { 24 | 25 | if (left[i] <= right[j]) { 26 | a[k] = left[i++]; 27 | } else { 28 | a[k] = right[j++]; 29 | } 30 | 31 | k++; 32 | } 33 | 34 | while (i < n1) { 35 | a[k++] = left[i++]; 36 | } 37 | 38 | while (j < n2) { 39 | a[k++] = right[j++]; 40 | } 41 | } 42 | 43 | void sort(int a[], int left, int right) { 44 | int mid = 0; 45 | if (left < right) { 46 | 47 | mid = left + ((right - left) >> 1); 48 | 49 | sort(a, left, mid); 50 | sort(a, mid + 1, right); 51 | 52 | merge(a, left, mid, right); 53 | } 54 | } 55 | 56 | static void printArray(int a[]) { 57 | System.out.println(Arrays.toString(a)); 58 | } 59 | 60 | public static void main(String args[]) { 61 | Scanner sc = new Scanner(System.in); 62 | int numberOfElements = sc.nextInt(); 63 | int a[] = new int[numberOfElements]; 64 | 65 | for (int i = 0; i < numberOfElements; i++) { 66 | a[i] = sc.nextInt(); 67 | } 68 | 69 | Solution object = new Solution(); 70 | object.sort(a, 0, numberOfElements - 1); 71 | 72 | printArray(a); 73 | 74 | sc.close(); 75 | } 76 | } 77 | -------------------------------------------------------------------------------- /Programs/Algorithms/MergeSort.md: -------------------------------------------------------------------------------- 1 | # Problem 2 | 3 | Implementing Merge algorithm to sort an integer array in ascending order, and try to make it as fast as possible. 4 | 5 | ## Input Format 6 | 7 | First line:the size of the array Second line:the array to be sorted(numbers seperated by spaces) 8 | 9 | ## Constraints 10 | 11 | N <= 1000 12 | 13 | ## Output Format 14 | 15 | output between two brackets and each number seperated by a coma. 16 | 17 | ## Sample Input 18 | 19 | 6 20 | 21 | 1 9 3 5 4 7 22 | 23 | ## Sample Output 24 | 25 | [1,3,4,5,7,9] 26 | 27 | ## Explanation 28 | 29 | sort array in an ascending order. -------------------------------------------------------------------------------- /Programs/Algorithms/Prims.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | Scanner sc = new Scanner(System.in); 6 | 7 | int noOfNodes = sc.nextInt(); 8 | int numberOfEdges = sc.nextInt(); 9 | 10 | int[][] T = new int[noOfNodes][numberOfEdges]; 11 | int min = 0, result = 0; 12 | 13 | boolean[] visited = new boolean[numberOfEdges]; 14 | 15 | int v = 0; 16 | 17 | final int MAX_WEIGHT = 1000000; 18 | 19 | for (int i = 0; i < noOfNodes; i++) { 20 | Arrays.fill(T[i], MAX_WEIGHT); 21 | } 22 | 23 | for (int i = 0; i < numberOfEdges; i++) { 24 | 25 | int firstNode = sc.nextInt() - 1; 26 | int secondNode = sc.nextInt() - 1; 27 | int weight = sc.nextInt(); 28 | 29 | T[firstNode][secondNode] = weight; 30 | T[secondNode][firstNode] = T[firstNode][secondNode]; 31 | 32 | } 33 | 34 | for (int k = 1; k <= noOfNodes; k++) { 35 | min = MAX_WEIGHT; 36 | 37 | for (int i = 0; i < numberOfEdges; i++) { 38 | 39 | if (visited[i]){ 40 | for (int j = 0; j < numberOfEdges; j++) { 41 | 42 | if (!visited[j]) { 43 | if (min > T[i][j]) { 44 | min = T[i][j]; 45 | v = j; 46 | } 47 | } 48 | } 49 | } 50 | } 51 | 52 | visited[v] = true; 53 | 54 | if (min < MAX_WEIGHT) 55 | result += min; 56 | } 57 | 58 | System.out.println(result); 59 | 60 | sc.close(); 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /Programs/Algorithms/Prims.md: -------------------------------------------------------------------------------- 1 | # Prims Algorithm 2 | 3 | ## Given a graph which consists of several edges connecting its nodes, find a subgraph of the given graph with the following properties: 4 | 5 | - The subgraph contains all the nodes present in the original graph. 6 | - The subgraph is of minimum overall weight (sum of all edges) among all such subgraphs. 7 | - It is also required that there is exactly one, exclusive path between any two nodes of the subgraph. 8 | 9 | 10 | ## Input Format 11 | 12 | The first line has two space-separated integers *n* and *m*, the number of nodes and edges in the graph. 13 | 14 | Each of the next *m* lines contains three space-separated integers *x*, *y* and *r*, the end nodes of *edges*, and the edge's weight. 15 | The last line has an integer *start*, denoting the starting node. 16 | 17 | 18 | ## Constraints 19 | 20 | 2 <= n <= 3000 21 | 22 | 1 <= m <= ( n * (n-1) ) / 2 23 | 24 | 1 <= x,y,start <= n 25 | 26 | 0 <= r <= 1000 27 | 28 | 29 | 30 | ## Sample Input 31 | 32 | 5 6 33 | 34 | 1 2 3 35 | 36 | 1 3 4 37 | 38 | 4 2 6 39 | 40 | 5 2 2 41 | 42 | 2 3 5 43 | 44 | 3 5 7 45 | 46 | 1 47 | 48 | 49 | ## Sample Output 50 | 51 | 15 52 | -------------------------------------------------------------------------------- /Programs/Algorithms/RecursiveBubbleSort.java: -------------------------------------------------------------------------------- 1 | public class Program { 2 | public static void main(String[] args) { 3 | int[] arr = bubbleSort(new int[] { 64, 34, 25, 12, 22, 11, 90 }); 4 | for (int i = 0; i < arr.length; System.out.println(arr[i++])) 5 | ; 6 | 7 | } 8 | 9 | public static int[] bubbleSort(int[] arr) { 10 | for (int i = 0; i < arr.length; i++) { 11 | try { 12 | int num = arr[i]; 13 | if (arr[i + 1] < num) { 14 | arr[i] = arr[i + 1]; 15 | arr[i + 1] = num; 16 | bubbleSort(arr); 17 | } 18 | } catch (IndexOutOfBoundsException e) { 19 | continue; 20 | } 21 | } 22 | return arr; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /Programs/ArrayList Example/ReplaceOddSequenceWithCount.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | import java.io.*; 3 | 4 | public class ReplaceOddSequenceWithCount { 5 | public static int processArray(ArrayList array) { 6 | int count=0; 7 | for(int i=0;i1) //I had to replace length of odd seq greater than or equal to 2 13 | { 14 | array.set(i,count); //set curren count to current odd no and remove previous odd number 15 | array.remove(i-1); 16 | if(i>0) //For handling change in indices 17 | i=i-1; 18 | else 19 | i=0; 20 | } 21 | } 22 | else 23 | { 24 | count=0; 25 | } 26 | } 27 | return array.size(); 28 | } 29 | 30 | public static void main (String[] args) { 31 | ArrayList arrayL = new ArrayList(); 32 | Scanner s = new Scanner(System.in); 33 | while(s.hasNextInt()) { 34 | int number = s.nextInt(); 35 | if (number < 0) 36 | break; 37 | arrayL.add(new Integer(number)); 38 | } 39 | int new_l = processArray(arrayL); 40 | for(int i=0; i array[array.length - i]) 19 | .toArray(); 20 | } 21 | 22 | private static Object[] reverseUsingCollectionsMethod(Object[] objects) { 23 | Collections.reverse(Arrays.asList(objects)); 24 | return objects; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /Programs/ArraysExample/ArrayToArrayList.java: -------------------------------------------------------------------------------- 1 | import java.util.ArrayList; 2 | import java.util.List; 3 | 4 | public class ArrayToArrayList { 5 | public static void main(String args[]) { 6 | String[] alphabets= { "a", "b", "c", "d", "e" }; 7 | 8 | // Create a new list. Iterate the array and add each element to the list. 9 | List list = new ArrayList(); 10 | for (String alphabet : alphabets) { 11 | list.add(alphabet); 12 | } 13 | System.out.println(list); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /Programs/ArraysExample/Arrays.java: -------------------------------------------------------------------------------- 1 | public class Arrays { 2 | public static void main(String[] args) { 3 | int[] numbers = new int[4]; 4 | int numbers2[] = new int[]{ 120, 125, 520, 625 }; 5 | // int [] numbers3, numbers4 ,numbers5; 6 | 7 | numbers[0] = 10; 8 | numbers[1] = 15; 9 | numbers[2] = 20; 10 | numbers[3] = 25; 11 | 12 | for (int i = 0; i < numbers.length; i++) { 13 | System.out.println(numbers[i]); 14 | } 15 | 16 | for (int val : numbers2) { 17 | System.out.println(val); 18 | } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /Programs/ArraysExample/CompareArrays.java: -------------------------------------------------------------------------------- 1 | public class CompareArrays { 2 | public static void main(String[] args) { 3 | int[] numbers1 = { 2, 4, 6, 8, 10 }; 4 | int[] numbers2 = { 2, 4, 6, 8, 10 }; 5 | 6 | boolean areArraysEqual = true; 7 | int i = 0; 8 | 9 | int numbers1Length = numbers1.length, numbers2Length = numbers2.length; 10 | 11 | if (numbers1Length != numbers2Length) { 12 | areArraysEqual = false; 13 | } 14 | 15 | while (i < numbers1Length) { 16 | if (numbers1[i] != numbers2[i]) { 17 | areArraysEqual = false; 18 | break; 19 | } 20 | i++; 21 | } 22 | 23 | System.out.printf("They are %s same!\n", areArraysEqual? "the" : "not"); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /Programs/ArraysExample/CopyArrays.java: -------------------------------------------------------------------------------- 1 | import java.util.Arrays; 2 | 3 | public class CopyArrays { 4 | public static void main(String[] args) { 5 | int[] array1 = { 2, 4, 6, 8, 10 }; // size 5 6 | 7 | int[] array2 = new int[array1.length]; 8 | 9 | for (int i = 0; i < array1.length; i++) { 10 | array2[i] = array1[i]; 11 | } 12 | 13 | array1[2] = 30; 14 | printArray(array1); 15 | 16 | System.out.println(); 17 | printArray(array2); 18 | } 19 | 20 | public static void printArray(int[] array) { 21 | System.out.println(Arrays.toString(array)); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /Programs/ArraysExample/HighestAndLowest.java: -------------------------------------------------------------------------------- 1 | public class HighestAndLowest 2 | { 3 | public static void main(String[] args) 4 | { 5 | int [] numbers = {2,6,10,12,15,17,1}; 6 | 7 | int highest = numbers[0]; 8 | for(int i = 1; i < numbers.length; i++) 9 | { 10 | if(numbers[i] > highest) 11 | { 12 | highest = numbers[i]; 13 | } 14 | } 15 | System.out.println("The highest value is: " + highest); 16 | 17 | int lowest = numbers[0]; 18 | for(int i = 1; i nums[j]) 12 | { 13 | count++; 14 | } 15 | } 16 | arr[i]=count; 17 | } 18 | return arr; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /Programs/ArraysExample/Insert.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | class Insert 3 | { 4 | public static void main(String[] args) 5 | { 6 | int len, p,ele; 7 | Scanner sc = new Scanner(System.in); 8 | System.out.print("Enter length of an array:"); 9 | len = sc.nextInt(); 10 | int arr[] = new int[len+1]; 11 | System.out.println("Enter "+len+" elements:"); 12 | for(int i = 0; i < len; i++) 13 | { 14 | arr[i] = sc.nextInt(); 15 | } 16 | System.out.print("Enter the element which you want to insert:"); 17 | ele = sc.nextInt(); 18 | arr[len] = ele; 19 | System.out.print("After inserting : "); 20 | for(int i = 0; i rotateLeft(int d, List arr) { 25 | // Write your code here 26 | 27 | } 28 | 29 | } 30 | 31 | public class Solution { 32 | public static void main(String[] args) throws IOException { 33 | BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in)); 34 | BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH"))); 35 | 36 | String[] firstMultipleInput = bufferedReader.readLine().replaceAll("\\s+$", "").split(" "); 37 | 38 | int n = Integer.parseInt(firstMultipleInput[0]); 39 | 40 | int d = Integer.parseInt(firstMultipleInput[1]); 41 | 42 | List arr = Stream.of(bufferedReader.readLine().replaceAll("\\s+$", "").split(" ")) 43 | .map(Integer::parseInt) 44 | .collect(toList()); 45 | 46 | List result = Result.rotateLeft(d, arr); 47 | 48 | bufferedWriter.write( 49 | result.stream() 50 | .map(Object::toString) 51 | .collect(joining(" ")) 52 | + "\n" 53 | ); 54 | 55 | bufferedReader.close(); 56 | bufferedWriter.close(); 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /Programs/ArraysExample/MergeArray.java: -------------------------------------------------------------------------------- 1 | import java.util.ArrayList; 2 | import java.util.Arrays; 3 | import java.util.List; 4 | 5 | public class Main { 6 | public static void main(String args[]) { 7 | String a[] = { "M", "E", "R" }; 8 | String b[] = { "G", "E" }; 9 | List list = new ArrayList(Arrays.asList(a)); 10 | list.addAll(Arrays.asList(b)); 11 | Object[] c = list.toArray(); 12 | System.out.println(Arrays.toString(c)); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /Programs/ArraysExample/PasswordValidator.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class PasswordValidator { 4 | 5 | // Method to check if the password is valid 6 | public static boolean isValidPassword(String password) { 7 | if (password.length() < 8) { 8 | return false; 9 | } 10 | 11 | boolean hasUpperCase = false; 12 | boolean hasLowerCase = false; 13 | boolean hasDigit = false; 14 | boolean hasSpecialChar = false; 15 | 16 | // Iterate over each character in the password 17 | for (char c : password.toCharArray()) { 18 | if (Character.isUpperCase(c)) { 19 | hasUpperCase = true; 20 | } else if (Character.isLowerCase(c)) { 21 | hasLowerCase = true; 22 | } else if (Character.isDigit(c)) { 23 | hasDigit = true; 24 | } else if (c >= 33 && c <= 46 || c == 64) { 25 | hasSpecialChar = true; 26 | } 27 | } 28 | return hasUpperCase && hasLowerCase && hasDigit && hasSpecialChar; 29 | } 30 | 31 | public static void main(String[] args) { 32 | Scanner scanner = new Scanner(System.in); 33 | 34 | System.out.print("Enter password to validate: "); 35 | String password = scanner.nextLine(); 36 | 37 | if (isValidPassword(password)) { 38 | System.out.println("Password is valid."); 39 | } else { 40 | System.out.println("Password is invalid. Make sure it contains at least 8 characters, one uppercase letter, one lowercase letter, one digit, and one special character."); 41 | } 42 | scanner.close(); 43 | } 44 | 45 | } 46 | -------------------------------------------------------------------------------- /Programs/ArraysExample/RecursiveBinarySearch.java: -------------------------------------------------------------------------------- 1 | public class RecursiveBinarySearch { 2 | public static int recursiveBinarySearch(int arr[], int firstElement, int lastElement, int elementToSearch) { 3 | 4 | // termination condition 5 | if (lastElement >= firstElement) { 6 | int mid = firstElement + (lastElement - firstElement) / 2; 7 | 8 | // if the middle element is our goal element, return its index 9 | if (arr[mid] == elementToSearch) 10 | return mid; 11 | 12 | // if the middle element is bigger than the goal element 13 | // recursively call the method with narrowed data 14 | if (arr[mid] > elementToSearch) 15 | return recursiveBinarySearch(arr, firstElement, mid - 1, elementToSearch); 16 | 17 | // else, recursively call the method with narrowed data 18 | return recursiveBinarySearch(arr, mid + 1, lastElement, elementToSearch); 19 | } 20 | 21 | return -1; 22 | } 23 | public static void main(String[] args) { 24 | int index = recursiveBinarySearch(new int[]{3, 22, 27, 47, 57, 67, 89, 91, 95, 99}, 0, 10, 27); 25 | System.out.print(27+" found at index "+ index); 26 | } 27 | } -------------------------------------------------------------------------------- /Programs/ArraysExample/RemoveDuplicatesInArray.java: -------------------------------------------------------------------------------- 1 | import java.util.ArrayList; 2 | import java.util.Arrays; 3 | import java.util.Set; 4 | import java.util.TreeSet; 5 | 6 | public class RemoveDuplicatesInArray { 7 | 8 | //Remove Duplicate Character from 2 array and display array in sorted order 9 | public static void main(String[] args) { 10 | String[] array1 = {"Goa", "Gujarat", "Bihar", "Odisha", "Assam", "Kerala", "Goa", "Karnataka"}; 11 | String[] array2 = {"Karnataka", "Bihar", "Maharashtra", "West Bengal", "Tripura", "Uttar Pradesh", "Sikkim"}; 12 | 13 | ArrayList list = new ArrayList( Arrays.asList(array1)); 14 | // Merging 2 arrays 15 | list.addAll(Arrays.asList(array2)); 16 | System.out.println("Merged Array: "+list); 17 | Set set = new TreeSet(); 18 | // Duplicates removed and sorted 19 | set.addAll(list); 20 | System.out.println("Sorted after removing duplicates: "+set); 21 | 22 | // Converting back to array 23 | array1 = set.toArray(array1); 24 | System.out.println("Sorted Array: "+Arrays.toString(array1)); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /Programs/ArraysExample/SortArrays.java: -------------------------------------------------------------------------------- 1 | import java.util.Arrays; 2 | 3 | public class SortArrays 4 | { 5 | public static void main(String[] args) 6 | { 7 | int[] intArray = {0, 6, 2, 98, 32}; 8 | String[] stringArray = {"Superman", "Batman", "Spiderman", "Deadpool", "Ironman"}; 9 | 10 | SortIntArray(intArray); 11 | SortStringArray(stringArray); 12 | } 13 | 14 | private static void SortIntArray(int[] array) 15 | { 16 | // Loop through every element in the array 17 | for (int i=1; i 0; j--) 21 | { 22 | if (array[j] < array[j-1]) 23 | { 24 | // Swap the values around in the array 25 | int largerValue = array[j-1]; 26 | 27 | array[j-1] = array[j]; 28 | array[j] = largerValue; 29 | } 30 | } 31 | } 32 | 33 | System.out.println("Int array was sorted to " + Arrays.toString(array)); 34 | } 35 | 36 | private static void SortStringArray(String[] array) 37 | { 38 | // Loop through every element in the array 39 | for (int i=1; i 0; j--) 43 | { 44 | boolean compared = false; 45 | // Here, determining whether to values should swap places works differently to comparing ints 46 | // Chars have a decimal value associated with them, see http://www.asciitable.com/ 47 | // We can use these ascii values to determine the order the elements of the array should be in 48 | 49 | // You would need to convert all of the elements to be lower/upper case if you want these to be ignored 50 | // This is because upper and lower case characters have different ASCII values 51 | int lengthToCompare = array[j].length(); 52 | if (array[j].length() > array[j-1].length()) 53 | { 54 | lengthToCompare = array[j-1].length(); 55 | } 56 | for (int charPosition = 0; charPosition < lengthToCompare; charPosition++) 57 | { 58 | int firstElementAscii = (int) array[j-1].charAt(charPosition); 59 | int secondElementAscii = (int) array[j].charAt(charPosition); 60 | 61 | if (firstElementAscii != secondElementAscii) 62 | { 63 | compared = true; 64 | if (secondElementAscii < firstElementAscii) 65 | { 66 | // Swap the values around in the array 67 | String largerValue = array[j-1]; 68 | 69 | array[j-1] = array[j]; 70 | array[j] = largerValue; 71 | } 72 | break; 73 | } 74 | } 75 | // If we haven't been able to compare the strings (e.g. tower and towers), 76 | // because their first n chars match, where n is the length of the shortest string, 77 | // the longer string will be placed after the shorter one 78 | if (!compared) 79 | { 80 | if (array[j].length() > array[j-1].length()) 81 | { 82 | // Swap the values around in the array 83 | String largerValue = array[j-1]; 84 | 85 | array[j-1] = array[j]; 86 | array[j] = largerValue; 87 | } 88 | } 89 | } 90 | } 91 | 92 | System.out.println("String array was sorted to " + Arrays.toString(array)); 93 | } 94 | } -------------------------------------------------------------------------------- /Programs/ArraysExample/StringArray.java: -------------------------------------------------------------------------------- 1 | public class StringArray 2 | { 3 | public static void main(final String[] args) { 4 | final String[] names = getNames(); 5 | for (int i = 0; i < names.length; i++) { 6 | if (names[i] != null) { 7 | System.out.println(names[i].charAt(0)); 8 | } 9 | 10 | } 11 | } 12 | 13 | public static String[] getNames() { 14 | final String[] names = new String[6]; 15 | names[0] = "Anushka"; 16 | names[1] = "Silky"; 17 | names[2] = "Farhin"; 18 | names[3] = "Pragati"; 19 | names[4] = "Ruchi"; 20 | 21 | return names; 22 | 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /Programs/ArraysExample/SumAndAverage.java: -------------------------------------------------------------------------------- 1 | 2 | public class SumAndAverage 3 | { 4 | public static void main(String[] args) 5 | { 6 | int [] values = {1,2,3}; 7 | 8 | int sum = 0; 9 | for(int i = 0; i < values.length ;i++) 10 | { 11 | sum += values[i]; 12 | } 13 | double average = (double) sum / values.length; 14 | 15 | System.out.println("The sum is: " + sum); 16 | System.out.println("The average is :" + average); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /Programs/ArraysExample/TheArrayList.java: -------------------------------------------------------------------------------- 1 | import java.util.ArrayList; 2 | 3 | public class TheArrayList 4 | { 5 | public static void main(String[] args) { 6 | 7 | //Standard Java arrays are of a fixed length.After arrays are created 8 | //they cannot grow or shrink, which means that you must know in advance 9 | //how many elements an array will hold. 10 | 11 | //Array lists are created with an initial size.When this size is exceeded, 12 | //the collection is automatically enlarged.When objects are removed, 13 | //the array may be shrunk 14 | 15 | ArrayList names = new ArrayList<>(); 16 | 17 | names.add("Amaya");//0 18 | names.add("Arya");//1 19 | names.add("Aman");//2 20 | names.add("Jazz");//3 21 | names.add(4,"John");//4 22 | names.add(3,"Paul"); 23 | names.set(1,"Peter"); 24 | 25 | for (int i = 0; i< names.size();i++) 26 | { 27 | System.out.println(names.get(i)); 28 | } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /Programs/ArraysExample/Two_Sum.java: -------------------------------------------------------------------------------- 1 | //In this code a array is taken along with an target element. The sum of two elements matching with the target element is displayed 2 | 3 | 4 | import java.util.*; 5 | public class Two_Sum { 6 | public static void main(String args[]) 7 | { 8 | try (Scanner sc = new Scanner(System.in)) { 9 | int n = sc.nextInt(); 10 | // length of the array is taken 11 | int a[] = new int[n]; 12 | for(int i = 0; i < n; i++) 13 | { 14 | a[i] = sc.nextInt(); 15 | } 16 | int target=sc.nextInt(); 17 | // target value is initialzized 18 | int array[] = new int[2]; 19 | a=twoSum(array,target); 20 | // method is called 21 | for(int i=0;i<2;i++) 22 | { 23 | System.out.print(a[i]+" "); 24 | } 25 | System.out.println(); 26 | } 27 | } 28 | public static int[] twoSum(int[] n, int target) { 29 | Map nm=new HashMap<>(); 30 | // hashmap is created to store the array elements 31 | int array[]=new int[2]; 32 | int k=n.length; 33 | for(int i=0;i> nQueen(int n) { 4 | cols = new boolean[n]; 5 | leftDiagonal = new boolean[2*n]; 6 | rightDiagonal = new boolean[2*n]; 7 | result = new ArrayList<>(); 8 | List temp = new ArrayList<>(); 9 | for(int i=0;i> result,int n,int row,List comb){ 14 | if(row==n){ 15 | result.add(new ArrayList<>(comb)); 16 | return; 17 | } 18 | for(int col = 0;col > result 30 | = new ArrayList >(); 31 | static boolean[] cols,leftDiagonal,rightDiagonal; 32 | public static void main(String[] args) 33 | { 34 | int n = 4; 35 | List > res = nQueen(n); 36 | System.out.println(res); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /Programs/Basic-java-program/Age Calculator.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | class Date{ 4 | int date; 5 | int month; 6 | int year; 7 | } 8 | 9 | class Student { 10 | String name; 11 | Date dob; 12 | 13 | public Student(String n, Date db) 14 | { 15 | name = n; 16 | dob = db; 17 | } 18 | 19 | public void calAge() 20 | { 21 | int calcDate, calcMonth, calcYear; 22 | int feb=28; 23 | 24 | Date curD=new Date(); 25 | Scanner usIn = new Scanner(System.in); 26 | 27 | System.out.print("Enter Current : \nDate - "); 28 | curD.date = usIn.nextInt(); 29 | System.out.print("Month - "); 30 | curD.month = usIn.nextInt(); 31 | System.out.print("Year - "); 32 | curD.year = usIn.nextInt(); 33 | 34 | if (curD.year%4 == 0) { feb = 29; } 35 | else if(curD.year %4 != 0) { feb = 28; } 36 | 37 | int []months = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; 38 | months[1] = feb; 39 | 40 | if(dob.date > curD.date){ 41 | curD.month = curD.month -1; 42 | curD.date = curD.date + months[curD.month]; 43 | } 44 | if(dob.month > curD.month){ 45 | curD.year = curD.year - 1; 46 | curD.month = curD.month +12; 47 | } 48 | 49 | calcDate = curD.date - dob.date; 50 | calcMonth = curD.month - dob.month; 51 | calcYear = curD.year - dob.year; 52 | System.out.println("------------"); 53 | System.out.println(name+"'s Age is: "+ calcYear + " year, "+ calcMonth + " month, "+ calcDate + " day"); } 54 | } 55 | 56 | public class Main { 57 | 58 | public static void main(String[] args) { 59 | Date DOB = new Date(); 60 | Scanner userIn = new Scanner(System.in); 61 | 62 | System.out.println("-- Gregorian Calendar Age Calculator --"); 63 | System.out.print("Enter Name: "); 64 | String userN = userIn.nextLine(); 65 | 66 | System.out.print("Enter Date of Birth: \nDate - "); 67 | DOB.date = userIn.nextInt(); 68 | System.out.print("Month - "); 69 | DOB.month = userIn.nextInt(); 70 | System.out.print("Year - "); 71 | DOB.year = userIn.nextInt(); 72 | 73 | Student Std = new Student(userN, DOB); 74 | System.out.println("------------------------------"); 75 | Std.calAge(); 76 | System.out.println("------------------------------"); 77 | } 78 | 79 | } 80 | -------------------------------------------------------------------------------- /Programs/Basic-java-program/Array.java: -------------------------------------------------------------------------------- 1 | public class Array{ 2 | 3 | public static void main(String args []){ 4 | 5 | // System.out.print(args[0]); 6 | 7 | /*for(int i=0; i studentsList = new ArrayList(); 6 | ArrayList deptList = new ArrayList(); 7 | 8 | // one argunment Constructer when we pass the name of student in return the object 9 | University(String uni, String[] snames, String[] dnames) { 10 | uname=uni; 11 | // to find out number of names which are pass in this object we write for loop 12 | for (int i = 0; i < snames.length; i++) { 13 | studentsList.add(new Students(snames[i])); 14 | } 15 | for (int j = 0; j < dnames.length; j++) { 16 | deptList.add(new Departments(dnames[j])); 17 | } 18 | } // end of constructer 19 | 20 | // students class inside university class 21 | class Students { 22 | String snames; 23 | Departments dept; 24 | 25 | Students(String snames) { 26 | this.snames = snames; 27 | } 28 | 29 | void display() { 30 | System.out.print(" name of the student is : " + snames); 31 | 32 | } 33 | } 34 | 35 | class Departments { 36 | String dnames; 37 | ArrayListdeptstudents = new ArrayList(); 38 | 39 | Departments(String dnames) { 40 | this.dnames = dnames; 41 | } 42 | 43 | // display all the students name which are study in that department 44 | public void displayStudents() { 45 | for (int i = 0; i < deptstudents.size(); i++) { 46 | System.out.println(deptstudents.get(i).snames); 47 | 48 | } 49 | } 50 | } // end of departments class 51 | 52 | Departments getDepartments(String name) { 53 | for (int i = 0; i < deptList.size(); i++) { 54 | if (deptList.get(i).dnames.equals(name)) 55 | return deptList.get(i); 56 | } 57 | return null; 58 | } 59 | 60 | Students getstudents(String name) { 61 | for (int j = 0; j < studentsList.size(); j++) { 62 | if (studentsList.get(j).snames.equals(name)) 63 | return studentsList.get(j); 64 | } 65 | return null; 66 | } 67 | 68 | public void enrollIn(Departments d, Students s) { 69 | s.dept = d; 70 | d.deptstudents.add(s); 71 | } 72 | 73 | public void displayStudentsInDept(Departments d) { 74 | d.displayStudents(); 75 | } 76 | 77 | } 78 | // university class end here 79 | public class Charyo{ 80 | public static void main(String[] args) { 81 | String [] snames={ "Ali","Mohsin","Ahsan","Muhsin","Zakir"}; 82 | String [] dept= {"SW","CS"}; 83 | University muet=new University("Mehran UET ",snames,dept); 84 | muet.enrollIn(muet.getDepartments("SW"),muet.getstudents("Ali") ); 85 | muet.displayStudentsInDept(muet.getDepartments("SW")); 86 | } 87 | } 88 | -------------------------------------------------------------------------------- /Programs/Basic-java-program/CheckAlphabet.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | public class CheckAlphabetOrNot 3 | { 4 | public static void main(String[] args) { 5 | Scanner sc=new Scanner(System.in); 6 | System.out.println("Enter any Number..."); 7 | char ch=sc.next().charAt(0); 8 | if(ch>'a' && ch<'z' || ch>'A' && ch<'Z') 9 | { 10 | System.out.println("It is an alphabet"); 11 | }else 12 | { 13 | System.out.println("It is not an alphabet"); 14 | } 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /Programs/Basic-java-program/CheckAnagram.java: -------------------------------------------------------------------------------- 1 | import java.util.Arrays; 2 | import java.util.Scanner; 3 | public class CheckAnagramOrNot 4 | { 5 | public static void main(String[] args) 6 | { 7 | Scanner s=new Scanner(System.in); 8 | System.out.println("Enter any String"); 9 | String x=s.nextLine(); 10 | System.out.println("Enter any String"); 11 | String y=s.nextLine(); 12 | char [] x1=x.toCharArray(); 13 | char [] y1=y.toCharArray(); 14 | Arrays.sort(x1); 15 | Arrays.sort(y1); 16 | Boolean result=Arrays.equals(x1,y1); 17 | if(result==true) 18 | System.out.println("It is an Anagram"); 19 | } 20 | else 21 | { 22 | System.out.println("It is not an Anagram"); 23 | } 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /Programs/Basic-java-program/CheckLeapYear.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | public class CheckLeapYearOrNot { 3 | public static void main(String[] args) { 4 | Scanner scan=new Scanner(System.in); 5 | System.out.println("Enter any Number"); 6 | int yr=scan.nextInt(); 7 | if(yr%4==0 && yr%100!=0) 8 | { 9 | System.out.println("It is a leap year"); 10 | } 11 | else 12 | if(yr%100==0 && yr%400==0) 13 | { 14 | System.out.println("It is leap year"); 15 | } 16 | else 17 | if(yr%400==0) 18 | { 19 | System.out.println("It is a leap year"); 20 | } 21 | else 22 | { 23 | System.out.println("It is not leap Year"); 24 | } 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /Programs/Basic-java-program/CheckPrimeNumber.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | public class CheckNumberPrimeOrNot 3 | { 4 | public static void main(String[] args) 5 | { 6 | Scanner s=new Scanner(System.in); 7 | System.out.println("Enter any number"); 8 | int x=s.nextInt(); 9 | boolean prime=true; 10 | for(int i=2;i0) 10 | { 11 | rem=n%10; 12 | rev=rev*10+rem; 13 | n/=10; 14 | } 15 | System.out.println("Reverse Number is "+rev); 16 | if(Orginal==rev) 17 | { 18 | System.out.println("It is Reverse is Equal"); 19 | } 20 | else 21 | { 22 | System.out.println("It is Reverse is Not eqaul"); 23 | } 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /Programs/Basic-java-program/Chips.java: -------------------------------------------------------------------------------- 1 | class University{ 2 | String uniName="Mehran UET"; 3 | 4 | static class Student{ 5 | 6 | static void display( ){ 7 | University muet = new University( ); 8 | 9 | String temp = muet.uniName; 10 | 11 | System.out.print("Inside desplay ( ) method of static nested class "); 12 | System.out.println("University is : " + temp); 13 | } 14 | } // Student class is end here 15 | 16 | public void outerDisplay( ){ 17 | System.out.println("Calling static nested class Display method "); 18 | 19 | Student.display( ); 20 | } 21 | } // end of university class 22 | 23 | class Chips{ 24 | 25 | public static void main (String args []){ 26 | University myuni=new University( ); 27 | 28 | myuni.outerDisplay(); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /Programs/Basic-java-program/Circle.java: -------------------------------------------------------------------------------- 1 | 2 | import java.util.Scanner; 3 | 4 | class Circle{ 5 | public static void main(String args []){ 6 | 7 | Scanner scan = new Scanner(System.in); 8 | 9 | System.out.print("Enter a radius of 2 circle num"); 10 | 11 | double r1 = scan.nextInt(); 12 | 13 | double r2 = scan.nextInt(); 14 | 15 | double circum1=r1*3.14*3.14; 16 | 17 | double circum2=r2*3.14* 3.14; 18 | 19 | System.out.println("Circumpherence of circle 1 is " + " " + circum1 ); 20 | System.out.println("Circumpherence of circle 2 is " + " " + circum2 ); 21 | 22 | double d1 = 2*r1; 23 | double d2 = 2*r2; 24 | 25 | System.out.println("Diameter of 1 is " + " " + d1 ); 26 | System.out.println("Diameter of 2 is " + " " + d2 ); 27 | 28 | double a1 = 3.14*r1*r1; 29 | double a2 = 3.14*r2*r2; 30 | 31 | System.out.println("area 1 is " + " " + a1 ); 32 | System.out.println("area 2 is " + " " + a2 ); 33 | 34 | 35 | } 36 | } 37 | 38 | 39 | -------------------------------------------------------------------------------- /Programs/Basic-java-program/Coin.java: -------------------------------------------------------------------------------- 1 | 2 | import java.util.Scanner; 3 | public class Coin{ 4 | 5 | public static void main(String args []){ 6 | 7 | Scanner input=new Scanner(System.in); 8 | 9 | System.out.println("Enter a Integer number : "); 10 | int x = input.nextInt(); 11 | 12 | int result=x/5000; 13 | System.out.println("5000 :" + result); 14 | int result2=result/1000; 15 | 16 | System.out.println("1000 :" + result); 17 | int result3=result2/500; 18 | 19 | System.out.println("500 :" + result); 20 | 21 | int result4=result3/100; 22 | 23 | System.out.println("50 :" + result4); 24 | } 25 | } 26 | 27 | 28 | -------------------------------------------------------------------------------- /Programs/Basic-java-program/Demo.java: -------------------------------------------------------------------------------- 1 | class Demo{ 2 | 3 | public static void main(String args []){ 4 | int i,j,sum; 5 | i=Integer.parseInt(args [0]); 6 | j=Integer.parseInt(args [1]); 7 | sum=i+j; 8 | System.out.print(sum); 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /Programs/Basic-java-program/Demo4.java: -------------------------------------------------------------------------------- 1 | class Student { 2 | String name; 3 | int section; 4 | String rollnum; 5 | Student(){ 6 | name="Zobiya jumani"; 7 | System.out.println(" Name is: " + name); 8 | section= 01; 9 | System.out.println("section is :" + section); 10 | rollnum = "21sw140"; System.out.println("RollNUmber is :" + rollnum); } 11 | // we also make overloading Constructor which is have no return type 12 | Student(String n, int s, String rollnu){ 13 | name=n; 14 | section=s; 15 | rollnum=rollnu; 16 | System.out.println("Overloading Constructor is Called"); 17 | System.out.println(" Name is: " + name); 18 | System.out.println("section is :" + section); 19 | System.out.println("RollNUmber is :" + rollnum); 20 | 21 | } 22 | } 23 | public class Demo4{ 24 | public static void main(String args[]){ 25 | Student s1=new Student(); 26 | System.out.println(" "); 27 | Student s2=new Student("Sawera",01,"21sw129"); 28 | } 29 | } 30 | 31 | -------------------------------------------------------------------------------- /Programs/Basic-java-program/Demo5.java: -------------------------------------------------------------------------------- 1 | class University{ 2 | public static void display(){ 3 | System.out.println("University name is MUET UET jamshoro"); 4 | } 5 | } 6 | public class Demo5{ 7 | public static void main(String args []){ 8 | University.display(); 9 | } 10 | } -------------------------------------------------------------------------------- /Programs/Basic-java-program/Demo6.java: -------------------------------------------------------------------------------- 1 | import java.util.ArrayList; 2 | class Continent{ 3 | 4 | String cName; 5 | ArrayList listOfCountries = new ArrayList(); 6 | double size; 7 | 8 | Continent( String cName, String[] conName){ 9 | this.cName=cName; 10 | for (int i=0; i listOfCities = new ArrayList(); 26 | City capitalCity = new City(); 27 | 28 | Countries(String conName, String[] cityName){ 29 | this.conName=conName; 30 | for (int i=0; i=0; i--){ 13 | for(int j=1; j<=3-i; j++) 14 | System.out.print(" "); 15 | for(int k=1; k<=(i*2)-1; k++) 16 | System.out.print("*"); 17 | System.out.println(" "); 18 | } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /Programs/Basic-java-program/Employee.java: -------------------------------------------------------------------------------- 1 | class Employee{ 2 | private String eName; 3 | private String eDepartment; 4 | private double ePhonenNum; 5 | 6 | public void setEName(String eName){ this.eName=eName;} 7 | public void seteDepartment (String eDepartment){ this.eDepartment=eDepartment;} 8 | public void setePhonenNum(double ePhonenNum){ this.ePhonenNum=ePhonenNum;} 9 | 10 | public String geteName(){ return eName;} 11 | public String geteDepartment(){ return eDepartment;} 12 | public double getePhonenNum(){ return ePhonenNum;} 13 | 14 | public static void main(String args []){ 15 | Employee obj = new Employee(); 16 | obj.setEName("Mubeen"); 17 | obj.seteDepartment("Berozgar"); 18 | obj.setePhonenNum(9234567891d); 19 | 20 | System.out.println(obj.geteName()); 21 | System.out.println(obj.geteDepartment()); 22 | System.out.println(obj.getePhonenNum()); 23 | } 24 | } -------------------------------------------------------------------------------- /Programs/Basic-java-program/Even.java: -------------------------------------------------------------------------------- 1 | class Even{ 2 | 3 | public static void main(String args []){ 4 | 5 | int num=1; 6 | 7 | while(num<=50){ 8 | 9 | if(num%2==0){ 10 | System.out.println( "Num is Even : " + num); 11 | } 12 | 13 | else{ 14 | System.out.println( "Num is odd : " + num); 15 | } 16 | ++num; 17 | } 18 | 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /Programs/Basic-java-program/ExceptionTask1.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | public class ExceptionTask1 { 3 | public static void main(String[] args) { 4 | Scanner sc= new Scanner(System.in); 5 | System.out.println("Enter num one : "); 6 | int num1= sc.nextInt(); 7 | System.out.println("Enter num two : "); 8 | int num2=sc.nextInt(); 9 | int result=0; 10 | try { 11 | result = num1/num2; 12 | } 13 | catch (ArithmeticException e){ 14 | System.out.println("Divide by 0 is not valid this is ArithmeticException"); 15 | } 16 | 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /Programs/Basic-java-program/Fabbonaccie.java: -------------------------------------------------------------------------------- 1 | class Fabbonaccie{ 2 | 3 | static int first=0, second=1,next; 4 | static int num; 5 | 6 | static void fab(int num) 7 | { 8 | num=10; 9 | for(int i=1; i<=num; i++){ 10 | 11 | System.out.print(first+ ","); 12 | 13 | next=first+second; 14 | first=second; 15 | second=next; 16 | } 17 | } 18 | public static void main(String args []){ 19 | 20 | System.out.println("The Fabbonaccie series is : "); 21 | 22 | fab(num); 23 | } 24 | } 25 | 26 | -------------------------------------------------------------------------------- /Programs/Basic-java-program/Face.java: -------------------------------------------------------------------------------- 1 | class Face{ 2 | 3 | public static void main(String args [ ]){ 4 | 5 | 6 | 7 | System.out.println(" +\" \"+"); 8 | System.out.println("[| o o |] "); 9 | System.out.println(" | ^ | "); 10 | System.out.println(" | '-' | "); 11 | } 12 | } 13 | 14 | 15 | -------------------------------------------------------------------------------- /Programs/Basic-java-program/Factorial.java: -------------------------------------------------------------------------------- 1 | class Factorial{ 2 | 3 | 4 | static int num; 5 | 6 | static void fac(int num) 7 | { 8 | int fact=1; 9 | num=5; 10 | for(int i=1; i<=num; i++) 11 | { 12 | fact*=i; 13 | } 14 | System.out.println(fact); 15 | } 16 | public static void main(String args []){ 17 | 18 | System.out.println("The Factorial is : "); 19 | 20 | fac(num); 21 | } 22 | } 23 | 24 | -------------------------------------------------------------------------------- /Programs/Basic-java-program/Glass.java: -------------------------------------------------------------------------------- 1 | class CarCompany{ 2 | 3 | 4 | class Car{ 5 | 6 | String carType = "Electric"; 7 | String color = "Black"; 8 | int engineNo = 1776; 9 | int capacity = 133; 10 | static int noOfWheels= 4; 11 | 12 | void display(){ 13 | 14 | System.out.print("Inside desplay ( ) method of nested class "); 15 | System.out.println("Car Type is : " + carType); 16 | System.out.println("Color is is : " + color); 17 | System.out.println("Engine no is : " + engineNo); 18 | System.out.println("Capacity is : " + capacity); 19 | System.out.println("Number of wheels is : " + noOfWheels); 20 | } 21 | }// Inner class is closed here 22 | 23 | public void outerDisplay( ){ 24 | System.out.println("Calling nested class Display method "); 25 | 26 | Car sedan = new Car(); 27 | Car hatchback = new Car(); 28 | sedan.display(); 29 | } 30 | } 31 | 32 | class Glass{ 33 | 34 | public static void main(String args [] ){ 35 | 36 | CarCompany myCompany= new CarCompany(); 37 | myCompany.outerDisplay(); 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /Programs/Basic-java-program/Google.java: -------------------------------------------------------------------------------- 1 | class Google{ 2 | String firstName; 3 | String middleName; 4 | String lastName; 5 | 6 | void setName(String f) { firstName=f;} 7 | 8 | 9 | void setName(String f, String m) { firstName=f; middleName=m; } 10 | 11 | void setName(String f, String m, String l) { firstName=f; middleName=m;lastName=l; } 12 | 13 | String getName() 14 | { return firstName + middleName + lastName;} 15 | 16 | public static void main(String args[]){ 17 | Google n1; 18 | n1 = new Google(); 19 | n1.setName("Irshad", "Ahmed", "Jumani"); 20 | n1.getName(); 21 | } 22 | } 23 | 24 | -------------------------------------------------------------------------------- /Programs/Basic-java-program/Hello.java: -------------------------------------------------------------------------------- 1 | class Hello{ 2 | 3 | public static void main(String args [ ]){ 4 | 5 | System.out.print("Hello World !!"); 6 | 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /Programs/Basic-java-program/Hellos.java: -------------------------------------------------------------------------------- 1 | class Example{ 2 | 3 | int x; // Instance member 4 | private static int y; // Static member variable 5 | 6 | public void fun1( ) { }// Instance member function 7 | public static int fun2( ) 8 | {return y=4;} // static member function 9 | 10 | static class Test{ 11 | public static String country="Pakistan"; 12 | } 13 | } 14 | 15 | public class Hellos{ 16 | public static void main(String[] args) 17 | { 18 | Example ex1=new Example(); 19 | 20 | System.out.print(Example.fun2( )); 21 | System.out.println(Example.Test.country); 22 | } 23 | } -------------------------------------------------------------------------------- /Programs/Basic-java-program/Homework.java: -------------------------------------------------------------------------------- 1 | public class Homework{ 2 | String carType; 3 | String color; 4 | 5 | public static void main(String args[]){ 6 | 7 | Car car1; 8 | car1 = new Car(); 9 | car1.color = "red"; 10 | System.out.print (car1); 11 | } 12 | } -------------------------------------------------------------------------------- /Programs/Basic-java-program/Human.java: -------------------------------------------------------------------------------- 1 | public class Human { 2 | Clothes c1 = new Clothes(); 3 | 4 | 5 | public class Eye { 6 | 7 | int FPS; 8 | int MegaPixel; 9 | 10 | } 11 | 12 | public class Heart { 13 | 14 | int pumpingRate; 15 | int BloodRange; 16 | 17 | } 18 | 19 | public class Lungs { 20 | int Sizeoflungs; 21 | 22 | public void display() { 23 | System.out.println("Lungs can work"); 24 | } 25 | 26 | } 27 | 28 | public class Brain { 29 | int neorones; 30 | int sizeOfNeorones; 31 | 32 | } 33 | 34 | public class Clothes { 35 | int colorOfClothes; 36 | String PentShirt; 37 | String KurtaShalwar; 38 | } 39 | Lungs l1 = new Lungs(); 40 | Human(){ 41 | l1.display( ); 42 | } 43 | 44 | 45 | 46 | 47 | public static void main(String args[]){ 48 | Human h1=new Human(); 49 | 50 | 51 | }} 52 | 53 | -------------------------------------------------------------------------------- /Programs/Basic-java-program/Input.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | class Input{ 4 | 5 | public static void main(String args []){ 6 | 7 | Scanner scan = new Scanner(System.in); 8 | System.out.print("Enter a number : "); 9 | 10 | int a =scan.nextInt(); 11 | System.out.println("Enter a second number "); 12 | int b = scan.nextInt(); 13 | int sum = a+b; 14 | System.out.println(" Sum is " + " " + sum); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /Programs/Basic-java-program/Logical.java: -------------------------------------------------------------------------------- 1 | public class Logical{ 2 | 3 | public static void main(String args[]) 4 | { 5 | int x=4, y=3,z=-4; 6 | boolean result; 7 | result=(int) x==y && y==x; 8 | System.out.print(result); 9 | System.out.println( ); 10 | 11 | result= x>y || y>x; 12 | System.out.println(result); 13 | 14 | System.out.println( ); 15 | 16 | result= x>y != y studentList = new ArrayList(); 6 | ArrayList deptlist = new ArrayList(); 7 | 8 | University(String uname, String[] snames,String[] dnames){ 9 | this.uname=uname; 10 | for (int i=0; i deptstudents = new ArrayList(); 32 | Departments(String dnames){ 33 | this.dnames=dnames; 34 | } 35 | 36 | public void displayStudents() { 37 | for (int i = 0; i < deptstudents.size(); i++) { 38 | System.out.println(deptstudents.get(i).snames); 39 | } 40 | } 41 | 42 | } 43 | 44 | Departments getDepartments(String name){ 45 | for (int i=0; i studentList = new ArrayList ( ); 9 | ArrayList deptList = new ArrayList ( ); 10 | 11 | // and we have to initialized the Studentlist and arraylist so one thing is that we pass the array list in main that is the way we intialized the student and dept list 12 | University(String univ, String[] snames, String[] dnames{ 13 | 14 | uname=univ; 15 | 16 | // For loop to find out number of names which are pass in this object means how many 17 | for(int i=0; i deptstudents = new ArrayList ( ); 48 | Departments(String dname) { this.dname=dname;} 49 | 50 | 51 | // we make now method in which print the students name that study in which department 52 | void displayStudents(){ 53 | for(int i=0; i=90){ 38 | System.out.print("Grade is A+"); 39 | } 40 | 41 | else if(percentage==80 || percentage >=80){ 42 | System.out.print("Grade is B+"); 43 | } 44 | 45 | else if(percentage==70 || percentage>=70){ 46 | System.out.print("Grade is B"); 47 | } 48 | 49 | 50 | else if(percentage==60 || percentage >=60 ){ 51 | System.out.print("Grade is B"); 52 | } 53 | 54 | 55 | else if(percentage<50 ){ 56 | System.out.print("Student is fail Sorry "); 57 | } 58 | 59 | } 60 | } -------------------------------------------------------------------------------- /Programs/Basic-java-program/StudentInform.java: -------------------------------------------------------------------------------- 1 | class StudentInform{ 2 | 3 | String name; 4 | byte age; 5 | char gender; 6 | String rollNum; 7 | String departmentName; 8 | int section; 9 | 10 | public void setAge(byte a) 11 | { age=a;} 12 | 13 | public void setName(String n) 14 | { name=n;} 15 | 16 | public void setGender(char g) 17 | { gender=g;} 18 | public void setRollnum(String rn) 19 | { rollNum=rn;} 20 | public void setDepname(String d) 21 | { departmentName=d;} 22 | public void setsection(int s) 23 | { section=s;} 24 | 25 | public String getname( ){ return name;} 26 | public byte getage( ) { return age;} 27 | public char getgender( ) { return gender;} 28 | public String getrollNum( ) { return rollNum; } 29 | public String getdepName( ) { return departmentName; } 30 | public int getsection() { return section; } 31 | 32 | public void display( ){ 33 | System.out.print("Name is : " + name ); 34 | System.out.println( ); 35 | 36 | System.out.print("Age is : " + age ); 37 | System.out.println( ); 38 | 39 | System.out.print("Gender is : " + gender ); 40 | System.out.println( ); 41 | 42 | System.out.print("Roll number is : " + rollNum); 43 | System.out.println( ); 44 | 45 | System.out.print("Department is : " + departmentName ); 46 | System.out.println( ); 47 | 48 | System.out.print("Section is : " + section); 49 | } 50 | 51 | 52 | public static void main(String args []){ 53 | StudentInform Student1 ; 54 | Student1= new StudentInform( ); 55 | Student1.setName=("Zobiya"); 56 | } 57 | } -------------------------------------------------------------------------------- /Programs/Basic-java-program/Students.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | class Students{ 4 | 5 | public static void main(String args []){ 6 | 7 | Scanner input = new Scanner(System.in); 8 | 9 | System.out.print("Enter PF subject marks : " ); 10 | 11 | int sub1 = input.nextInt(); 12 | 13 | System.out.print("Enter IICT subject marks : " ); 14 | 15 | int sub2 = input.nextInt(); 16 | 17 | System.out.print("Enter Ac subject marks : " ); 18 | 19 | int sub3 = input.nextInt(); 20 | 21 | System.out.print("Enter AP subject marks : " ); 22 | 23 | int sub4 = input.nextInt(); 24 | 25 | System.out.print("Enter FE subject marks : " ); 26 | 27 | int sub5 = input.nextInt(); 28 | 29 | float sum; 30 | 31 | sum=sub1+sub2+sub3+sub4+sub5; 32 | 33 | float percentage = (sum/450)*100; 34 | 35 | System.out.println("Percentage of a Student is : " + percentage+"%" ); 36 | 37 | if(percentage==90 || percentage >=90){ 38 | System.out.println("Grade is A+"); 39 | } 40 | 41 | else if(percentage==80 || percentage >=80){ 42 | System.out.println("Grade is A"); 43 | } 44 | 45 | else if(percentage==70 || percentage>=70){ 46 | System.out.println("Grade is B+"); 47 | } 48 | 49 | 50 | else if(percentage==60 || percentage >=60 ){ 51 | System.out.println("Grade is B"); 52 | } 53 | 54 | else if(percentage==50 || percentage >=50 ){ 55 | System.out.println("Grade is C"); 56 | } 57 | 58 | else if(percentage<50 ){ 59 | System.out.println("Student is fail Sorry "); 60 | } 61 | 62 | } 63 | } -------------------------------------------------------------------------------- /Programs/Basic-java-program/Sub.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | class Sub{ 4 | 5 | public static void main(String args []){ 6 | 7 | Scanner s1 = new Scanner(System.in); 8 | 9 | System.out.println("Enter a number 1 : "); 10 | int num1 = s1.nextInt(); 11 | 12 | System.out.println("Enter a number 2 : "); 13 | int num2 = s1.nextInt(); 14 | 15 | int sub= num1-num2; 16 | 17 | System.out.println("The Subtracte of a number is :"+ " " + sub); 18 | } 19 | } 20 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /Programs/Basic-java-program/Table.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | class Table{ 4 | 5 | public static void main(String args [ ] ){ 6 | 7 | Scanner scan = new Scanner(System.in); 8 | System.out.print("Enter a start point : "); 9 | 10 | int start =scan.nextInt(); 11 | 12 | System.out.println("Enter a end point "); 13 | int end = scan.nextInt(); 14 | 15 | System.out.println("Enter a table no : "); 16 | int num = scan.nextInt(); 17 | int product; 18 | 19 | for( int i=1; i<=10; ++i){ 20 | if(i==end) 21 | break; 22 | product=num*start; 23 | System.out.println(num+"*"+start+"="+product); 24 | 25 | start++; 26 | 27 | } 28 | 29 | } 30 | } 31 | 32 | -------------------------------------------------------------------------------- /Programs/Basic-java-program/Task.java: -------------------------------------------------------------------------------- 1 | public class Task{ 2 | 3 | public static void main(String[ ] args){ 4 | 5 | for(int i=0; i Studentlist = new ArrayList ( ); 8 | ArrayList deptlist = new ArrayList ( ); 9 | 10 | University(String univ, String[ ] snames, String[] dnames){ 11 | 12 | uname=univ; 13 | // to find out number of names which are pass in this object 14 | 15 | for(int i=0, i deptstudents = new ArrayList ( ); 41 | 42 | Departments(String dname){ 43 | this.dname = dname;} 44 | 45 | void displayStudents(){ 46 | for(int i=0; i 2 | 3 | 6 | 7 | CSV File Upload 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | -------------------------------------------------------------------------------- /Programs/CSVFileImportAndConversionFromUTF8BOMtoUTF8/FileUploadBean.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | * @author Anushka 4 | */ 5 | import java.io.*; 6 | import java.nio.charset.StandardCharsets; 7 | import javax.faces.application.FacesMessage; 8 | import javax.faces.bean.ManagedBean; 9 | import javax.faces.bean.ViewScoped; 10 | import javax.faces.context.FacesContext; 11 | import javax.servlet.http.Part; 12 | 13 | @ManagedBean(name="fileUploadBean") 14 | @ViewScoped 15 | public class FileUploadBean implements Serializable { 16 | 17 | private Part file; 18 | 19 | // Getter and Setter for the file 20 | public Part getFile() { 21 | return file; 22 | } 23 | 24 | public void setFile(Part file) { 25 | this.file = file; 26 | } 27 | 28 | // Method to handle file upload and conversion 29 | public void processFile() { 30 | if (file != null) { 31 | try { 32 | // Save the uploaded file temporarily 33 | String tempFilePath = FacesContext.getCurrentInstance().getExternalContext().getRealPath("/") + "temp.csv"; 34 | File tempFile = new File(tempFilePath); 35 | 36 | // Write the uploaded file to the temp file 37 | try (InputStream input = file.getInputStream(); 38 | FileOutputStream output = new FileOutputStream(tempFile)) { 39 | byte[] buffer = new byte[1024]; 40 | int bytesRead; 41 | while ((bytesRead = input.read(buffer)) != -1) { 42 | output.write(buffer, 0, bytesRead); 43 | } 44 | } 45 | 46 | // Path to the output file (without BOM) 47 | String outputFilePath = FacesContext.getCurrentInstance().getExternalContext().getRealPath("/") + "converted_utf8.csv"; 48 | 49 | // Convert the file from UTF-8 BOM to UTF-8 50 | convertUTF8BOMToUTF8(tempFilePath, outputFilePath); 51 | 52 | // Inform the user that the file was processed successfully 53 | FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("File converted and saved successfully as UTF-8!")); 54 | 55 | } catch (IOException e) { 56 | FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "File processing error", e.getMessage())); 57 | } 58 | } else { 59 | FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_WARN, "No file selected", "")); 60 | } 61 | } 62 | 63 | // Method to convert a CSV file from UTF-8 BOM to UTF-8 64 | public void convertUTF8BOMToUTF8(String inputFilePath, String outputFilePath) { 65 | try ( 66 | // Reader to read the source file with UTF-8 encoding 67 | BufferedReader reader = new BufferedReader( 68 | new InputStreamReader(new FileInputStream(inputFilePath), StandardCharsets.UTF_8)); 69 | 70 | // Writer to write the new file in UTF-8 without BOM 71 | BufferedWriter writer = new BufferedWriter( 72 | new OutputStreamWriter(new FileOutputStream(outputFilePath), StandardCharsets.UTF_8)); 73 | ) { 74 | // Detect BOM (0xEF, 0xBB, 0xBF) and skip if present 75 | reader.mark(3); 76 | if (reader.read() != 0xEF || reader.read() != 0xBB || reader.read() != 0xBF) { 77 | // No BOM detected, reset the reader to start from the beginning 78 | reader.reset(); 79 | } 80 | 81 | // Read and copy the content line by line 82 | String line; 83 | while ((line = reader.readLine()) != null) { 84 | line = line.replaceAll("[^\\x20-\\x7E]", " "); // Removes non-printable characters 85 | writer.write(line); 86 | writer.newLine(); 87 | } 88 | 89 | System.out.println("File conversion from UTF-8 BOM to UTF-8 completed successfully!"); 90 | 91 | } catch (IOException e) { 92 | e.printStackTrace(); 93 | } 94 | } 95 | } -------------------------------------------------------------------------------- /Programs/Classes and Object Example/DriverClass.java: -------------------------------------------------------------------------------- 1 | public class DriverClass 2 | { 3 | public static void main(String[] args) 4 | { 5 | House house = new House(10,2,"Tiles","Brick"); 6 | house.setNrOfDoors(3); 7 | house.setTypeOfWall("Plaster"); 8 | System.out.println("House:"); 9 | System.out.println("Number of windows: " + house.getNrOfWindows()); 10 | System.out.println("Number of doors: " + house.getNrOfDoors()); 11 | System.out.println("Type of roof: " + house.getTypeOfRoof()); 12 | System.out.println("Type of wall: " + house.getTypeOfWall()); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /Programs/Classes and Object Example/House.java: -------------------------------------------------------------------------------- 1 | public class House 2 | { 3 | private int nrOfWindows; 4 | private int nrOfDoors; 5 | private String typeOfRoof; 6 | private String typeOfWall; 7 | 8 | public House() 9 | { 10 | nrOfWindows = 0; 11 | nrOfDoors = 0; 12 | typeOfRoof = null; 13 | typeOfWall = null; 14 | } 15 | 16 | public House(int nrOfWindows, int nrOfDoors, String typeOfRoof, String typeOfWall) 17 | { 18 | this.nrOfWindows = nrOfWindows; 19 | this.nrOfDoors = nrOfDoors; 20 | this.typeOfRoof = typeOfRoof; 21 | this.typeOfWall = typeOfWall; 22 | 23 | } 24 | public int getNrOfWindows() 25 | { 26 | return nrOfWindows; 27 | } 28 | 29 | public int getNrOfDoors() 30 | { 31 | return nrOfDoors; 32 | } 33 | 34 | public String getTypeOfRoof() 35 | { 36 | return typeOfRoof; 37 | } 38 | 39 | public String getTypeOfWall() 40 | { 41 | return typeOfWall; 42 | } 43 | 44 | public void setNrOfWindows(int nrOfWindows) 45 | { 46 | this.nrOfWindows = nrOfWindows; 47 | } 48 | 49 | public void setNrOfDoors(int nrOfDoors) 50 | { 51 | this.nrOfDoors = nrOfDoors; 52 | } 53 | 54 | public void setTypeOfRoof(String typeOfRoof) 55 | { 56 | this.typeOfRoof = typeOfRoof; 57 | } 58 | 59 | public void setTypeOfWall(String typeOfWall) 60 | { 61 | this.typeOfWall = typeOfWall; 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /Programs/Classes and Object Example/README.md: -------------------------------------------------------------------------------- 1 | # Getting a Class Instance 2 | 3 | There are three patterns to get a Class instance: 4 | - from an object 5 | - from a known class 6 | - from the name of a class 7 | 8 | 9 | 10 | Pattern 1 11 | ```java 12 | Class class = "Hello".getClass(); 13 | ``` 14 | 15 | Pattern 2 16 | ```java 17 | Class class = String.class(); 18 | ``` 19 | 20 | Pattern 3 21 | ```java 22 | Class class = Class.forName("java.lang.String"); 23 | ``` -------------------------------------------------------------------------------- /Programs/Comments/Comments.java: -------------------------------------------------------------------------------- 1 | public class Comments 2 | { 3 | 4 | /** 5 | *This is the main method of the class 6 | * @param args This is just a variable 7 | */ 8 | 9 | public static void main(String[] args) 10 | { 11 | 12 | int firstNumber = 11; 13 | int secondNumber = 12; //this is the second number entered 14 | /*Hello 15 | * This is multiline comment*/ 16 | double average = (firstNumber + secondNumber) / 2.0; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /Programs/CompareStrings/CompareStrings.java: -------------------------------------------------------------------------------- 1 | import javax.swing.*; 2 | 3 | public class CompareStrings 4 | { 5 | public static void main(String[] args) 6 | { 7 | String name1 = JOptionPane.showInputDialog("Please enter name 1: "); 8 | String name2 = JOptionPane.showInputDialog("Please enter name 2:"); 9 | 10 | if (name1.compareTo(name2) == 0) 11 | { 12 | System.out.println("name1 is equal to name 2"); 13 | } 14 | if(name1.compareTo(name2) < 0) 15 | { 16 | System.out.println("name 1 is first and name 2 is second"); 17 | } 18 | if(name1.compareTo(name2) > 0) 19 | { 20 | System.out.println("name 2 is first and name 1 is second"); 21 | } 22 | System.exit(0); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /Programs/DataTypes/Salary.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class Salary { 4 | public static void main(String[] args) { 5 | Scanner myObj = new Scanner(System.in); 6 | 7 | System.out.println("Enter name:"); 8 | String name = myObj.nextLine(); 9 | 10 | System.out.println("\nEnter Age:"); 11 | int age = myObj.nextInt(); 12 | 13 | System.out.println("\nEnter Salary:"); 14 | double salary = myObj.nextDouble(); 15 | 16 | System.out.printf("Name: %s Age: %d Salary: %.2f", name, age, salary); 17 | 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /Programs/DataTypes/StudentRecord.java: -------------------------------------------------------------------------------- 1 | public class StudentRecord { 2 | public static void main(String[] args) { 3 | int Age = 21; 4 | double GPA = 4.0; 5 | char FirstInitial = 'A'; 6 | char LastInitial = 'A'; 7 | boolean hasPerfectAttendance = true; 8 | String Name = "Anushka"; 9 | 10 | System.out.println(Age); 11 | System.out.println(GPA); 12 | System.out.println(FirstInitial); 13 | System.out.println(LastInitial); 14 | System.out.println(hasPerfectAttendance); 15 | System.out.println(Name); 16 | } 17 | } -------------------------------------------------------------------------------- /Programs/Decision Structures/DecisionStructures.java: -------------------------------------------------------------------------------- 1 | import javax.swing.JOptionPane; 2 | 3 | public class DecisionStructures { 4 | 5 | public static void main(String[] args) { 6 | int number; 7 | 8 | String input; 9 | 10 | input = JOptionPane.showInputDialog("Please enter your mark: "); 11 | number = Integer.parseInt(input); 12 | /* 13 | * if (number == 5) { JOptionPane.showMessageDialog(null, "The number is exactly 5!"); } 14 | * else if (number > 10) { JOptionPane.showMessageDialog(null, "The number is greater than 10!"); } 15 | * else if(number > 5) { 16 | * JOptionPane.showMessageDialog(null,"The number is greater than 5!"); 17 | * } 18 | * else{ 19 | * JOptionPane.showMessageDialog(null,"The number is less than 5!"); 20 | * } 21 | */ 22 | 23 | if (number < 50 && number >= 45) { 24 | JOptionPane.showMessageDialog(null, "You can write re-assessment!"); 25 | } else if (number < 50) { 26 | JOptionPane.showMessageDialog(null, "You failed!"); 27 | } else { 28 | if (number >= 75) { 29 | JOptionPane.showMessageDialog(null, "You have a distinction!"); 30 | } else { 31 | JOptionPane.showMessageDialog(null, "You passed!"); 32 | } 33 | } 34 | System.exit(0); 35 | } 36 | 37 | } 38 | -------------------------------------------------------------------------------- /Programs/DecisionMaking/Demo1.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class Demo1{ 4 | 5 | public static void main(String[] args){ 6 | System.out.println("Choose a number between 1-10"); 7 | Scanner scanner = new Scanner(System.in); 8 | 9 | int inputNum = scanner.nextInt(); 10 | 11 | if(inputNum < 6){ 12 | System.out.println("Good Luck!!"); 13 | }else{ 14 | System.out.println("Try Again!!"); 15 | } 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /Programs/Dynamic_programming(DP)/Knapsack.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class Knapsack { 4 | 5 | public static void main(String[] args) { 6 | Scanner s = new Scanner(System.in); 7 | int n = s.nextInt(); 8 | int weight[] = new int[n]; 9 | 10 | for (int i = 0; i < n; i++) { 11 | weight[i] = s.nextInt(); 12 | } 13 | 14 | int value[] = new int[n]; 15 | for (int i = 0; i < n; i++) { 16 | value[i] = s.nextInt(); 17 | } 18 | 19 | int maxWeight = s.nextInt(); 20 | 21 | System.out.println(knapsack(weight, value, maxWeight, n)); 22 | } 23 | 24 | public static int knapsack(int[] weight, int value[], int maxWeight, int n) { 25 | return knapsack(weight, value, maxWeight, weight.length, 0); 26 | } 27 | 28 | public static int knapsack(int[] weight, int value[], int maxWeight, int n, int SI) { 29 | if (SI == n || maxWeight == 0) { 30 | return 0; 31 | } 32 | 33 | if (weight[SI] > maxWeight) { 34 | return knapsack(weight, value, maxWeight, n, SI + 1); 35 | } else { 36 | int opt1 = value[SI] + knapsack(weight, value, maxWeight - weight[SI], n, SI + 1); 37 | int opt2 = knapsack(weight, value, maxWeight, n, SI + 1); 38 | return Math.max(opt1, opt2); 39 | } 40 | } 41 | 42 | } -------------------------------------------------------------------------------- /Programs/Dynamic_programming(DP)/Longest_palindromic_subsequence.java: -------------------------------------------------------------------------------- 1 | public class Longest_palindromic_subsequence { 2 | 3 | public int longestPalindromeSubseq(String s) { 4 | int[][] dp = new int[s.length()][s.length()]; 5 | 6 | for (int i = s.length() - 1; i >= 0; i--) { 7 | dp[i][i] = 1; 8 | for (int j = i+1; j < s.length(); j++) { 9 | if (s.charAt(i) == s.charAt(j)) { 10 | dp[i][j] = dp[i+1][j-1] + 2; 11 | } else { 12 | dp[i][j] = Math.max(dp[i+1][j], dp[i][j-1]); 13 | } 14 | } 15 | } 16 | return dp[0][s.length()-1]; 17 | } 18 | } -------------------------------------------------------------------------------- /Programs/Dynamic_programming(DP)/MinimumCoinProblem.java: -------------------------------------------------------------------------------- 1 | 2 | import java.util.*; 3 | 4 | public class MinimumCoinProblem { 5 | static int dp[][]; 6 | 7 | 8 | public static void main(String[] args) { 9 | Scanner sc= new Scanner(System.in); 10 | int cost=sc.nextInt(); 11 | int n=sc.nextInt(); 12 | int coins[]= new int[n]; 13 | for(int i=0;i i) 40 | break; 41 | else 42 | dp[i] = Math.min(dp[i], 1 + dp[i - temp]); 43 | } 44 | } 45 | int res = dp[n]; 46 | 47 | return res; 48 | } 49 | -------------------------------------------------------------------------------- /Programs/Dynamic_programming(DP)/editdistance.java: -------------------------------------------------------------------------------- 1 | public class editdistance { 2 | public static int editM(String s , String t) { 3 | int m = s.length(); 4 | int n = t.length(); 5 | int storage[][]= new int[m+1][n+1]; 6 | for(int i=0;i<=m;i++) { 7 | for(int j=0;j<=n;j++) { 8 | storage[i][j]=-1; 9 | } 10 | } 11 | return editM(s,t,storage); 12 | 13 | } 14 | 15 | private static int editM(String s, String t, int[][] storage) { 16 | int m = s.length(); 17 | int n = t.length(); 18 | if(storage[m][n]!=-1) { 19 | return storage[m][n]; 20 | } 21 | if(m==0) { 22 | storage[m][n]= n; 23 | return storage[m][n]; 24 | } 25 | if(n==0) { 26 | storage[m][n]=m; 27 | return storage[m][n]; 28 | } 29 | if(s.charAt(0)==t.charAt(0)) { 30 | storage[m][n] = editM(s.substring(1),t.substring(1),storage); 31 | } 32 | else { 33 | //insert 34 | int op1 = editM(s,t.substring(1),storage); 35 | //delete 36 | int op2 = editM(s.substring(1),t,storage); 37 | //substituite 38 | int op3=editM(s.substring(1),t.substring(1),storage); 39 | 40 | storage[m][n]= 1+Math.min(op1,Math.min(op2, op3)); 41 | } 42 | return storage[m][n]; 43 | } 44 | 45 | public static void main(String[] args) { 46 | // TODO Auto-generated method stub 47 | String s = "adef"; 48 | String t = "gbde"; 49 | int res= editM(s,t); 50 | System.out.print(res); 51 | } 52 | 53 | } 54 | -------------------------------------------------------------------------------- /Programs/Dynamic_programming(DP)/longest_common_subsequence.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class longest_common_subsequence{ 4 | 5 | static void lcs(String S1, String S2, int m, int n) { 6 | int[][] LCS_table = new int[m + 1][n + 1]; 7 | 8 | // Building the mtrix in bottom-up way 9 | for (int i = 0; i <= m; i++) { 10 | for (int j = 0; j <= n; j++) { 11 | if (i == 0 || j == 0) 12 | LCS_table[i][j] = 0; 13 | else if (S1.charAt(i - 1) == S2.charAt(j - 1)) 14 | LCS_table[i][j] = LCS_table[i - 1][j - 1] + 1; 15 | else 16 | LCS_table[i][j] = Math.max(LCS_table[i - 1][j], LCS_table[i][j - 1]); 17 | } 18 | } 19 | 20 | int index = LCS_table[m][n]; 21 | int temp = index; 22 | 23 | char[] lcs = new char[index + 1]; 24 | lcs[index] = '\0'; 25 | 26 | int i = m, j = n; 27 | while (i > 0 && j > 0) { 28 | if (S1.charAt(i - 1) == S2.charAt(j - 1)) { 29 | lcs[index - 1] = S1.charAt(i - 1); 30 | 31 | i--; 32 | j--; 33 | index--; 34 | } 35 | 36 | else if (LCS_table[i - 1][j] > LCS_table[i][j - 1]) 37 | i--; 38 | else 39 | j--; 40 | } 41 | 42 | // Printing the sub sequences 43 | System.out.print("S1 : " + S1 + "\nS2 : " + S2 + "\nLCS: "); 44 | for (int k = 0; k <= temp; k++) 45 | System.out.print(lcs[k]); 46 | System.out.println(""); 47 | } 48 | 49 | public static void main(String[] args) { 50 | Scanner sc=new Scanner(System.in); 51 | String S1 = sc.next(); 52 | String S2 = sc.next(); 53 | int m = S1.length(); 54 | int n = S2.length(); 55 | lcs(S1, S2, m, n); 56 | } 57 | } -------------------------------------------------------------------------------- /Programs/Dynamic_programming(DP)/maximum_product_subarray.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | 3 | class Satkar 4 | { 5 | 6 | static int maxSubarrayProduct(int arr[],int n){ 7 | 8 | int max_ending_here = arr[0]; 9 | int min_ending_here = arr[0]; 10 | int max_so_far = arr[0]; 11 | 12 | for(int i=1;i 1) ? "things" : "thing"); 10 | 11 | /* 12 | * if (thing > 1) { 13 | * System.out.print("things"); 14 | * } else { 15 | * System.out.print("thing"); 16 | * } 17 | */ 18 | 19 | } 20 | 21 | } -------------------------------------------------------------------------------- /Programs/JDBC/Example/VaccineReport.java: -------------------------------------------------------------------------------- 1 | // Importing required classes 2 | import java.sql.Connection; 3 | import java.sql.DriverManager; 4 | import java.sql.ResultSet; 5 | import java.sql.SQLException; 6 | import java.sql.Statement; 7 | import java.util.Scanner; 8 | 9 | public class VaccineReport { 10 | 11 | public static void main(String[] args) throws ClassNotFoundException, SQLException { 12 | 13 | //Read user input 14 | Scanner sc = new Scanner(System.in); 15 | System.out.println("Enter vaccine name to see data"); 16 | String vacName = sc.nextLine(); 17 | System.out.println("You choose " + vacName + " vaccine! Available vaccine centers are: "); 18 | 19 | try { 20 | Class.forName("com.mysql.jdbc.Driver"); 21 | 22 | Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/vaccineDB", "root", "anushkaraj@11"); 23 | //Create a statement 24 | 25 | Statement stmt = con.createStatement(); 26 | ResultSet rs = stmt.executeQuery("select * from data where vacName ='" + vacName + "' "); 27 | 28 | while (rs.next()) { 29 | String vaccineCenterName = rs.getString("vacCtName"); 30 | System.out.println(vaccineCenterName); 31 | } 32 | con.close(); 33 | } catch (SQLException ex) { 34 | ex.printStackTrace(); 35 | } 36 | 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /Programs/JavaNIOAPI/CSVFile.java: -------------------------------------------------------------------------------- 1 | import java.io.BufferedReader; 2 | import java.io.IOException; 3 | import java.nio.file.Files; 4 | import java.nio.file.Path; 5 | import java.nio.file.Paths; 6 | import java.io.Reader; 7 | import java.util.stream.*; 8 | 9 | import model.Person; 10 | 11 | public class CSVFile { 12 | public static void main(String[] args) { 13 | 14 | Path path = Paths.get("Files/data.csv"); 15 | 16 | try (Stream lines = Files.lines(path)) { 17 | 18 | lines.filter(line -> !line.startsWith("#")).flatMap(CSVFile::lineToPerson).forEach(System.out::println); 19 | 20 | } catch (IOException ioe) { 21 | ioe.printStackTrace(); 22 | } 23 | } 24 | 25 | private static Stream lineToPerson(String line) { 26 | try { 27 | String[] elements = line.split(";"); 28 | String name = elements[0]; 29 | int age = Integer.parseInt(elements[1]); 30 | String city = elements[2]; 31 | 32 | Person p = new Person(name, age, city); 33 | return Stream.of(p); 34 | } catch (Exception e) { 35 | } 36 | return Stream.empty(); 37 | } 38 | } -------------------------------------------------------------------------------- /Programs/JavaNIOAPI/Files/Data.csv: -------------------------------------------------------------------------------- 1 | # name;age;city 2 | Anushka;21;Hajipur 3 | Silky;21;Jaipur 4 | Farhin;22;Hajipur -------------------------------------------------------------------------------- /Programs/JavaNIOAPI/Files/SomeText.txt: -------------------------------------------------------------------------------- 1 | Hello World 2 | I = 10 3 | -------------------------------------------------------------------------------- /Programs/JavaNIOAPI/ReadingFiles.java: -------------------------------------------------------------------------------- 1 | import java.io.BufferedReader; 2 | import java.io.IOException; 3 | import java.nio.file.Files; 4 | import java.nio.file.Path; 5 | import java.nio.file.Paths; 6 | 7 | /** 8 | * 9 | * @author ASUS 10 | */ 11 | public class ReadingFiles { 12 | public static void main(String[] args) throws IOException{ 13 | Path path = Paths.get("Files/SomeText.txt"); 14 | boolean exists = Files.exists(path); 15 | System.out.println("Exists = " + exists); 16 | try(BufferedReader reader = Files.newBufferedReader(path);){ 17 | String line = reader.readLine(); 18 | while(line!=null){ 19 | System.out.println("Line = " + line); 20 | line = reader.readLine(); 21 | } 22 | } 23 | catch(IOException ioe){ 24 | ioe.printStackTrace(); 25 | } 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /Programs/JavaNIOAPI/WalkFileTreePattern.java: -------------------------------------------------------------------------------- 1 | import java.nio.file.Path; 2 | import java.nio.file.Paths; 3 | import java.nio.file.Files; 4 | import java.io.IOException; 5 | 6 | 7 | 8 | public class WalkFileTreePattern { 9 | public static void main(String[] args) throws IOException { 10 | Path path = Paths.get("files/"); 11 | 12 | boolean directory = Files.isDirectory(path); 13 | System.out.println("Directory = " + directory); 14 | 15 | var visitor = new FileVisitor(){ 16 | 17 | private long countFiles = 0L; 18 | private long countDirs = 0L; 19 | 20 | @Override 21 | public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException { 22 | countDirs++; 23 | return FileVisitResult.CONTINUE; 24 | } 25 | 26 | @Override 27 | public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { 28 | countFiles++; 29 | return FileVisitResult.CONTINUE; 30 | } 31 | 32 | 33 | @Override 34 | public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException { 35 | return null; 36 | } 37 | 38 | @Override 39 | public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException { 40 | return FileVisitResult.CONTINUE; 41 | } 42 | 43 | }; 44 | 45 | 46 | Files.walkFileTree(path, visitor); 47 | 48 | System.out.println("Number of files = " + visitor.countFiles); 49 | System.out.println("Number of directories = " + visitor.countDirs); 50 | 51 | } 52 | 53 | } 54 | -------------------------------------------------------------------------------- /Programs/JavaNIOAPI/WalkPattern.java: -------------------------------------------------------------------------------- 1 | import java.io.IOException; 2 | import java.nio.file.Files; 3 | import java.nio.file.Path; 4 | import java.nio.file.Paths; 5 | import java.util.stream.Stream; 6 | 7 | public class WalkPattern { 8 | public static void main(String[] args) throws IOException { 9 | Path path = Paths.get("files/"); 10 | 11 | boolean directory = Files.isDirectory(path); 12 | System.out.println("Is " + path + " a directory? " + directory); 13 | 14 | Stream stream = Files.walk(path); 15 | 16 | long count = stream.count(); 17 | 18 | long countDirs = Files.walk(path).filter(Files::isDirectory).count(); 19 | 20 | long countFiles = Files.walk(path).filter(Files::isRegularFile).count(); 21 | 22 | System.out.println("Count = " + count); 23 | System.out.println("Count files = " + visitor.countFiles); 24 | System.out.println("Count directories = " + visitor.countDirs); 25 | 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /Programs/JavaNIOAPI/WritingFiles.java: -------------------------------------------------------------------------------- 1 | import java.io.BufferedWriter; 2 | import java.io.IOException; 3 | import java.io.PrintWriter; 4 | import java.nio.file.Files; 5 | import java.nio.file.Path; 6 | import java.nio.file.Paths; 7 | 8 | public class WritingFiles { 9 | 10 | public static void main(String[] args) throws IOException { 11 | 12 | Path path = Paths.get("Files/SomeText.txt"); 13 | 14 | try (BufferedWriter writer1 = Files.newBufferedWriter(path); 15 | BufferedWriter writer2 = new BufferedWriter(writer1); 16 | PrintWriter pw = new PrintWriter(writer2);) { 17 | 18 | writer1.write("Hello World"); 19 | 20 | pw.printf("\n I = %d\n", 10); 21 | 22 | } catch (IOException ioe) { 23 | ioe.printStackTrace(); 24 | } 25 | System.out.println("Done"); 26 | } 27 | } -------------------------------------------------------------------------------- /Programs/JavaNIOAPI/WritingTextFiles.java: -------------------------------------------------------------------------------- 1 | import java.nio.file.Files; 2 | import java.nio.file.Path; 3 | import java.nio.file.Paths; 4 | import java.nio.file.StandardOpenOption; 5 | import java.io.BufferedWriter; 6 | import java.io.IOException; 7 | 8 | public class WritingTextFiles { 9 | public static void main(String[] args) throws IOException { 10 | Path path = Paths.get("Files/SomeText.txt"); 11 | try (BufferedWriter writer = Files.newBufferedWriter(path, StandardOpenOption.WRITE)) { 12 | writer.write("Hello Java Developers"); 13 | } catch (IOException ioe) { 14 | ioe.printStackTrace(); 15 | } 16 | System.out.println("File written successfully"); 17 | } 18 | } -------------------------------------------------------------------------------- /Programs/JavaNIOAPI/model/Person.java: -------------------------------------------------------------------------------- 1 | package model; 2 | 3 | public class Person { 4 | 5 | private String name; 6 | private int age; 7 | private String city; 8 | 9 | 10 | 11 | public Person(){ 12 | } 13 | 14 | public Person(String name, int age, String city) { 15 | this.name = name; 16 | this.age = age; 17 | this.city = city; 18 | } 19 | 20 | public String getName() { 21 | return name; 22 | } 23 | 24 | public void setName(String name) { 25 | this.name = name; 26 | } 27 | 28 | public int getAge() { 29 | return age; 30 | } 31 | 32 | public void setAge(int age) { 33 | this.age = age; 34 | } 35 | 36 | public String getCity() { 37 | return city; 38 | } 39 | 40 | public void setCity(String city) { 41 | this.city = city; 42 | } 43 | 44 | @Override 45 | public String toString() { 46 | return "Person[name=" + name + ", age=" + age + ", city=" + city + "]"; 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /Programs/JavaReflectionAPI/src/com/anushcodergirl/reflection/PlayWithReflection.java: -------------------------------------------------------------------------------- 1 | package com.anushcodergirl.reflection; 2 | 3 | public class PlayWithReflection{ 4 | public static void main(String[] args){ 5 | 6 | String personClassName = "com.anushcodergirl.reflection.model.Person"; 7 | Class personClass = Class.forName(personClass); 8 | 9 | System.out.println("Person class = " + personClass); 10 | 11 | Field[] fields = personClass.getFields(); 12 | System.out.println("--Fields--"); 13 | System.out.println(Array.toString(fields)); 14 | 15 | Field[] declaredFields = personClass.getDeclaredFields(); 16 | System.out.println("--Declared Fields--"); 17 | System.out.println(Array.toString(declaredFields)); 18 | 19 | System.out.println("--Methods--") 20 | Method[] methods = personClass.getMethods(); 21 | for(Method method : methods){ 22 | System.out.println(method); 23 | } 24 | 25 | System.out.println("--Declared Methods--") 26 | Method[] declaredMethods = personClass.getDeclaredMethods(); 27 | for(Method method : declaredMethods){ 28 | System.out.println(method); 29 | } 30 | 31 | System.out.println("--Static Declared Methods--") 32 | Arrays.stream(declaredMethods).filter(m -> Modifier.isStatic(m.getModifiers())).forEach(System.out::println); 33 | } 34 | } -------------------------------------------------------------------------------- /Programs/JavaReflectionAPI/src/com/anushcodergirl/reflection/model/Person.java: -------------------------------------------------------------------------------- 1 | package com.anushcodergirl.reflection.model; 2 | 3 | public class Person{ 4 | private String name; 5 | private int age; 6 | 7 | 8 | public Person(){ 9 | } 10 | 11 | public Person(String name, int age){ 12 | this.name = name; 13 | this.age = age; 14 | } 15 | 16 | public static Person of(String name, int age){ 17 | return new Person(name,age); 18 | } 19 | 20 | public String getName(){ 21 | return name; 22 | } 23 | 24 | public void setName(String name){ 25 | this.name = name; 26 | } 27 | 28 | public int getAge(){ 29 | return age; 30 | } 31 | 32 | public void setAge(int age){ 33 | this.age = age; 34 | } 35 | 36 | public String toString(){ 37 | return "Person [name=" + name + ", age=" + age + "]"; 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /Programs/Loops/Loops.java: -------------------------------------------------------------------------------- 1 | public class Loops 2 | { 3 | public static void main(String[] args) 4 | { 5 | //the while loop 6 | int x = 0; //loop control variable 7 | 8 | while (x < 5) //pre-test loop 9 | { 10 | System.out.println("Hello World!"); 11 | x++; 12 | } 13 | 14 | System.out.println(""); 15 | 16 | //the do-while loop 17 | int y = 0; //loop control variable 18 | do { 19 | System.out.println("Hello World!"); 20 | y++; 21 | } while(y<5); //post-test loop 22 | 23 | System.out.println(""); 24 | 25 | //the for loop 26 | for(int i = 0;i<5;i++) 27 | { 28 | System.out.println("Hello World!"); 29 | } 30 | } 31 | 32 | } 33 | -------------------------------------------------------------------------------- /Programs/Loops/WhileLoops/Main.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class Main{ 4 | public static void main(String[] args){ 5 | Scanner input = new Scanner(System.in); 6 | boolean isOnRepeat = true; 7 | while(isOnRepeat){ 8 | System.out.println("Playing current video"); 9 | System.out.println("Would you like to take this video off of repeat ? If so, say yes!"); 10 | String userInput = input.next(); 11 | if(userInput.equals("yes")){ 12 | isOnRepeat = false; 13 | } 14 | } 15 | System.out.println("Playing next video"); 16 | } 17 | 18 | } 19 | -------------------------------------------------------------------------------- /Programs/Methods/Methods.java: -------------------------------------------------------------------------------- 1 | public class Methods 2 | { 3 | public static void main(String[] args) 4 | { 5 | printAverage(20, 30); 6 | double average = getAverage(20,40); 7 | System.out.println("Average: " + average); 8 | String fullName = fullName("Anushka", "Raj"); 9 | System.out.println(fullName); 10 | int value = 20; 11 | if(isValid(value)) 12 | { 13 | System.out.println("The value is in range!"); 14 | } 15 | else 16 | { 17 | System.out.println("The value is not in range"); 18 | } 19 | } 20 | 21 | /**** 22 | * This method prints the average of two numbers 23 | * @param val1 The first value 24 | * @param val2 The second value 25 | */ 26 | public static void printAverage(int val1, int val2) 27 | { 28 | double average = (val1 + val2) / 2.0; 29 | System.out.println("Average: " + average); 30 | } 31 | 32 | /**** 33 | * This method calculates the average and return it 34 | * @param val1 The first value 35 | * @param val2 The second value 36 | * @return The average of two values 37 | */ 38 | public static double getAverage(int val1, int val2) 39 | { 40 | double average = (val1 + val2) / 2.0; 41 | return average; 42 | } 43 | 44 | public static String fullName(String name, String surname) 45 | { 46 | return name + " " + surname; 47 | } 48 | 49 | //range between 1 and 100 50 | public static boolean isValid(int number) 51 | { 52 | boolean status; 53 | 54 | return (number >= 1 && number <=100); 55 | 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /Programs/Methods/TestCustomException1.java: -------------------------------------------------------------------------------- 1 | class InvalidAgeException extends Exception 2 | { 3 | public InvalidAgeException (String str) 4 | { 5 | // calling the constructor of parent Exception 6 | super(str); 7 | } 8 | } 9 | 10 | // class that uses custom exception InvalidAgeException 11 | public class TestCustomException1 12 | { 13 | 14 | // method to check the age 15 | static void validate (int age) throws InvalidAgeException{ 16 | if(age < 18){ 17 | 18 | // throw an object of user defined exception 19 | throw new InvalidAgeException("age is not valid to vote"); 20 | } 21 | else { 22 | System.out.println("welcome to vote"); 23 | } 24 | } 25 | 26 | // main method 27 | public static void main(String args[]) 28 | { 29 | try 30 | { 31 | // calling the method 32 | validate(13); 33 | } 34 | catch (InvalidAgeException ex) 35 | { 36 | System.out.println("Caught the exception"); 37 | 38 | // printing the message from InvalidAgeException object 39 | System.out.println("Exception occured: " + ex); 40 | } 41 | 42 | System.out.println("rest of the code..."); 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /Programs/Miscellaneous/Age_Calculator.java: -------------------------------------------------------------------------------- 1 | // Program Written by minhaj-313 2 | import java.time.LocalDate; 3 | import java.time.Period; 4 | import java.util.Scanner; 5 | public class Age_Calculator 6 | { 7 | public static void main(String args[]) 8 | { 9 | System.out.print("Enter Your date of birth in YYYY-MM-DD format: "); 10 | Scanner scanner = new Scanner(System.in); 11 | String input = scanner.nextLine(); 12 | scanner.close(); 13 | LocalDate dob = LocalDate.parse(input); 14 | System.out.println("You are " + calculateAge(dob)+" years old."); 15 | } 16 | public static int calculate_Age(LocalDate dob) 17 | { 18 | LocalDate curDate = LocalDate.now(); 19 | if ((dob != null) && (curDate != null)) 20 | { 21 | return Period.between(dob, curDate).getYears(); 22 | } 23 | else 24 | { 25 | return 0; 26 | } 27 | } 28 | } -------------------------------------------------------------------------------- /Programs/Miscellaneous/date.java: -------------------------------------------------------------------------------- 1 | import java.time.LocalDate; 2 | import java.time.Month; 3 | 4 | public class Main { 5 | public static void main(String args[]) { 6 | 7 | // declare variables for birthday 8 | int birthDate = 23; 9 | Month birthMonth = Month.SEPTEMBER; 10 | 11 | // get current date 12 | LocalDate currentDate = LocalDate.now(); 13 | System.out.println("Todays Date: " + currentDate); 14 | 15 | // get current date and month 16 | int date = currentDate.getDayOfMonth(); 17 | Month month = currentDate.getMonth(); 18 | 19 | if(date == birthDate && month == birthMonth) { 20 | System.out.println("HAPPY BIRTHDAY TO YOU !!"); 21 | } 22 | else { 23 | System.out.println("Today is not my birthday."); 24 | } 25 | } 26 | } -------------------------------------------------------------------------------- /Programs/Miscellaneous/fibonaccisearch.java: -------------------------------------------------------------------------------- 1 | package com.thealgorithms.searches; 2 | 3 | import com.thealgorithms.devutils.searches.SearchAlgorithm; 4 | 5 | /* 6 | * Fibonacci Search algorithm finds the position of a target value in 7 | * a sorted array 8 | */ 9 | public class FibonacciSearch implements SearchAlgorithm { 10 | 11 | @Override 12 | public > int find(T[] array, T key) { 13 | int fibMinus1 = 1; 14 | int fibMinus2 = 0; 15 | int fibNumber = fibMinus1 + fibMinus2; 16 | int n = array.length; 17 | 18 | while (fibNumber < n) { 19 | fibMinus2 = fibMinus1; 20 | fibMinus1 = fibNumber; 21 | fibNumber = fibMinus2 + fibMinus1; 22 | } 23 | 24 | int offset = -1; 25 | 26 | while (fibNumber > 1) { 27 | int i = Math.min(offset + fibMinus2, n - 1); 28 | 29 | if (array[i].compareTo(key) < 0) { 30 | fibNumber = fibMinus1; 31 | fibMinus1 = fibMinus2; 32 | fibMinus2 = fibNumber - fibMinus1; 33 | offset = i; 34 | } else if (array[i].compareTo(key) > 0) { 35 | fibNumber = fibMinus2; 36 | fibMinus1 = fibMinus1 - fibMinus2; 37 | fibMinus2 = fibNumber - fibMinus1; 38 | } else { 39 | return i; 40 | } 41 | } 42 | 43 | if (fibMinus1 == 1 && array[offset + 1] == key) { 44 | return offset + 1; 45 | } 46 | 47 | return -1; 48 | } 49 | 50 | // Driver Program 51 | public static void main(String[] args) { 52 | Integer[] integers = { 1, 2, 4, 8, 16, 32, 64, 128, 256, 512 }; 53 | 54 | int size = integers.length; 55 | Integer shouldBeFound = 128; 56 | FibonacciSearch fsearch = new FibonacciSearch(); 57 | int atIndex = fsearch.find(integers, shouldBeFound); 58 | 59 | System.out.println( 60 | "Should be found: " + 61 | shouldBeFound + 62 | ". Found " + 63 | integers[atIndex] + 64 | " at index " + 65 | atIndex + 66 | ". An array length " + 67 | size 68 | ); 69 | } 70 | } -------------------------------------------------------------------------------- /Programs/Miscellaneous/taximeter.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | 3 | class TaxiMeter{ 4 | int taxiNo; 5 | String name; 6 | int km; 7 | double bill; 8 | public TaxiMeter(){ 9 | taxiNo = 0; 10 | name = ""; 11 | km = 0; 12 | bill = 0.0; 13 | } 14 | public void input()throws IOException{ 15 | BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 16 | System.out.print("Taxi Number: "); 17 | taxiNo = Integer.parseInt(br.readLine()); 18 | System.out.print("Name: "); 19 | name = br.readLine(); 20 | System.out.print("Number of km: "); 21 | km = Integer.parseInt(br.readLine()); 22 | } 23 | public void calculate(){ 24 | if(km <= 1) 25 | bill = 25 * km; 26 | else if(km > 1 && km <= 6) 27 | bill = 10 * km; 28 | else if(km > 6 && km <= 12) 29 | bill = 15 * km; 30 | else if(km > 12 && km <= 18) 31 | bill = 20 * km; 32 | else 33 | bill = 25 * km; 34 | } 35 | public void display(){ 36 | System.out.println("TaxiNo.\tName\tKilometers Traveled\tBill Amount"); 37 | System.out.println(taxiNo + "\t" + name + "\t" + km + "\t" + bill); 38 | } 39 | public static void main(String args[])throws IOException{ 40 | TaxiMeter obj = new TaxiMeter(); 41 | obj.input(); 42 | obj.calculate(); 43 | obj.display(); 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /Programs/Miscellaneous/test.java: -------------------------------------------------------------------------------- 1 | package com.company; 2 | 3 | 4 | import java.io.*; 5 | import java.util.*; 6 | class test 7 | { 8 | public static void main(String []args) 9 | { 10 | Scanner q = new Scanner(System.in); 11 | int p=q.nextInt(); 12 | int a=1,b=1; 13 | System.out.print(a+" "+b+" "); 14 | for(int i=2;i 0; i--) 23 | { 24 | for(j = 1; j <= rows-i; j++) 25 | { 26 | System.out.print(" "); 27 | } 28 | for(j = 1; j <= (2*i-1); j++) 29 | { 30 | System.out.print("*"); 31 | } 32 | System.out.println(); 33 | } 34 | } 35 | 36 | public static void halfDiamond(int rows) 37 | { // * 38 | int i, j, k = 0; // ** 39 | for(i = 0; i < rows; i++) // *** 40 | { // **** 41 | if(i<=(rows/2)) // *** 42 | { // ** 43 | for(j = 0; j <= i; j++) // * 44 | { 45 | System.out.print("*"); 46 | } 47 | } 48 | else 49 | { 50 | for(j = i; j < rows; j++) 51 | { 52 | System.out.print("*"); 53 | } 54 | } 55 | System.out.println(); 56 | } 57 | 58 | } 59 | public static void inverted_halfDiamond(int rows) // * 60 | { // ** 61 | int i,j,k; // *** 62 | for ( i = 0; i < rows; i++) // **** 63 | { // *** 64 | if(i <= (rows/2)) // ** 65 | { // * 66 | for(k = (rows/2); k > i; k--) 67 | { 68 | System.out.print(" "); 69 | } 70 | for(j = 0; j <= i; j++) 71 | { 72 | System.out.print("*"); 73 | } 74 | } 75 | else 76 | { 77 | for(k = rows/2; k < i; k++) 78 | { 79 | System.out.print(" "); 80 | } 81 | for(j = i; j < rows; j++) 82 | { 83 | System.out.print("*"); 84 | } 85 | } 86 | System.out.println(); 87 | } 88 | } 89 | 90 | public static void printPattern(int rows) 91 | { 92 | System.out.println("Diamond: "); 93 | diamond(rows); 94 | System.out.println(); 95 | System.out.println("Half Diamond: "); 96 | halfDiamond(rows); 97 | System.out.println(); 98 | System.out.println("inverted Half Diamond for odd rows: "); 99 | inverted_halfDiamond(rows); 100 | } 101 | 102 | public static void main(String args[]) 103 | { 104 | Scanner sc = new Scanner(System.in); 105 | System.out.println("Enter the number of row: "); 106 | int rows = sc.nextInt(); 107 | printPattern(rows); 108 | } 109 | } -------------------------------------------------------------------------------- /Programs/Pattern Programs/KShapeUsingAlgorithms/KShapeUsingAlphabets.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class KShapeUsingAlphabets{ 4 | 5 | public static void main(String[] args){ 6 | 7 | for (int i = 5; i >= 0; i--) 8 | { 9 | int alphabet = 65; 10 | for (int j = 0; j <= i; j++) 11 | { 12 | System.out.print((char) (alphabet + j) + " "); 13 | } 14 | System.out.println(); 15 | } 16 | for (int i = 0; i<= 5; i++) 17 | { 18 | int alphabet = 65; 19 | for (int j = 0; j <= i; j++) 20 | { 21 | System.out.print((char) (alphabet + j) + " "); 22 | } 23 | System.out.println(); 24 | } 25 | } 26 | } -------------------------------------------------------------------------------- /Programs/Pattern Programs/PatternOne.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class PatternOne { 4 | public static void main(String[] args) { 5 | 6 | Scanner sc = new Scanner(System.in); 7 | System.out.println("Enter the row number: "); 8 | int n = sc.nextInt(); 9 | for (int i = n; i >= 0; i--) { 10 | for (int j = i; j >= 0; j--) { 11 | if (i == n) { 12 | System.out.print("* "); 13 | } else { 14 | if (j == i || j == 0) { 15 | System.out.print("* "); 16 | } else { 17 | System.out.print(" "); 18 | } 19 | } 20 | } 21 | System.out.println(""); 22 | } 23 | 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /Programs/Pattern Programs/ReversedPyramidStarPattern.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | public class ReversedPyramidStarPattern 3 | { 4 | public static void main(String[] args) 5 | { 6 | Scanner sc = new Scanner(System.in); 7 | System.out.println("Enter the number of rows: "); 8 | 9 | int rows = sc.nextInt(); 10 | for (int i= 0; i<= rows-1 ; i++) 11 | { 12 | for (int j=0; j<=i; j++) 13 | { 14 | System.out.print(" "); 15 | } 16 | for (int k=0; k<=rows-1-i; k++) 17 | { 18 | System.out.print("*" + " "); 19 | } 20 | System.out.println(); 21 | } 22 | sc.close(); 23 | 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /Programs/Pattern Programs/Triangle_num_pattern1.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | public class Triangle_num_pattern1 3 | { 4 | public static void main(String args[]) 5 | { 6 | int i,j; 7 | Scanner sc = new Scanner(System.in); 8 | System.out.println("Enter the row number: "); 9 | int n = sc.nextInt(); 10 | for(i = 1; i <= n; i++) 11 | { 12 | for(j = 1; j <=i; j++) 13 | { 14 | System.out.print(j+" "); 15 | } 16 | System.out.println(); 17 | } 18 | } 19 | } -------------------------------------------------------------------------------- /Programs/Pattern Programs/diamondpattern.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | public class Prog4 3 | { 4 | public static void main(String[] args) 5 | { 6 | int i,j,r; 7 | System.out.print("Input number of rows (half of the diamond) : "); 8 | Scanner in = new Scanner(System.in); 9 | r = in.nextInt(); 10 | for(i=0;i<=r;i++) 11 | { 12 | for(j=1;j<=r-i;j++) 13 | System.out.print(" "); 14 | for(j=1;j<=2*i-1;j++) 15 | System.out.print("*"); 16 | System.out.print("\n"); 17 | } 18 | for(i=r-1;i>=1;i--) 19 | { 20 | for(j=1;j<=r-i;j++) 21 | System.out.print(" "); 22 | for(j=1;j<=2*i-1;j++) 23 | System.out.print("*"); 24 | System.out.print("\n"); 25 | } 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /Programs/Pattern Programs/n_pattern.java: -------------------------------------------------------------------------------- 1 | public class n_pattern { 2 | 3 | static void Alphabet_N_Pattern(int N) 4 | { 5 | 6 | int index, side_index, size; 7 | 8 | int Right = 1, Left = 1, Diagonal = 2; 9 | 10 | 11 | for (index = 0; index < N; index++) { 12 | 13 | 14 | System.out.print(Left++); 15 | 16 | 17 | for (side_index = 0; side_index < 2 * (index); side_index++) 18 | System.out.print(" "); 19 | 20 | 21 | if (index != 0 && index != N - 1) 22 | System.out.print(Diagonal++); 23 | else 24 | System.out.print(" "); 25 | 26 | 27 | for (side_index = 0; side_index < 2 * (N - index - 1); side_index++) 28 | System.out.print(" "); 29 | 30 | 31 | System.out.print(Right++); 32 | 33 | System.out.println(); 34 | } 35 | } 36 | 37 | 38 | public static void main(String args[]) 39 | { 40 | 41 | int Size = 6; 42 | 43 | 44 | Alphabet_N_Pattern(Size); 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /Programs/Pattern Programs/pyramid patterns/PascalTrianglePattern.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class PascalTrianglePattern { 4 | static int factorial(int n) { 5 | int f; 6 | 7 | for(f = 1; n > 1; n--){ 8 | f *= n; 9 | } 10 | return f; 11 | } 12 | static int func(int n,int r) { 13 | return factorial(n) / ( factorial(n-r) * factorial(r) ); 14 | } 15 | public static void main(String args[]){ 16 | System.out.println(); 17 | int n, i, j; 18 | Scanner scn = new Scanner(System.in); 19 | 20 | //Expected Input row count 21 | if (scn.hasNext()){ 22 | n = scn.nextInt(); 23 | } else { 24 | n = 5; 25 | } 26 | n--; 27 | for(i = 0; i <= n; i++) { 28 | for(j = 0; j <= n-i; j++){ 29 | System.out.print(" "); 30 | } 31 | for(j = 0; j <= i; j++){ 32 | System.out.print(" "+ func(i, j)); 33 | } 34 | System.out.println(); 35 | } 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /Programs/Pattern Programs/pyramid patterns/Pattern_1.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | public class Pattern_1 3 | { 4 | public static void main(String args[]) 5 | { 6 | int i, j; // loop control variables 7 | 8 | Scanner sc = new Scanner(System.in); 9 | System.out.println("Enter a number: "); 10 | 11 | int n = sc.nextInt(); 12 | 13 | // this loop works on the row 14 | for(i = 0; i < n; i++) 15 | { 16 | // this loop works on the column 17 | for(j = 0; j < i ; j++) 18 | { 19 | System.out.print("* "); 20 | } 21 | System.out.println(); 22 | } 23 | } 24 | } -------------------------------------------------------------------------------- /Programs/Pattern Programs/pyramid patterns/Pattern_2.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | public class Pattern_2 3 | { 4 | public static void main(String args[]) 5 | { 6 | int i, j, k = 1; 7 | Scanner sc = new Scanner(System.in); 8 | // input taking from the console 9 | System.out.println("Enter a number: "); 10 | int n = sc.nextInt(); 11 | 12 | // this loop works on the row 13 | for(i = 0; i < n; i++) 14 | { 15 | // this loop works on the column 16 | // here, we want to print * two more than previous row 17 | // so this loop works on the 'k' variable 18 | for(j = 0; j < k; j++) 19 | { 20 | System.out.print("* "); 21 | } 22 | 23 | // increment 'k' with 2 24 | k = k + 2; 25 | System.out.println(); 26 | } 27 | } 28 | } -------------------------------------------------------------------------------- /Programs/Pattern Programs/pyramid patterns/Pattern_3.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | public class Pattern_3 3 | { 4 | public static void main(String args[]) 5 | { 6 | int i, j, k = 8; 7 | 8 | // this loop works on the row 9 | for(i = 0; i < 5; i++) 10 | { 11 | // this loop works for spaces 12 | for(j = 0; j < k; j++) 13 | { 14 | System.out.print(" "); 15 | } 16 | 17 | // decrement k to reduce the spaces in next row 18 | k = k - 2; 19 | 20 | // this loop works to print the '*' 21 | for(j = 0; j <= i; j++) 22 | { 23 | System.out.print("* "); 24 | } 25 | System.out.println(); 26 | } 27 | 28 | } 29 | } -------------------------------------------------------------------------------- /Programs/Pattern Programs/pyramid patterns/Pattern_4.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | public class Pattern_4 3 | { 4 | public static void main(String args[]) 5 | { 6 | int i, j, k = 16, s = 1; 7 | 8 | //this loop work on the row 9 | for(i = 0; i < 5; i++) 10 | { 11 | //this loop work on space 12 | for(j = 0; j < k; j++) 13 | { 14 | System.out.print(" "); 15 | } 16 | //decrement k for reduce the spaces in the next row 17 | k = k - 4; 18 | 19 | //this loop work on the column 20 | //here we want to print * two more than the previous row 21 | //so this loop work on the k 22 | for(j = 1; j <= s; j++) 23 | { 24 | System.out.print("* "); 25 | } 26 | s = s + 2; 27 | System.out.println(); 28 | } 29 | 30 | } 31 | } -------------------------------------------------------------------------------- /Programs/Pattern Programs/pyramid patterns/Pattern_5.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | public class Pattern_5 3 | { 4 | public static void main(String args[]) 5 | { 6 | int i, j, k = 0; 7 | Scanner sc = new Scanner(System.in); 8 | System.out.println("Enter a number: "); 9 | int n = sc.nextInt(); 10 | 11 | //this loop work on the row 12 | for(i = 1; i <= n; i++) 13 | { 14 | //this loop work for both the space and print * 15 | for(j = 1; j <= n-i; j++) 16 | { 17 | System.out.print(" "); 18 | } 19 | 20 | //until value of the k is not equal to 2*i-i, it print star 21 | while(k != (2*i-1)) 22 | { 23 | System.out.print("*"); 24 | k++; 25 | } 26 | k = 0; 27 | System.out.println(); 28 | } 29 | 30 | } 31 | } -------------------------------------------------------------------------------- /Programs/ReadingInputUsingJOptionPane/ReadJOptionPane.java: -------------------------------------------------------------------------------- 1 | import javax.swing.*; 2 | 3 | public class ReadJOptionPane { 4 | public static void main(String[] args){ 5 | 6 | String name; 7 | String surname; 8 | 9 | name = JOptionPane.showInputDialog("Please enter your name"); 10 | 11 | surname = JOptionPane.showInputDialog("Please enter your surname"); 12 | 13 | JOptionPane.showMessageDialog(null, name + " " + surname); 14 | 15 | int firstNumber; 16 | int secondNumber; 17 | String input; 18 | input = JOptionPane.showInputDialog("Please enter the first number: "); 19 | firstNumber = Integer.parseInt(input); 20 | 21 | input = JOptionPane.showInputDialog("Please enter the second number: "); 22 | secondNumber = Integer.parseInt(input); 23 | 24 | double average = (firstNumber + secondNumber) / 2.0; 25 | 26 | JOptionPane.showMessageDialog(null, "Average" + average); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /Programs/ReadingInputUsingScanner/ReadingInput.java: -------------------------------------------------------------------------------- 1 | import java.net.SocketOption; 2 | import java.util.Scanner; 3 | 4 | public class ReadingInput { 5 | 6 | public static void main(String[] args){ 7 | 8 | int firstNumber; 9 | int secondNumber; 10 | 11 | Scanner sc = new Scanner(System.in); 12 | 13 | System.out.println("Please enter the first number:"); 14 | firstNumber = sc.nextInt(); 15 | 16 | System.out.println("Please enter the second number"); 17 | secondNumber = sc.nextInt(); 18 | 19 | double average = (firstNumber + secondNumber) / 2; 20 | 21 | System.out.println("The average is:" + average); 22 | 23 | String name; 24 | String surname; 25 | 26 | sc.nextLine(); 27 | System.out.println("Please enter your name"); 28 | name = sc.nextLine(); 29 | 30 | System.out.println("Please enter your surname"); 31 | surname = sc.nextLine(); 32 | 33 | System.out.println(); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /Programs/Stack Implementation/BalanceParaenthesis.java: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Anushka-codergirl/Java/74136a5c868ba03e10c6eab28281fc7c0de51431/Programs/Stack Implementation/BalanceParaenthesis.java -------------------------------------------------------------------------------- /Programs/Stack Implementation/Implementation.java: -------------------------------------------------------------------------------- 1 | /* > Stack is a Linear Data Structure. 2 | > It follows the principle of Last in First out (LIFO) i.e the element entered last 3 | in the stack will be the first one to come out. 4 | > To implement the stack in Java, we have a Stack class in Java Collections Framework. 5 | > Methods in Stack class: 6 | 1) push() --- To add a element on top of the stack 7 | 2) pop() --- To remove the top element from the stack 8 | 3) peek() --- Returns the top most element of the stack 9 | 4) empty() --- Return boolean whether the stack is empty or not 10 | TC of above three method is O(1) 11 | 12 | > Below is the implementation of Stack Data Structure 13 | */ 14 | 15 | 16 | 17 | 18 | 19 | import java.util.*; 20 | public class Implementation { 21 | 22 | public static void main(String[] args) { 23 | 24 | // Creating a new Stack 25 | Stack st = new Stack(); 26 | st.push(1); // Pushing elements at the top 27 | st.push(2); 28 | st.push(3); 29 | st.push(4); 30 | st.push(5); 31 | 32 | System.out.println("Is stack empty: " + st.empty()); // Check if the stack is empty or not 33 | 34 | while(!st.empty()){ 35 | System.out.println(st.peek() + " removed from stack"); // Prints the top element 36 | st.pop(); // removes the top element 37 | } 38 | System.out.println("Is stack empty: " + st.empty()); 39 | 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /Programs/Stack Implementation/reverseString.java: -------------------------------------------------------------------------------- 1 | 2 | /* This program will show you how to reverse a string using stack data structure 3 | 4 | Input: helloworld 5 | Output: dlrowolleh 6 | 7 | ** Link to question: https://practice.geeksforgeeks.org/problems/reverse-a-string-using-stack/1?page=1&difficulty[]=-2&difficulty[]=-1&difficulty[]=0&category[]=Stack&sortBy=submissions 8 | 9 | */ 10 | 11 | 12 | import java.util.Stack; 13 | 14 | 15 | public class reverseString { 16 | 17 | private static String reverseTheString(String input) { 18 | 19 | Stack st = new Stack(); //creating a stack 20 | int size = input.length(); 21 | for(int count = 0; count < size; count++){ 22 | 23 | char toPush = input.charAt(count); // Pushing each character of string into the stack 24 | st.push(toPush); 25 | } 26 | 27 | // Now when we pop out the elements of stack, the last element to be added will come out first 28 | String reversedString = ""; 29 | while(!st.empty()){ 30 | char toAdd = st.peek(); // storing the top element 31 | reversedString = reversedString + toAdd; 32 | st.pop(); // Pop out the top element 33 | } 34 | 35 | return reversedString; // return the reversed string 36 | } 37 | 38 | public static void main(String[] args) { 39 | 40 | String input = "Aayan"; 41 | String output = reverseTheString(input); // reverse operation is carried out in diffrent function 42 | System.out.println("Output: " + output); 43 | 44 | } 45 | 46 | } 47 | -------------------------------------------------------------------------------- /Programs/StringConcatenation/Example1.java: -------------------------------------------------------------------------------- 1 | public class Example1{ 2 | public static void main(String[] args){ 3 | String firstName = "Anushka"; 4 | String lastName = "Raj"; 5 | System.out.println("Hi There! I am " + firstName + " "+ lastName + "."); 6 | } 7 | } -------------------------------------------------------------------------------- /Programs/Strings/Pangram.java: -------------------------------------------------------------------------------- 1 | /// Wikipedia: https://en.wikipedia.org/wiki/Pangram 2 | public class Pangram { 3 | 4 | public static void main(String[] args) { 5 | System.out.println("Is Pangram: "+isPangram("Pangram is a sentence using every letter of alphabet at least once.")); 6 | System.out.println("Is Pangram: "+isPangram("qwertyuiopasdfghjklxcvbnzm.")); 7 | System.out.println("Is Pangram: "+isPangram("QWERTYUIOPADFGHJKLXCVBNZM!")); 8 | System.out.println("Is Pangram: "+isPangram("The quick brown fox jumps over the lazy dog")); 9 | } 10 | 11 | // Check if a string is Pangram 12 | public static boolean isPangram(String s) { 13 | s = s.replaceAll("[^a-zA-Z]", ""); 14 | char ch ='a'; 15 | char CH='A'; 16 | for(int i=0; i<26; i++, ch++, CH++) { 17 | if(!s.contains(String.valueOf(ch)) && !s.contains(String.valueOf(CH))) { 18 | return false; 19 | } 20 | } 21 | return true; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /Programs/TextFiles/OutputFile.txt: -------------------------------------------------------------------------------- 1 | 5 2 | 10 3 | 15 4 | 20 5 | 25 6 | 30 7 | 35 8 | 40 9 | -------------------------------------------------------------------------------- /Programs/TextFiles/ReadingFromFiles.java: -------------------------------------------------------------------------------- 1 | import javax.swing.*; 2 | import java.io.File; 3 | import java.io.IOException; 4 | import java.util.Scanner; 5 | 6 | public class ReadingFromFiles 7 | { 8 | public static void main(String[] args) throws IOException 9 | { 10 | int sum = 0; 11 | int count = 0; 12 | double average; 13 | File file = new File("OutputFile.txt"); 14 | 15 | if (file.exists()) 16 | { 17 | Scanner inputFile = new Scanner(file); 18 | 19 | while (inputFile.hasNext()) 20 | { 21 | sum = sum + inputFile.nextInt(); 22 | count++; 23 | } 24 | 25 | inputFile.close(); 26 | average = sum / (double)count; 27 | 28 | JOptionPane.showMessageDialog(null,"Sum of all values: " + sum + "\n" + "The number of values: " + count + "\n" + "The average is: " + average); 29 | } 30 | else 31 | { 32 | JOptionPane.showMessageDialog(null,"The file does" + "not exists!"); 33 | } 34 | } 35 | } 36 | 37 | -------------------------------------------------------------------------------- /Programs/TextFiles/WritingToFiles.java: -------------------------------------------------------------------------------- 1 | import java.io.IOException; 2 | import java.io.PrintWriter; 3 | 4 | public class WritingToFiles 5 | { 6 | public static void main(String[] args) throws IOException 7 | { 8 | //create the file and open the file 9 | //if the file exists it will replace the file 10 | PrintWriter outputFile = new PrintWriter("OutputFile.txt"); 11 | 12 | outputFile.println("This is line 1"); 13 | outputFile.println("This is line 2"); 14 | outputFile.println("This is line 3"); 15 | outputFile.println("This is line 4"); 16 | outputFile.println("This is line 5"); 17 | 18 | 19 | outputFile.close(); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /Programs/TheSwitchStatement/Java15NewSwitchStatement.java: -------------------------------------------------------------------------------- 1 | import javax.swing.*; 2 | 3 | /** 4 | * @author Alessandro Arosio - 03/10/2020 17:02 5 | */ 6 | public class Java15NewSwitchStatement { 7 | public static void main(String[] args) { 8 | int number; 9 | String input = JOptionPane.showInputDialog("Please enter 1,2 or 3: "); 10 | try { 11 | number = Integer.parseInt(input); 12 | switch (number) { 13 | case 1 -> JOptionPane.showMessageDialog(null,"You entered 1!"); 14 | case 2 -> JOptionPane.showMessageDialog(null,"You entered 2!"); 15 | case 3 -> JOptionPane.showMessageDialog(null,"You entered 3!"); 16 | case 4 -> JOptionPane.showMessageDialog(null,"You entered 4!"); 17 | default -> JOptionPane.showMessageDialog(null,"You did not enter 1,2,3 or a 4!"); 18 | } 19 | } catch (NumberFormatException e) { 20 | JOptionPane.showMessageDialog(null,"Don't cheat... it has to be a number!"); 21 | } 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /Programs/TheSwitchStatement/TheSwitchStatement.java: -------------------------------------------------------------------------------- 1 | import javax.swing.*; 2 | public class TheSwitchStatement 3 | { 4 | public static void main(String[] args) 5 | { 6 | int number; 7 | String input = JOptionPane.showInputDialog("Please enter 1,2 or 3: "); 8 | number = Integer.parseInt(input); 9 | 10 | switch (number) 11 | { 12 | case 1: 13 | JOptionPane.showMessageDialog(null,"You entered 1!"); 14 | break; 15 | 16 | case 2: 17 | JOptionPane.showMessageDialog(null,"You entered 2!"); 18 | break; 19 | 20 | case 3: 21 | JOptionPane.showMessageDialog(null,"You entered 3!"); 22 | break; 23 | 24 | default: 25 | JOptionPane.showMessageDialog(null,"You did not enter 1,2 or a 3!"); 26 | 27 | } 28 | input = JOptionPane.showInputDialog("Please enter a,b or c: "); 29 | char letter; 30 | letter = input.charAt(0); 31 | 32 | switch (letter) 33 | { 34 | case 'A': 35 | case 'a': 36 | JOptionPane.showMessageDialog(null,"You entered a!"); 37 | break; 38 | 39 | case 'B': 40 | case 'b': 41 | JOptionPane.showMessageDialog(null,"You entered b!"); 42 | break; 43 | 44 | case 'C': 45 | case 'c': 46 | JOptionPane.showMessageDialog(null,"You entered c!"); 47 | break; 48 | 49 | default: 50 | JOptionPane.showMessageDialog(null,"You did not enter a correct value!"); 51 | } 52 | System.exit(0); 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /Programs/TypeConversion/TypeConversion.java: -------------------------------------------------------------------------------- 1 | public class TypeConversion { 2 | public static void main(String args[]){ 3 | 4 | byte a = 127; 5 | int b = a; 6 | int c = 223; 7 | byte d = (byte) c; 8 | 9 | float f = 5.6f; 10 | int i = (int) f; 11 | 12 | byte e = 10; 13 | byte t = 20; 14 | 15 | int result = e*t; 16 | 17 | System.out.println(a); 18 | System.out.println(d); 19 | System.out.println(i); 20 | System.out.println(result); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /Programs/UsefulLoopCalculations/InputValidation.java: -------------------------------------------------------------------------------- 1 | import javax.swing.*; 2 | public class InputValidation { 3 | public static void main(String[] args) { 4 | String input = JOptionPane.showInputDialog("Please enter a number in the range of 1 through 100"); 5 | 6 | int number = Integer.parseInt(input); 7 | 8 | while (number < 1 || number > 100) { 9 | JOptionPane.showMessageDialog(null, "That number is not valid!"); 10 | input = JOptionPane.showInputDialog("Please enter a number in the range of 1 through 100!"); 11 | number = Integer.parseInt(input); 12 | } 13 | JOptionPane.showMessageDialog(null,"Correct!" ); 14 | } 15 | 16 | } -------------------------------------------------------------------------------- /Programs/UsefulLoopCalculations/RunningTotals.java: -------------------------------------------------------------------------------- 1 | import javax.print.attribute.standard.JobPriority; 2 | import javax.swing.*; 3 | import java.text.DecimalFormat; 4 | 5 | public class RunningTotals 6 | { 7 | public static void main(String[] args) 8 | { 9 | int days; 10 | double sales; 11 | double totalSales = 0.0; //running total 12 | 13 | DecimalFormat dollar = new DecimalFormat("#,##0.000"); 14 | 15 | String input = JOptionPane.showInputDialog("For how many days do you have sales" + "figures?" ); 16 | 17 | days = Integer.parseInt(input); 18 | 19 | for(int count = 1; count <= days; count++) 20 | { 21 | input = JOptionPane.showInputDialog("Enter the sales for day " + count); 22 | 23 | sales = Double.parseDouble(input); 24 | 25 | totalSales += sales; 26 | } 27 | 28 | JOptionPane.showMessageDialog(null,"The total sales are : $" +dollar.format(totalSales)); 29 | 30 | System.exit(0); 31 | } 32 | 33 | 34 | } 35 | -------------------------------------------------------------------------------- /Programs/UsefulLoopCalculations/SentinelValues.java: -------------------------------------------------------------------------------- 1 | import javax.swing.*; 2 | 3 | public class SentinelValues 4 | { 5 | public static void main(String[] args) 6 | { 7 | int value; 8 | int doubleVlaue; 9 | 10 | String input = JOptionPane.showInputDialog("Please enter a value to double " + "(0 to stop)"); 11 | 12 | value = Integer.parseInt(input); 13 | 14 | while (value != 0) 15 | { 16 | doubleVlaue = value * 2; 17 | JOptionPane.showMessageDialog(null,"" + value + " doubled is " + doubleVlaue); 18 | 19 | input = JOptionPane.showInputDialog("Please enter a value to double " + "(0 to stop)"); 20 | 21 | value = Integer.parseInt(input); 22 | 23 | } 24 | System.exit(0); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /Programs/UsefulLoopCalculations/UserControlledLoop.java: -------------------------------------------------------------------------------- 1 | import javax.swing.*; 2 | 3 | public class UserControlledLoop 4 | { 5 | public static void main(String[] args) 6 | { 7 | int maxValue; 8 | 9 | String input = JOptionPane.showInputDialog("How high should I go to square the number?"); 10 | maxValue = Integer.parseInt(input); 11 | 12 | System.out.println("Number Number squares"); 13 | System.out.println("------------------------------"); 14 | 15 | for(int number = 1 ; number <=maxValue; number++) 16 | { 17 | System.out.println(number + "\t\t\t" + number*number); 18 | } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /Programs/Variables, Literals and Constants/Variables_Literals_Constants.java: -------------------------------------------------------------------------------- 1 | public class Variables_Literals_Constants { 2 | public static void main(String[] args){ 3 | int firstNumber; 4 | int secondNumber; 5 | 6 | firstNumber = 11; 7 | secondNumber = 20; 8 | 9 | double average; 10 | 11 | average = (firstNumber + secondNumber) / 2.0; 12 | 13 | System.out.println("Average: " + average); 14 | 15 | String name; 16 | String surname; 17 | 18 | name = "Anushka"; 19 | surname = "Raj"; 20 | 21 | System.out.println(name + " " + surname); 22 | 23 | boolean value; 24 | value = true; 25 | 26 | System.out.println(value); 27 | 28 | char letter; 29 | letter = 'A'; 30 | 31 | System.out.println(letter); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Java 2 | 3 | **Java** is an object-oriented programming language that produces software for multiple platforms. When a programmer writes a Java application, the compiled code (known as bytecode) runs on most operating systems (OS), including Windows, Linux and Mac OS. Java derives much of its syntax from the C and C++ programming languages. 4 | 5 | * [Programs](/Programs) 6 | * [Resources](/RESOURCES.md) 7 | 8 | 9 | ## [Interesting Facts about Java](/Facts.md) 10 | 11 | ## Java Editions 12 | We have four editions of Java, each used for building a different type of 13 | application: 14 | - **Java Standard Edition (SE)** - the core Java platform. It contains all of the 15 | libraries that every Java developer must learn. 16 | - **Java Enterprise Edition (EE)** - used for building very large scale, 17 | distributed systems. It’s built on top of Java SE and provides additional 18 | libraries for building fault-tolerant, distributed, multi-tier software. 19 | - **Java Micro Edition (ME)** - a subset of Java SE, designed for mobile 20 | devices. It also has libraries specific to mobile devices. 21 | - **Java Card** - used in smart cards. 22 | 23 | ## How Java Code Gets Executed 24 | 25 | The Java compiler takes Java code and compiles it down to Java Bytecode which is 26 | a cross-platform format. When we run Java applications, Java Virtual Machine 27 | (JVM) gets loaded in the memory. It takes our bytecode as the input and translates 28 | it to the native code for the underlying operating system. There are various 29 | implementations of Java Virtual Machine for almost all operating systems. 30 | 31 | ## Architecture of Java Applications 32 | 33 | The smallest building blocks in Java programs are methods (also called functions 34 | in other programming languages). We combine related methods in classes, and 35 | related classes in packages. This modularity in Java allows us to break down large 36 | programs into smaller building blocks that are easier to understand and re-use. 37 | -------------------------------------------------------------------------------- /RESOURCES.md: -------------------------------------------------------------------------------- 1 | # JAVA Resources Compilation 2 | 3 | ## 💻 **Web resources** 4 | * [Pluralsight (paid or free)](https://www.pluralsight.com/) 5 | * [Baeldung (paid or free)](https://www.baeldung.com/) 6 | * [BinarySearch (free)](https://binarysearch.com/) 7 | * [Codecademy (paid)](https://www.codecademy.com/) 8 | * [Udemy (paid or free)](https://www.udemy.com/) 9 | * [CodeGym (free)](https://codegym.cc/) 10 | * [Codewars (free)](https://www.codewars.com/) 11 | * [Edx](https://www.edx.org/) 12 | * [JetBrains Academy (paid, free trial)](https://www.jetbrains.com/academy/) 13 | * [Code Academy(free)](https://www.codecademy.com/learn/learn-java) 14 | * [Programiz (free)](https://www.programiz.com/java-programming) 15 | * [Java2Blog (free)](https://java2blog.com/core-java-tutorial-for-beginners-experienced/) 16 | * [Pluralsight (paid, free trial)](https://www.pluralsight.com/browse/software-development/java) 17 | * [Object Oriented Programming in Java](https://www.coursera.org/learn/object-oriented-java/home/welcome) 18 | * [Java Basics Tutorial](https://www.javatpoint.com/java-tutorial) 19 | * [Java Placement Course](https://www.youtube.com/playlist?list=PLKKfKV1b9e8ps6dD3QA5KFfHdiWj9cB1s) 20 | * [Code Academy (free)](https://www.codecademy.com/learn/learn-java) 21 | * [Skill Share (free)](https://www.skillshare.com/classes/Java-Basics-For-Complete-Beginners/330257664?via=browse-rating-java-layout-grid) 22 | * [Baeldung (free)](https://www.baeldung.com/get-started-with-java-series) 23 | * [Dzone (free)](https://dzone.com/java-jdk-development-tutorials-tools-news) 24 | * [W3Schools](https://www.w3schools.com/java/) 25 | * [Tutorialspoint](https://www.tutorialspoint.com/java/index.htm) 26 | 27 | ## 📱 **Mobile Apps** 28 | * [SoloLearn](https://www.sololearn.com/) 29 | * [Mimo](https://getmimo.com/) 30 | * [Grasshopper](https://grasshopper.app/) 31 | * [GeeksforGeeks](https://www.geeksforgeeks.org/) 32 | * [Android Developer Guide](https://developer.android.com/guide) 33 | * [Grasshopper](https://grasshopper.app/) 34 | 35 | ## 👩🏻‍💻 **Project Based Learning** 36 | * [Build a Simple(HTTP Server with Java](https://medium.com/@ssaurel/create-a-simple-http-web-server-in-java-3fc12b29d5fd) 37 | * [How to Build a Sudoku Game Java Desktop Application](https://www.youtube.com/watch?v=qH9mWpYMtYU) 38 | 39 | ## **Docs** 40 | * [Oracle's Java Docs](https://docs.oracle.com/en/java/) 41 | 42 | ## 📚 **Books** 43 | * [Headfirst Java](https://www.wickedlysmart.com/head-first-java/) 44 | * [Java in a Nutshell](https://www.oreilly.com/library/view/java-in-a/9781492037248/) 45 | * [Clean Code](https://www.amazon.in/Clean-Code-Robert-C-Martin/dp/8131773388) 46 | * [Java Design Patterns](https://www.oreilly.com/library/view/head-first-design/0596007124/) 47 | * [Introduction to Programming in Java by Robert Sedgewick and Kevin Wayne](https://introcs.cs.princeton.edu/java/home/) 48 | * [Think Java: How to Think Like a Computer Scientist](https://greenteapress.com/wp/think-java/) 49 | 50 | ## 📺 **Youtube Channels** 51 | * [Java](https://www.youtube.com/c/javaplatform) 52 | * [JetBrainsTV](https://www.youtube.com/user/JetBrainsTV) 53 | * [Devoxx](https://www.youtube.com/channel/UCCBVCTuk6uJrN3iFV_3vurg) 54 | * [SpringDeveloper](https://www.youtube.com/user/SpringSourceDev) 55 | * [Simplicode](https://www.youtube.com/watch?v=HpLZJaQBBmM&list=PLGevxjyhYfA5fuHI3upUQBMsJXur5xtQ5) 56 | * [Spring Framework Guru](https://www.youtube.com/c/SpringframeworkGuru) 57 | * [Thorben Janssen](https://www.youtube.com/channel/UCYeDPubBiFCZXIOgGYoyADw/videos) 58 | * [Java.IL](https://www.youtube.com/channel/UC733JWbIvjSGJZzM0G8_ExA/videos) 59 | * [Java By Kiran](https://www.youtube.com/channel/UCZoq1kylnMYP_c5WG9FV9XA/videos) 60 | * [GOTOConferences](https://www.youtube.com/user/GotoConferences) 61 | * [InfoQ](https://www.youtube.com/user/MarakanaTechTV) 62 | * [vJUG](https://www.youtube.com/user/virtualJUG) 63 | * [Adam Bien](https://www.youtube.com/user/bienadam) 64 | 65 | --------------------------------------------------------------------------------