├── .gitattributes ├── .vscode └── settings.json ├── 2D Array ├── creatingArray.java ├── diagonalSum.java ├── searchInSortedMatrix.java └── spiralMatrix.java ├── Apna-College-Alpha-JAVA-DSA.iml ├── Array ├── buSellStock.java ├── findKey_LinearSearch.java ├── findkey_BinarySearch.java ├── largestInArray.java ├── maxSubArraySum_Kadanes.java ├── maxSubArraySum_prefixSum.java ├── maxSubArraysSum.java ├── pairs_in_Array.java ├── printSubArrays.java ├── reverse_an_Array.java └── trappingRainWater.java ├── Conditional Statement ├── Tax_Calculator.java ├── largest_of_two_numbers.java └── print_odd_even.java ├── Functino & Method ├── binaryToDecimal.java ├── binomial_Coefficient.java ├── decimalToBinary.java ├── factorial.java ├── prime_or_not.java └── primes_in_range.java ├── Loop ├── prime_or_not.java ├── printNum_1To10_usingWhile.java ├── reverse_of_given_number.java ├── reverse_of_number.java └── sum_of_first_natural_num.java ├── Object-Orieted-Programing ├── Object-Orieted-Programing.iml └── src │ ├── Abstract.java │ ├── AccessModifier.java │ ├── Constructors.java │ ├── GetterAndSetter.java │ ├── HierarchialInteritance.java │ ├── Inheritance.java │ ├── Interfaces.java │ ├── MultiLevelInheritance.java │ ├── StaticKeyword.java │ ├── SuperKeyword.java │ └── classes_objects.java ├── Pattern ├── charactorPattern.java ├── half_pramid.java ├── invertedStarPattern.java └── starPattern.java ├── README.md ├── Recursion └── Recursion.iml ├── Sorting ├── bubbleSort.java ├── countingSorting.java ├── inbuiltSorting.java ├── insertionSorting.java └── selectionSorting.java ├── Strings ├── Strings.iml └── src │ ├── printStrings.java │ ├── shortestPath.java │ └── stringPalindrome.java └── Variables & Data Types ├── AreaOfA_Circle.java ├── printAPattern.java ├── productOfAandB.java ├── sumOfAandB.java └── sumOfAandB_InputUser.java /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | -------------------------------------------------------------------------------- /.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "java.debug.settings.onBuildFailureProceed": true 3 | } -------------------------------------------------------------------------------- /2D Array/creatingArray.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class creatingArray { 4 | public static void main(String[] args) { 5 | Scanner sc = new Scanner(System.in); 6 | int[][] matrix = new int[3][3]; 7 | int n = matrix.length; 8 | int m = matrix[0].length; 9 | 10 | for (int i = 0; i < n; i++) { 11 | for (int j = 0; j < m; j++) { 12 | matrix[i][j] = sc.nextInt(); 13 | } 14 | } 15 | for (int[] ints : matrix) { 16 | for (int j = 0; j < m; j++) { 17 | System.out.print(ints[j] + " "); 18 | } 19 | System.out.println(); 20 | } 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /2D Array/diagonalSum.java: -------------------------------------------------------------------------------- 1 | public class diagonalSum { 2 | public static int diagonalSumPrint(int matrix[][]){ 3 | int sum = 0; 4 | // 5 | // Optimised Aprooch 6 | for(int i=0; i=0){ 6 | if(matrix[row][col] == key){ 7 | System.out.println("Found Key at (" + row + "," + col + ")"); 8 | return true; 9 | }else if(key < matrix[row][col]){ 10 | col--; 11 | }else{ 12 | row++; 13 | } 14 | } 15 | System.out.println("Key not found"); 16 | return false; 17 | } 18 | public static void main(String[] args) { 19 | int matrix[][] = { 20 | {10,20,30,40}, 21 | {15,25,35,45}, 22 | {27,29,37,48}, 23 | {32,33,39,50} 24 | }; 25 | int key = 33; 26 | stairCaseSearch(matrix,key); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /2D Array/spiralMatrix.java: -------------------------------------------------------------------------------- 1 | public class spiralMatrix { 2 | public static void printSpiral(int matrix[][]) { 3 | int startRow = 0; 4 | int startCol = 0; 5 | int endRow = matrix.length - 1; 6 | int endCol = matrix[0].length - 1; 7 | 8 | while (startRow <= endRow && startCol <= endCol) { 9 | // Top 10 | for (int j = startCol; j <= endCol; j++) { 11 | System.out.print(matrix[startRow][j] + " "); 12 | } 13 | // right 14 | for (int i = startRow + 1; i <= endRow; i++) { 15 | System.out.print(matrix[i][endCol] + " "); 16 | } 17 | // bottom 18 | for (int j = endCol - 1; j >= startCol; j--) { 19 | if (startRow == endRow) { 20 | break; 21 | } 22 | System.out.print(matrix[endRow][j] + " "); 23 | } 24 | // left 25 | for (int i = endRow - 1; i >= startRow; i--) { 26 | if (startCol == endCol) { 27 | break; 28 | } 29 | System.out.print(matrix[i][startCol] + " "); 30 | } 31 | startRow++; 32 | startCol++; 33 | endRow--; 34 | endCol--; 35 | } 36 | } 37 | 38 | public static void main(String[] args) { 39 | int matrix[][] = { 40 | { 1, 2, 3, 4 }, 41 | { 5, 6, 7, 8 }, 42 | { 9, 10, 11, 12 }, 43 | { 13, 14, 15, 16 } 44 | }; 45 | printSpiral(matrix); 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /Apna-College-Alpha-JAVA-DSA.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /Array/buSellStock.java: -------------------------------------------------------------------------------- 1 | package Array; 2 | 3 | public class buSellStock { 4 | public static int buySellStocks(int prices[]) { 5 | int buyPrice = Integer.MAX_VALUE; 6 | int maxProfit = 0; 7 | for (int i = 0; i < prices.length; i++) { 8 | if (buyPrice < prices[i]) { 9 | int profit = prices[i] - buyPrice; 10 | maxProfit = Math.max(maxProfit, profit); 11 | } else { 12 | buyPrice = prices[i]; 13 | } 14 | } 15 | return maxProfit; 16 | } 17 | 18 | public static void main(String[] args) { 19 | int prices[] = { 7, 1, 5, 3, 6, 4 }; 20 | System.out.println(buySellStocks(prices)); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /Array/findKey_LinearSearch.java: -------------------------------------------------------------------------------- 1 | package Array; 2 | 3 | public class findKey_LinearSearch { 4 | public static int linearSearch(int numbers[], int key) { 5 | for (int i = 0; i < numbers.length; i++) { 6 | if (numbers[i] == key) { 7 | return i; 8 | } 9 | } 10 | return -1; 11 | } 12 | 13 | public static void main(String[] args) { 14 | int numbers[] = { 2, 4, 6, 8, 10, 12, 14, 16 }; 15 | int key = 10; 16 | int index = linearSearch(numbers, key); 17 | if (index == -1) { 18 | System.out.print("Key Not Found"); 19 | } else { 20 | System.out.print("Key is at Index : " + index); 21 | } 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /Array/findkey_BinarySearch.java: -------------------------------------------------------------------------------- 1 | package Array; 2 | 3 | public class findkey_BinarySearch { 4 | public static int binarySearch(int numbers[], int key) { 5 | int start = 0; 6 | int end = numbers.length - 1; 7 | 8 | while (start <= end) { 9 | int mid = (start + end) / 2; 10 | 11 | // Comparisons 12 | if (numbers[mid] == key) { 13 | return mid; 14 | } 15 | // right half 16 | if (numbers[mid] < key) { 17 | start = mid + 1; 18 | } else { 19 | end = mid - 1; 20 | } 21 | } 22 | return -1; 23 | } 24 | 25 | public static void main(String[] args) { 26 | int numbers[] = { 2, 4, 6, 8, 10, 12, 14 }; 27 | int key = 2; 28 | System.out.println("index for key is : " + binarySearch(numbers, key)); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /Array/largestInArray.java: -------------------------------------------------------------------------------- 1 | package Array; 2 | 3 | public class largestInArray { 4 | public static int getLargest(int numbers[]) { 5 | int largest = Integer.MIN_VALUE; 6 | for (int i = 0; i < numbers.length; i++) { 7 | if (largest < numbers[i]) { 8 | largest = numbers[i]; 9 | } 10 | } 11 | return largest; 12 | } 13 | 14 | public static void main(String[] args) { 15 | int numbers[] = { 1, 2, 6, 3, 5 }; 16 | System.out.println("Largest numbers is : " + getLargest(numbers)); 17 | 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /Array/maxSubArraySum_Kadanes.java: -------------------------------------------------------------------------------- 1 | package Array; 2 | 3 | public class maxSubArraySum_Kadanes { 4 | public static void kadanes(int numbers[]) { 5 | int ms = Integer.MIN_VALUE; 6 | int cs = 0; 7 | 8 | for (int i = 0; i < numbers.length; i++) { 9 | cs = cs + numbers[i]; 10 | if (cs < 0) { 11 | cs = 0; 12 | } 13 | ms = Math.max(cs, ms); 14 | } 15 | System.out.println("Max Sum : " + ms); 16 | } 17 | 18 | public static void main(String[] args) { 19 | int numbers[] = { -2, -3, 4, -1, -2, 1, 5, -3 }; 20 | kadanes(numbers); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /Array/maxSubArraySum_prefixSum.java: -------------------------------------------------------------------------------- 1 | package Array; 2 | 3 | public class maxSubArraySum_prefixSum { 4 | public static void maxSubArraysSum(int numbers[]) { 5 | int currSum = 0; 6 | int maxSum = Integer.MIN_VALUE; 7 | int prefix[] = new int[numbers.length]; 8 | prefix[0] = numbers[0]; 9 | for (int i = 1; i < prefix.length; i++) { 10 | prefix[i] = prefix[i - 1] + numbers[i]; 11 | } 12 | for (int i = 0; i < numbers.length; i++) { 13 | int start = i; 14 | for (int j = i; j < numbers.length; j++) { 15 | int end = j; 16 | 17 | currSum = start == 0 ? prefix[end] : prefix[end] - prefix[start - 1]; 18 | 19 | if (maxSum < currSum) { 20 | maxSum = currSum; 21 | } 22 | } 23 | } 24 | System.out.println("Max Sum : " + maxSum); 25 | } 26 | 27 | public static void main(String[] args) { 28 | int numbers[] = { 2, 4, 6, 8, 10 }; 29 | maxSubArraysSum(numbers); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /Array/maxSubArraysSum.java: -------------------------------------------------------------------------------- 1 | package Array; 2 | 3 | public class maxSubArraysSum { 4 | public static void maxSubarraySum(int numbers[]) { 5 | int currSum = 0; 6 | int maxSum = Integer.MIN_VALUE; 7 | 8 | for (int i = 0; i < numbers.length; i++) { 9 | for (int j = i; j < numbers.length; j++) { 10 | currSum = 0; 11 | for (int k = i; k <= j; k++) { 12 | currSum += numbers[k]; 13 | } 14 | System.out.println(currSum); 15 | if (maxSum < currSum) { 16 | maxSum = currSum; 17 | } 18 | } 19 | } 20 | System.out.println("Max Sum : " + maxSum); 21 | } 22 | 23 | public static void main(String[] args) { 24 | int numbers[] = { 2, 4, 6, 8, 10 }; 25 | maxSubarraySum(numbers); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /Array/pairs_in_Array.java: -------------------------------------------------------------------------------- 1 | package Array; 2 | 3 | public class pairs_in_Array { 4 | public static void printPairs(int numbers[]) { 5 | for (int i = 0; i < numbers.length; i++) { 6 | for (int j = i + 1; j < numbers.length; j++) { 7 | System.out.print("(" + numbers[i] + "," + numbers[j] + ")"); 8 | } 9 | System.out.println(); 10 | } 11 | } 12 | 13 | public static void main(String[] args) { 14 | int numbers[] = { 2, 4, 6, 8, 10 }; 15 | printPairs(numbers); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /Array/printSubArrays.java: -------------------------------------------------------------------------------- 1 | package Array; 2 | 3 | public class printSubArrays { 4 | public static void printSubArray(int numbers[]) { 5 | for (int i = 0; i < numbers.length; i++) { 6 | for (int j = i; j < numbers.length; j++) { 7 | for (int k = i; k <= j; k++) { 8 | System.out.print(numbers[k] + " "); 9 | } 10 | System.out.println(); 11 | } 12 | System.out.println(); 13 | } 14 | } 15 | 16 | public static void main(String[] args) { 17 | int numbers[] = { 2, 4, 6, 8, 10 }; 18 | printSubArray(numbers); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /Array/reverse_an_Array.java: -------------------------------------------------------------------------------- 1 | package Array; 2 | 3 | public class reverse_an_Array { 4 | public static void reverse(int numbers[]) { 5 | int start = 0; 6 | int end = numbers.length - 1; 7 | 8 | while (start < end) { 9 | // Swap 10 | int temp = numbers[end]; 11 | numbers[end] = numbers[start]; 12 | numbers[start] = temp; 13 | 14 | start++; 15 | end--; 16 | } 17 | } 18 | 19 | public static void main(String[] args) { 20 | int numbers[] = { 2, 4, 6, 8, 10 }; 21 | reverse(numbers); 22 | for (int i = 0; i < numbers.length; i++) { 23 | System.out.print(numbers[i] + " "); 24 | } 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /Array/trappingRainWater.java: -------------------------------------------------------------------------------- 1 | package Array; 2 | 3 | public class trappingRainWater { 4 | public static int trappedRainWater(int height[]) { 5 | int n = height.length; 6 | // Calculate left max boundary - Array 7 | int leftMax[] = new int[n]; 8 | leftMax[0] = height[0]; 9 | for (int i = 1; i < n; i++) { 10 | leftMax[i] = Math.max(height[i], leftMax[i - 1]); 11 | } 12 | // Calculate right max boundary - Array 13 | int rightMax[] = new int[n]; 14 | rightMax[n - 1] = height[n - 1]; 15 | for (int i = n - 2; i >= 0; i--) { 16 | rightMax[i] = Math.max(height[i], rightMax[i + 1]); 17 | } 18 | // loop 19 | int trappedWater = 0; 20 | for (int i = 0; i < n; i++) { 21 | int waterLevel = Math.min(leftMax[i], rightMax[i]); 22 | trappedWater += waterLevel - height[i]; 23 | } 24 | return trappedWater; 25 | } 26 | 27 | public static void main(String[] args) { 28 | int height[] = { 4, 2, 0, 6, 3, 2, 5 }; 29 | System.out.println(trappedRainWater(height)); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /Conditional Statement/Tax_Calculator.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class Tax_Calculator { 4 | public static void main(String args[]) { 5 | Scanner sc = new Scanner(System.in); 6 | int income = sc.nextInt(); 7 | int tax; 8 | 9 | if (income < 500000) { 10 | tax = 0; 11 | } else if (income >= 500000 && income < 1000000) { 12 | tax = (int) (income * 0.2); 13 | } else { 14 | tax = (int) (income * 0.3); 15 | } 16 | System.out.print("Your Tax is : " + tax); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /Conditional Statement/largest_of_two_numbers.java: -------------------------------------------------------------------------------- 1 | 2 | public class largest_of_two_numbers { 3 | public static void main(String[] args) { 4 | int a = 1; 5 | int b = 5; 6 | 7 | if (a >= b) { 8 | System.out.print("A is largest"); 9 | } else { 10 | System.out.print("B is largest"); 11 | } 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /Conditional Statement/print_odd_even.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class print_odd_even { 4 | public static void main(String[] args) { 5 | Scanner sc = new Scanner(System.in); 6 | int num = sc.nextInt(); 7 | 8 | if (num % 2 == 0) { 9 | System.out.print("Number is Even"); 10 | } else { 11 | System.out.print("Number is Odd"); 12 | } 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /Functino & Method/binaryToDecimal.java: -------------------------------------------------------------------------------- 1 | public class binaryToDecimal { 2 | public static void binToDec(int binNum) { 3 | int myNum = binNum; 4 | int pow = 0; 5 | int decNum = 0; 6 | 7 | while (binNum > 0) { 8 | int lastDigit = binNum % 10; 9 | decNum = decNum + (lastDigit * (int) Math.pow(2, pow)); 10 | pow++; 11 | binNum = binNum / 10; 12 | } 13 | System.out.println("Decimal of " + myNum + " = " + decNum); 14 | } 15 | 16 | public static void main(String args[]) { 17 | binToDec(101); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /Functino & Method/binomial_Coefficient.java: -------------------------------------------------------------------------------- 1 | public class binomial_Coefficient { 2 | public static int findFactorial(int n) { 3 | int f = 1; 4 | for (int i = 1; i <= n; i++) { 5 | f = f * i; 6 | } 7 | return f; 8 | } 9 | 10 | public static int binCoeff(int n, int r) { 11 | int fact_n = findFactorial(n); 12 | int fact_r = findFactorial(r); 13 | int fact_nmr = findFactorial(n - r); 14 | 15 | int binCoeff = fact_n / (fact_r * fact_nmr); 16 | return binCoeff; 17 | } 18 | 19 | public static void main(String[] args) { 20 | System.out.println(binCoeff(5, 2)); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /Functino & Method/decimalToBinary.java: -------------------------------------------------------------------------------- 1 | public class decimalToBinary { 2 | public static void decToBin(int n) { 3 | int myNum = n; 4 | int pow = 0; 5 | int binNum = 0; 6 | 7 | while (n > 0) { 8 | int rem = n % 2; 9 | binNum = binNum + (rem * (int) Math.pow(10, pow)); 10 | pow++; 11 | n = n / 2; 12 | } 13 | System.out.println("Binary form of " + myNum + " = " + binNum); 14 | } 15 | 16 | public static void main(String[] args) { 17 | decToBin(7); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /Functino & Method/factorial.java: -------------------------------------------------------------------------------- 1 | 2 | public class factorial { 3 | public static int factoriall(int n) { 4 | int f = 1; 5 | for (int i = 1; i <= n; i++) { 6 | f = f * i; 7 | } 8 | return f; 9 | } 10 | 11 | public static void main(String[] args) { 12 | System.out.println(factoriall(4)); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /Functino & Method/prime_or_not.java: -------------------------------------------------------------------------------- 1 | public class prime_or_not { 2 | public static boolean isPrime(int n) { 3 | if (n == 2) { 4 | return true; 5 | } 6 | for (int i = 2; i <= Math.sqrt(n); i++) { 7 | if (n % i == 0) { 8 | return false; 9 | } 10 | } 11 | return true; 12 | } 13 | 14 | public static void main(String[] args) { 15 | System.out.println(isPrime(10)); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /Functino & Method/primes_in_range.java: -------------------------------------------------------------------------------- 1 | public class primes_in_range { 2 | public static boolean isPrime(int n) { 3 | if (n == 2) { 4 | return true; 5 | } 6 | for (int i = 2; i <= Math.sqrt(n); i++) { 7 | if (n % i == 0) { 8 | return false; 9 | } 10 | } 11 | return true; 12 | } 13 | 14 | public static void primesInRange(int n) { 15 | for (int i = 2; i <= n; i++) { 16 | if (isPrime(i)) { 17 | System.out.print(i + " "); 18 | } 19 | } 20 | System.out.println(); 21 | } 22 | 23 | public static void main(String args[]) { 24 | primesInRange(20); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /Loop/prime_or_not.java: -------------------------------------------------------------------------------- 1 | package Loop; 2 | 3 | import java.util.*; 4 | 5 | public class prime_or_not { 6 | public static void main(String[] args) { 7 | Scanner sc = new Scanner(System.in); 8 | int n = sc.nextInt(); 9 | 10 | if (n == 2) { 11 | System.out.println("N is Prime"); 12 | } else { 13 | boolean isPrime = true; 14 | 15 | for (int i = 2; i <= Math.sqrt(n); i++) { 16 | if (n % i == 0) { 17 | isPrime = false; 18 | } 19 | } 20 | if (isPrime == true) { 21 | System.out.println("n is Prime Number"); 22 | } else { 23 | System.out.println("n is not prime"); 24 | } 25 | } 26 | 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /Loop/printNum_1To10_usingWhile.java: -------------------------------------------------------------------------------- 1 | package Loop; 2 | 3 | public class printNum_1To10_usingWhile { 4 | public static void main(String[] args) { 5 | int counter = 1; 6 | 7 | while (counter <= 10) { 8 | System.out.println(counter); 9 | counter++; 10 | } 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /Loop/reverse_of_given_number.java: -------------------------------------------------------------------------------- 1 | package Loop; 2 | 3 | public class reverse_of_given_number { 4 | public static void main(String[] args) { 5 | int n = 10899; 6 | int rev = 0; 7 | 8 | while (n > 0) { 9 | int lastDigit = n % 10; 10 | rev = (rev * 10) + lastDigit; 11 | n = n / 10; 12 | } 13 | System.out.print(rev); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /Loop/reverse_of_number.java: -------------------------------------------------------------------------------- 1 | package Loop; 2 | 3 | public class reverse_of_number { 4 | public static void main(String[] args) { 5 | int n = 10899; 6 | while (n > 0) { 7 | int lastDigit = n % 10; 8 | System.out.print(lastDigit); 9 | n = n / 10; 10 | } 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /Loop/sum_of_first_natural_num.java: -------------------------------------------------------------------------------- 1 | package Loop; 2 | 3 | import java.util.*; 4 | 5 | public class sum_of_first_natural_num { 6 | public static void main(String[] args) { 7 | Scanner sc = new Scanner(System.in); 8 | int n = sc.nextInt(); 9 | int i = 1; 10 | int sum = 0; 11 | 12 | while (i <= n) { 13 | sum += i; 14 | i++; 15 | } 16 | System.out.print(sum); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /Object-Orieted-Programing/Object-Orieted-Programing.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /Object-Orieted-Programing/src/Abstract.java: -------------------------------------------------------------------------------- 1 | public class Abstract { 2 | public static void main(String[] args) { 3 | Horse h =new Horse(); 4 | h.eat(); 5 | h.walk(); 6 | Chicken c = new Chicken(); 7 | c.eat(); 8 | c.walk(); 9 | } 10 | } 11 | abstract class Animal3{ 12 | String color; 13 | Animal3(){ 14 | color = "brown"; 15 | } 16 | void eat(){ 17 | System.out.println("animal eats"); 18 | } 19 | abstract void walk(); 20 | } 21 | 22 | class Horse extends Animal3{ 23 | void changeColor(){ 24 | color = "dark brown"; 25 | } 26 | 27 | void walk(){ 28 | System.out.println("walks on 4 legs"); 29 | } 30 | } 31 | class Chicken extends Animal3{ 32 | void changeColor(){ 33 | color = "yellow"; 34 | } 35 | 36 | void walk(){ 37 | System.out.println("walks on 2 legs"); 38 | } 39 | } -------------------------------------------------------------------------------- /Object-Orieted-Programing/src/AccessModifier.java: -------------------------------------------------------------------------------- 1 | public class AccessModifier { 2 | public static void main(String[] args) { 3 | BankAccount myAcc = new BankAccount(); 4 | myAcc.userName = "rahuljangid236"; 5 | myAcc.setPassword("abcd"); 6 | } 7 | } 8 | class BankAccount{ 9 | public String userName; 10 | private String password; 11 | public void setPassword(String pwd){ 12 | password = pwd; 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /Object-Orieted-Programing/src/Constructors.java: -------------------------------------------------------------------------------- 1 | public class Constructors { 2 | public static void main(String []args){ 3 | Student1 s1 = new Student1("Rahul"); 4 | System.out.println(s1.name); 5 | } 6 | } 7 | class Student1{ 8 | String name; 9 | int roll; 10 | Student1(String name){ 11 | this.name = name; 12 | System.out.println("Constructor is Called"); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /Object-Orieted-Programing/src/GetterAndSetter.java: -------------------------------------------------------------------------------- 1 | public class GetterAndSetter { 2 | public static void main(String[] args) { 3 | Pen1 p1 = new Pen1(); 4 | p1.setColor("Blue"); 5 | System.out.println(p1.getColor()); 6 | p1.setTip(5); 7 | System.out.println(p1.getTip()); 8 | p1.setColor("Yellow"); 9 | System.out.println(p1.getColor()); 10 | 11 | } 12 | } 13 | class Pen1{ 14 | private String color; 15 | private int tip; 16 | String getColor(){ 17 | return this.color; 18 | } 19 | int getTip(){ 20 | return this.tip; 21 | } 22 | void setColor(String newColor){ 23 | color = newColor; 24 | } 25 | void setTip(int newTip){ 26 | tip = newTip; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /Object-Orieted-Programing/src/HierarchialInteritance.java: -------------------------------------------------------------------------------- 1 | public class HierarchialInteritance { 2 | public static void main(String[] args) { 3 | Bird eagle = new Bird(); 4 | eagle.eat(); 5 | } 6 | } 7 | class Animal2{ 8 | String color; 9 | void eat(){ 10 | System.out.println("eats"); 11 | } 12 | void breathe(){ 13 | 14 | System.out.println("breathes"); 15 | } 16 | } 17 | class Mammal extends Animal2{ 18 | void walk(){ 19 | System.out.println("walks"); 20 | } 21 | } 22 | class Fish1 extends Animal2{ 23 | void swim(){ 24 | System.out.println("swim"); 25 | } 26 | } 27 | class Bird extends Animal2{ 28 | void fly(){ 29 | System.out.println("fly"); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /Object-Orieted-Programing/src/Inheritance.java: -------------------------------------------------------------------------------- 1 | public class Inheritance { 2 | public static void main(String[] args) { 3 | Fish shark = new Fish(); 4 | shark.eat(); 5 | } 6 | } 7 | //Base Class 8 | class Animal{ 9 | String color; 10 | void eat(){ 11 | System.out.println("eats"); 12 | } 13 | void breathe(){ 14 | System.out.println("breathes"); 15 | } 16 | } 17 | //Derived Class 18 | class Fish extends Animal{ 19 | int fins; 20 | void swim(){ 21 | System.out.println("swims in water"); 22 | } 23 | } 24 | 25 | -------------------------------------------------------------------------------- /Object-Orieted-Programing/src/Interfaces.java: -------------------------------------------------------------------------------- 1 | public class Interfaces { 2 | public static void main(String[] args) { 3 | Queen q = new Queen(); 4 | q.moves(); 5 | } 6 | } 7 | interface ChessPlayer{ 8 | void moves(); 9 | } 10 | 11 | class Queen implements ChessPlayer{ 12 | public void moves(){ 13 | System.out.println("up,down,left,right diagonal in all 4 dire "); 14 | } 15 | } 16 | class Rook implements ChessPlayer{ 17 | public void moves(){ 18 | System.out.println("up,down,right,left"); 19 | } 20 | } 21 | class King implements ChessPlayer{ 22 | public void moves(){ 23 | System.out.println("up,down,right,left diagonal by 1"); 24 | } 25 | } -------------------------------------------------------------------------------- /Object-Orieted-Programing/src/MultiLevelInheritance.java: -------------------------------------------------------------------------------- 1 | public class MultiLevelInheritance { 2 | public static void main(String[] args) { 3 | Dog dobby = new Dog(); 4 | dobby.eat(); 5 | dobby.legs = 4; 6 | System.out.println(dobby.legs); 7 | } 8 | } 9 | class Animal1{ 10 | String color; 11 | void eat(){ 12 | System.out.println("eats"); 13 | } 14 | void breathe(){ 15 | System.out.println("breathes"); 16 | } 17 | } 18 | class Mammels extends Animal1{ 19 | int legs; 20 | } 21 | class Dog extends Mammels{ 22 | String bread; 23 | } -------------------------------------------------------------------------------- /Object-Orieted-Programing/src/StaticKeyword.java: -------------------------------------------------------------------------------- 1 | public class StaticKeyword { 2 | public static void main(String[] args) { 3 | Students s1 = new Students(); 4 | s1.schoolName = "JMV"; 5 | Students s2 = new Students(); 6 | System.out.println(s2.schoolName); 7 | } 8 | } 9 | class Students{ 10 | String name; 11 | int roll; 12 | static String schoolName; 13 | 14 | void setName(String name){ 15 | this.name = name; 16 | } 17 | String getName(){ 18 | return this.name; 19 | } 20 | } -------------------------------------------------------------------------------- /Object-Orieted-Programing/src/SuperKeyword.java: -------------------------------------------------------------------------------- 1 | public class SuperKeyword { 2 | public static void main(String[] args) { 3 | Horses h = new Horses(); 4 | } 5 | } 6 | class Animals{ 7 | Animals(){ 8 | System.out.println("animals cons called"); 9 | } 10 | } 11 | class Horses extends Animals{ 12 | Horses(){ 13 | super(); 14 | System.out.println("Horses cons called"); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /Object-Orieted-Programing/src/classes_objects.java: -------------------------------------------------------------------------------- 1 | public class classes_objects { 2 | public static void main(String[] args) { 3 | Pen p1 = new Pen(); 4 | p1.setColor("Blue"); 5 | System.out.println(p1.color); 6 | p1.setTip(5); 7 | System.out.println(p1.tip); 8 | } 9 | } 10 | class Pen{ 11 | String color; 12 | int tip; 13 | 14 | void setColor(String newColor){ 15 | color = newColor; 16 | } 17 | void setTip(int newTip){ 18 | tip = newTip; 19 | } 20 | } 21 | 22 | class Student{ 23 | String name; 24 | int age; 25 | float per; 26 | void calcPer(int phy,int chem, int math){ 27 | per = (phy+chem+math)/3; 28 | } 29 | } -------------------------------------------------------------------------------- /Pattern/charactorPattern.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class charactorPattern { 4 | public static void main(String[] args) { 5 | Scanner sc = new Scanner(System.in); 6 | int n = sc.nextInt(); 7 | char ch = 'A'; 8 | 9 | for (int i = 1; i <= n; i++) { 10 | for (int j = 1; j <= i; j++) { 11 | System.out.print(ch); 12 | ch++; 13 | } 14 | System.out.println(); 15 | } 16 | 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /Pattern/half_pramid.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class half_pramid { 4 | public static void main(String[] args) { 5 | Scanner sc = new Scanner(System.in); 6 | int n = sc.nextInt(); 7 | 8 | for (int i = 1; i <= n; i++) { 9 | for (int j = 1; j <= i; j++) { 10 | System.out.print(j); 11 | } 12 | System.out.println(); 13 | } 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /Pattern/invertedStarPattern.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class invertedStarPattern { 4 | public static void main(String[] args) { 5 | Scanner sc = new Scanner(System.in); 6 | int n = sc.nextInt(); 7 | for (int i = 1; i <= n; i++) { 8 | for (int j = 1; j <= n - i + 1; j++) { 9 | System.out.print("*"); 10 | } 11 | System.out.println(); 12 | } 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /Pattern/starPattern.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class starPattern { 4 | public static void main(String[] args) { 5 | Scanner sc = new Scanner(System.in); 6 | int n = sc.nextInt(); 7 | 8 | for (int i = 0; i <= n; i++) { 9 | for (int j = 0; j < i; j++) { 10 | System.out.print("*"); 11 | } 12 | System.out.println(); 13 | } 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Apna-College-Alpha-JAVA-DSA 2 | -------------------------------------------------------------------------------- /Recursion/Recursion.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /Sorting/bubbleSort.java: -------------------------------------------------------------------------------- 1 | package Sorting; 2 | 3 | public class bubbleSort { 4 | public static void arraySort(int arr[]) { 5 | for (int i = 0; i < arr.length - 1; i++) { 6 | for (int j = 0; j < arr.length - 1 - i; j++) { 7 | if (arr[j] > arr[j + 1]) { 8 | // Swap 9 | int temp = arr[j]; 10 | arr[j] = arr[j + 1]; 11 | arr[j + 1] = temp; 12 | } 13 | } 14 | } 15 | } 16 | 17 | public static void printArray(int arr[]) { 18 | for (int i = 0; i < arr.length; i++) { 19 | System.out.print(arr[i] + " "); 20 | } 21 | } 22 | 23 | public static void main(String[] args) { 24 | int arr[] = { 5, 4, 1, 3, 2 }; 25 | arraySort(arr); 26 | printArray(arr); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /Sorting/countingSorting.java: -------------------------------------------------------------------------------- 1 | package Sorting; 2 | 3 | public class countingSorting { 4 | public static void countingSort(int arr[]) { 5 | int largest = Integer.MIN_VALUE; 6 | for (int i = 0; i < arr.length; i++) { 7 | largest = Math.max(largest, arr[i]); 8 | } 9 | 10 | int count[] = new int[largest + 1]; 11 | for (int i = 0; i < arr.length; i++) { 12 | count[arr[i]]++; 13 | } 14 | // Sorting 15 | int j = 0; 16 | for (int i = 0; i < count.length; i++) { 17 | while (count[i] > 0) { 18 | arr[j] = i; 19 | j++; 20 | count[i]--; 21 | } 22 | } 23 | } 24 | 25 | public static void printArray(int arr[]) { 26 | for (int i = 0; i < arr.length; i++) { 27 | System.out.print(arr[i] + " "); 28 | } 29 | } 30 | 31 | public static void main(String[] args) { 32 | int arr[] = { 5, 4, 1, 3, 2 }; 33 | countingSort(arr); 34 | printArray(arr); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /Sorting/inbuiltSorting.java: -------------------------------------------------------------------------------- 1 | package Sorting; 2 | 3 | import java.util.Arrays; 4 | 5 | public class inbuiltSorting { 6 | public static void printArray(int arr[]) { 7 | for (int i = 0; i < arr.length; i++) { 8 | System.out.print(arr[i] + " "); 9 | } 10 | } 11 | 12 | public static void main(String[] args) { 13 | int arr[] = { 5, 4, 1, 3, 2 }; 14 | Arrays.sort(arr); 15 | printArray(arr); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /Sorting/insertionSorting.java: -------------------------------------------------------------------------------- 1 | package Sorting; 2 | 3 | public class insertionSorting { 4 | public static void insertionSort(int arr[]) { 5 | for (int i = 1; i < arr.length; i++) { 6 | int curr = arr[i]; 7 | int prev = i - 1; 8 | // finding out the currect pos to insert 9 | while (prev >= 0 && arr[prev] > curr) { 10 | arr[prev + 1] = arr[prev]; 11 | prev--; 12 | } 13 | // insertino 14 | arr[prev + 1] = curr; 15 | 16 | } 17 | } 18 | 19 | public static void printArray(int arr[]) { 20 | for (int i = 0; i < arr.length; i++) { 21 | System.out.print(arr[i] + " "); 22 | } 23 | } 24 | 25 | public static void main(String[] args) { 26 | int arr[] = { 5, 4, 1, 3, 2 }; 27 | insertionSort(arr); 28 | printArray(arr); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /Sorting/selectionSorting.java: -------------------------------------------------------------------------------- 1 | package Sorting; 2 | 3 | public class selectionSorting { 4 | public static void selectionSort(int arr[]) { 5 | for (int i = 0; i < arr.length - 1; i++) { 6 | int minPos = i; 7 | for (int j = i + 1; j < arr.length; j++) { 8 | if (arr[minPos] > arr[j]) { 9 | minPos = j; 10 | } 11 | } 12 | // Swap 13 | int temp = arr[minPos]; 14 | arr[minPos] = arr[i]; 15 | arr[i] = temp; 16 | } 17 | } 18 | 19 | public static void printArray(int arr[]) { 20 | for (int i = 0; i < arr.length; i++) { 21 | System.out.print(arr[i] + " "); 22 | } 23 | } 24 | 25 | public static void main(String[] args) { 26 | int arr[] = { 5, 4, 1, 3, 2 }; 27 | selectionSort(arr); 28 | printArray(arr); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /Strings/Strings.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /Strings/src/printStrings.java: -------------------------------------------------------------------------------- 1 | public class printStrings { 2 | public static void printLetters(String fullName){ 3 | for(int i=0; i