├── 1. Variables and Data Types ├── .idea │ ├── .gitignore │ ├── .name │ ├── misc.xml │ ├── modules.xml │ └── runConfigurations.xml ├── Variables and Data Types.iml ├── out │ └── production │ │ └── Variables and Data Types │ │ ├── AreaOfSquare.class │ │ ├── Bill.class │ │ └── ThreeNumbersAvg.class └── src │ ├── AreaOfSquare.java │ ├── Bill.java │ └── ThreeNumbersAvg.java ├── 2. Conditionals ├── .idea │ ├── .gitignore │ ├── .name │ ├── misc.xml │ ├── modules.xml │ └── runConfigurations.xml ├── Conditionals.iml ├── out │ └── production │ │ └── Conditionals │ │ ├── Feverish.class │ │ ├── LeapYear.class │ │ ├── PosOrNeg.class │ │ └── Weekday.class └── src │ ├── Feverish.java │ ├── LeapYear.java │ ├── PosOrNeg.java │ └── Weekday.java ├── 3. Loops ├── .idea │ ├── .gitignore │ ├── .name │ ├── misc.xml │ ├── modules.xml │ └── runConfigurations.xml ├── Loops.iml ├── out │ └── production │ │ └── Loops │ │ ├── Factorial.class │ │ ├── SumEvenSumOdd.class │ │ └── Table.class └── src │ ├── Factorial.java │ ├── SumEvenSumOdd.java │ └── Table.java ├── 4. Pattern ├── .idea │ ├── .gitignore │ ├── .name │ ├── misc.xml │ ├── modules.xml │ ├── runConfigurations.xml │ └── uiDesigner.xml ├── 4. Pattern.iml ├── out │ └── production │ │ └── 4. Pattern │ │ ├── BinaryHalfPyramid.class │ │ ├── ButterflyPattern.class │ │ ├── Diamond.class │ │ ├── FloydsTriangle.class │ │ ├── HalfPyramid.class │ │ ├── HalfPyramidAlphabets.class │ │ ├── HalfPyramidNum.class │ │ ├── HollowRectangle.class │ │ ├── HollowRhombus.class │ │ ├── InvertedHalfPyramid.class │ │ ├── InvertedHalfPyramidNum.class │ │ ├── NumberPyramid.class │ │ ├── Rhombus.class │ │ └── RotatedHalfPyramid.class └── src │ ├── BinaryHalfPyramid.java │ ├── ButterflyPattern.java │ ├── Diamond.java │ ├── FloydsTriangle.java │ ├── HalfPyramid.java │ ├── HalfPyramidAlphabets.java │ ├── HalfPyramidNum.java │ ├── HollowRectangle.java │ ├── HollowRhombus.java │ ├── InvertedHalfPyramid.java │ ├── InvertedHalfPyramidNum.java │ ├── NumberPyramid.java │ ├── Rhombus.java │ └── RotatedHalfPyramid.java ├── 5. Functions and Methods ├── .idea │ ├── .gitignore │ ├── .name │ ├── misc.xml │ ├── modules.xml │ ├── runConfigurations.xml │ └── vcs.xml ├── Functions and Methods.iml ├── out │ └── production │ │ └── Functions and Methods │ │ ├── AverageOfThreeNums.class │ │ ├── BinaryToDecimal.class │ │ ├── BinomialCoefficient.class │ │ ├── CheckEven.class │ │ ├── DecimalToBinary.class │ │ ├── Palindrome.class │ │ ├── PrimeOrNot.class │ │ ├── PrimesInRange.class │ │ ├── ProductOfAandB.class │ │ ├── Solution.class │ │ └── SumOfDigits.class └── src │ ├── AverageOfThreeNums.java │ ├── BinaryToDecimal.java │ ├── BinomialCoefficient.java │ ├── CheckEven.java │ ├── DecimalToBinary.java │ ├── Palindrome.java │ ├── PrimeOrNot.java │ ├── PrimesInRange.java │ ├── ProductOfAandB.java │ ├── Solution.java │ └── SumOfDigits.java ├── 6. Arrays ├── .idea │ ├── .gitignore │ ├── .name │ ├── misc.xml │ ├── modules.xml │ ├── runConfigurations.xml │ ├── uiDesigner.xml │ └── vcs.xml ├── Arrays.iml ├── out │ └── production │ │ └── Arrays │ │ ├── BinarySearch.class │ │ ├── Ceiling.class │ │ ├── CheckForDuplicate.class │ │ ├── ChocolateDistribution.class │ │ ├── FindPivot.class │ │ ├── FirstAndLastPosition.class │ │ ├── Floor.class │ │ ├── LargestAndSmallestInArray.class │ │ ├── MaxAndMin.class │ │ ├── MaxSubarraySum.class │ │ ├── Pairs.class │ │ ├── PeakIndexInMountainArray.class │ │ ├── PrintSubarrays.class │ │ ├── RainwaterTrapping.class │ │ ├── ReverseArray.class │ │ ├── SearchInInfiniteArray.class │ │ ├── SearchInMountainArray.class │ │ ├── SearchInRotated.class │ │ ├── SearchInRotatedWIthDuplicates.class │ │ ├── SmallestLetterGreaterThanTarget.class │ │ ├── Solution.class │ │ ├── SplitArrayLargestSum.class │ │ └── StockBuySell.class └── src │ ├── BinarySearch.java │ ├── Ceiling.java │ ├── CheckForDuplicate.java │ ├── ChocolateDistribution.java │ ├── FindPivot.java │ ├── FirstAndLastPosition.java │ ├── Floor.java │ ├── LargestAndSmallestInArray.java │ ├── MaxAndMin.java │ ├── MaxSubarraySum.java │ ├── Pairs.java │ ├── PeakIndexInMountainArray.java │ ├── PrintSubarrays.java │ ├── RainwaterTrapping.java │ ├── ReverseArray.java │ ├── SearchInInfiniteArray.java │ ├── SearchInMountainArray.java │ ├── SearchInRotated.java │ ├── SearchInRotatedWIthDuplicates.java │ ├── SmallestLetterGreaterThanTarget.java │ ├── Solution.java │ ├── SplitArrayLargestSum.java │ └── StockBuySell.java ├── 7. OOPS ├── .idea │ ├── .gitignore │ ├── .name │ ├── misc.xml │ ├── modules.xml │ └── runConfigurations.xml ├── OOPS.iml ├── out │ └── production │ │ └── OOPS │ │ ├── Aneemal.class │ │ ├── Animal.class │ │ ├── Animals.class │ │ ├── Animilia.class │ │ ├── Bear.class │ │ ├── Birds.class │ │ ├── Bishop.class │ │ ├── BlueCar.class │ │ ├── Calculator.class │ │ ├── Car.class │ │ ├── Carnivore.class │ │ ├── ChessPiece.class │ │ ├── Complex.class │ │ ├── Constructors.class │ │ ├── Deer.class │ │ ├── Dog.class │ │ ├── ExceptionHandling.class │ │ ├── Fish.class │ │ ├── Fishes.class │ │ ├── Herbivore.class │ │ ├── HierachialInheritance.class │ │ ├── Horse.class │ │ ├── InterfaceInJava.class │ │ ├── King.class │ │ ├── Knight.class │ │ ├── Main.class │ │ ├── Mammal.class │ │ ├── Mammals.class │ │ ├── Mane.class │ │ ├── MultiLevelInheritance.class │ │ ├── MultipleInheritanceThroughInterfaces.class │ │ ├── OOPS.class │ │ ├── Pawn.class │ │ ├── Pen.class │ │ ├── Polymorphism.class │ │ ├── Queen.class │ │ ├── Rook.class │ │ ├── SingleLevelInheritance.class │ │ ├── Student.class │ │ └── SuperKeywordImplementation.class └── src │ ├── BlueCar.java │ ├── Constructors.java │ ├── ExceptionHandling.java │ ├── HierachialInheritance.java │ ├── InterfaceInJava.java │ ├── Main.java │ ├── Mane.java │ ├── MultiLevelInheritance.java │ ├── MultipleInheritanceThroughInterfaces.java │ ├── OOPS.java │ ├── Polymorphism.java │ ├── SingleLevelInheritance.java │ └── SuperKeywordImplementation.java └── README.md /1. Variables and Data Types/.idea/.gitignore: -------------------------------------------------------------------------------- 1 | # Default ignored files 2 | /shelf/ 3 | /workspace.xml 4 | -------------------------------------------------------------------------------- /1. Variables and Data Types/.idea/.name: -------------------------------------------------------------------------------- 1 | Variables and Data Types -------------------------------------------------------------------------------- /1. Variables and Data Types/.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /1. Variables and Data Types/.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /1. Variables and Data Types/.idea/runConfigurations.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 9 | 10 | -------------------------------------------------------------------------------- /1. Variables and Data Types/Variables and Data Types.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /1. Variables and Data Types/out/production/Variables and Data Types/AreaOfSquare.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashprasad234/Java-Basics/411547f5c993e1a544b702ece01747639a7bb96e/1. Variables and Data Types/out/production/Variables and Data Types/AreaOfSquare.class -------------------------------------------------------------------------------- /1. Variables and Data Types/out/production/Variables and Data Types/Bill.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashprasad234/Java-Basics/411547f5c993e1a544b702ece01747639a7bb96e/1. Variables and Data Types/out/production/Variables and Data Types/Bill.class -------------------------------------------------------------------------------- /1. Variables and Data Types/out/production/Variables and Data Types/ThreeNumbersAvg.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashprasad234/Java-Basics/411547f5c993e1a544b702ece01747639a7bb96e/1. Variables and Data Types/out/production/Variables and Data Types/ThreeNumbersAvg.class -------------------------------------------------------------------------------- /1. Variables and Data Types/src/AreaOfSquare.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class AreaOfSquare { 4 | public static void main(String[] args) { 5 | Scanner sc = new Scanner(System.in); 6 | System.out.print("Enter the side of the square: "); 7 | int a = sc.nextInt(); 8 | System.out.println("Area of the square with the side " + a + " is : " + a*a); 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /1. Variables and Data Types/src/Bill.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class Bill { 4 | public static void main(String[] args) { 5 | Scanner sc = new Scanner(System.in); 6 | System.out.print("Enter the price of pencil : "); 7 | float pencil = sc.nextFloat(); 8 | System.out.print("Enter the price of pen : "); 9 | float pen = sc.nextFloat(); 10 | System.out.print("Enter the price of eraser : "); 11 | float eraser = sc.nextFloat(); 12 | float gst = (float) ((pen + pencil + eraser)*0.18); 13 | System.out.println(); 14 | System.out.println("Bill"); 15 | System.out.println("Pencil : " + pencil); 16 | System.out.println("Pen : " + pen); 17 | System.out.println("Eraser : " + eraser); 18 | System.out.println("GST: " + gst); 19 | float total = pencil+pen+eraser+gst; 20 | System.out.println("Total : " + total); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /1. Variables and Data Types/src/ThreeNumbersAvg.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class ThreeNumbersAvg { 4 | public static void main(String[] args) { 5 | Scanner sc = new Scanner(System.in); 6 | int num1 = sc.nextInt(); 7 | int num2 = sc.nextInt(); 8 | int num3 = sc.nextInt(); 9 | int avg = (num1+num2+num3)/3; 10 | System.out.println("The average of the above 3 numbers is : " +avg); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /2. Conditionals/.idea/.gitignore: -------------------------------------------------------------------------------- 1 | # Default ignored files 2 | /shelf/ 3 | /workspace.xml 4 | -------------------------------------------------------------------------------- /2. Conditionals/.idea/.name: -------------------------------------------------------------------------------- 1 | Conditionals -------------------------------------------------------------------------------- /2. Conditionals/.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /2. Conditionals/.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /2. Conditionals/.idea/runConfigurations.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 9 | 10 | -------------------------------------------------------------------------------- /2. Conditionals/Conditionals.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /2. Conditionals/out/production/Conditionals/Feverish.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashprasad234/Java-Basics/411547f5c993e1a544b702ece01747639a7bb96e/2. Conditionals/out/production/Conditionals/Feverish.class -------------------------------------------------------------------------------- /2. Conditionals/out/production/Conditionals/LeapYear.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashprasad234/Java-Basics/411547f5c993e1a544b702ece01747639a7bb96e/2. Conditionals/out/production/Conditionals/LeapYear.class -------------------------------------------------------------------------------- /2. Conditionals/out/production/Conditionals/PosOrNeg.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashprasad234/Java-Basics/411547f5c993e1a544b702ece01747639a7bb96e/2. Conditionals/out/production/Conditionals/PosOrNeg.class -------------------------------------------------------------------------------- /2. Conditionals/out/production/Conditionals/Weekday.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashprasad234/Java-Basics/411547f5c993e1a544b702ece01747639a7bb96e/2. Conditionals/out/production/Conditionals/Weekday.class -------------------------------------------------------------------------------- /2. Conditionals/src/Feverish.java: -------------------------------------------------------------------------------- 1 | public class Feverish { 2 | public static void main(String[] args) { 3 | double temp = 103.5; 4 | if(temp > 100) 5 | System.out.println("You have fever"); 6 | else 7 | System.out.println("You don't have fever"); 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /2. Conditionals/src/LeapYear.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class LeapYear { 4 | public static void main(String[] args) { 5 | Scanner in = new Scanner(System.in); 6 | System.out.print("Enter year : "); 7 | int year = in.nextInt(); 8 | if(year%4 == 0) { 9 | System.out.println(year + " is a leap year"); 10 | } 11 | else if(year%400 == 0) 12 | System.out.println(year + " is a leap year"); 13 | else 14 | System.out.println(year + " is not a leap year"); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /2. Conditionals/src/PosOrNeg.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class PosOrNeg { 4 | public static void main(String[] args) { 5 | Scanner in = new Scanner(System.in); 6 | int num = in.nextInt(); 7 | if(num < 0) 8 | System.out.println("The number you entered is negative"); 9 | else 10 | System.out.println("The number you entered is positive"); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /2. Conditionals/src/Weekday.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class Weekday { 4 | public static void main(String[] args) { 5 | Scanner in = new Scanner(System.in); 6 | System.out.println("Enter a number from 1-7"); 7 | int num = in.nextInt(); 8 | switch (num) { 9 | case 1: 10 | System.out.println("Monday"); 11 | break; 12 | case 2: 13 | System.out.println("Tuesday"); 14 | break; 15 | case 3: 16 | System.out.println("Wednesday"); 17 | break; 18 | case 4: 19 | System.out.println("Thursday"); 20 | break; 21 | case 5: 22 | System.out.println("Friday"); 23 | break; 24 | case 6: 25 | System.out.println("Saturday"); 26 | break; 27 | case 7: 28 | System.out.println("Sunday"); 29 | break; 30 | default: 31 | System.out.println("There are only 7 days in a week"); 32 | } 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /3. Loops/.idea/.gitignore: -------------------------------------------------------------------------------- 1 | # Default ignored files 2 | /shelf/ 3 | /workspace.xml 4 | -------------------------------------------------------------------------------- /3. Loops/.idea/.name: -------------------------------------------------------------------------------- 1 | Loops -------------------------------------------------------------------------------- /3. Loops/.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /3. Loops/.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /3. Loops/.idea/runConfigurations.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 9 | 10 | -------------------------------------------------------------------------------- /3. Loops/Loops.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /3. Loops/out/production/Loops/Factorial.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashprasad234/Java-Basics/411547f5c993e1a544b702ece01747639a7bb96e/3. Loops/out/production/Loops/Factorial.class -------------------------------------------------------------------------------- /3. Loops/out/production/Loops/SumEvenSumOdd.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashprasad234/Java-Basics/411547f5c993e1a544b702ece01747639a7bb96e/3. Loops/out/production/Loops/SumEvenSumOdd.class -------------------------------------------------------------------------------- /3. Loops/out/production/Loops/Table.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashprasad234/Java-Basics/411547f5c993e1a544b702ece01747639a7bb96e/3. Loops/out/production/Loops/Table.class -------------------------------------------------------------------------------- /3. Loops/src/Factorial.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class Factorial { 4 | public static void main(String[] args) { 5 | Scanner in = new Scanner (System.in); 6 | System.out.print("Enter a number : "); 7 | int n = in.nextInt(); 8 | int p = 1; 9 | for (int i = 1; i <= n; i++) { 10 | p *= i; 11 | } 12 | System.out.println("The factorial of " + n + " is : " + p); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /3. Loops/src/SumEvenSumOdd.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class SumEvenSumOdd { 4 | public static void main(String[] args) { 5 | Scanner in = new Scanner(System.in); 6 | int sum1 = 0, sum2 = 0; 7 | System.out.print("How many integers do you want to input : "); 8 | int i = in.nextInt(); 9 | System.out.print("Input " + i + " integers : "); 10 | for(int j = 0; j < i; j++) { 11 | int n = in.nextInt(); 12 | if(n % 2 == 0) 13 | sum1 += n; 14 | else 15 | sum2 += n; 16 | } 17 | System.out.println("The sum of all the odd integers inputed is : " + sum2); 18 | System.out.println("The sum of all the even integers inputed is : " + sum1); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /3. Loops/src/Table.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class Table { 4 | public static void main(String[] args) { 5 | Scanner in = new Scanner(System.in); 6 | System.out.print("Enter the number for which you want the multiplication table to be displayed : "); 7 | int n = in.nextInt(); 8 | for (int i = 1; i <= 10; i++) { 9 | System.out.println(n + " * " + i + " = " + n*i); 10 | } 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /4. Pattern/.idea/.gitignore: -------------------------------------------------------------------------------- 1 | # Default ignored files 2 | /shelf/ 3 | /workspace.xml 4 | -------------------------------------------------------------------------------- /4. Pattern/.idea/.name: -------------------------------------------------------------------------------- 1 | Pattern -------------------------------------------------------------------------------- /4. Pattern/.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /4. Pattern/.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /4. Pattern/.idea/runConfigurations.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 9 | 10 | -------------------------------------------------------------------------------- /4. Pattern/.idea/uiDesigner.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | -------------------------------------------------------------------------------- /4. Pattern/4. Pattern.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /4. Pattern/out/production/4. Pattern/BinaryHalfPyramid.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashprasad234/Java-Basics/411547f5c993e1a544b702ece01747639a7bb96e/4. Pattern/out/production/4. Pattern/BinaryHalfPyramid.class -------------------------------------------------------------------------------- /4. Pattern/out/production/4. Pattern/ButterflyPattern.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashprasad234/Java-Basics/411547f5c993e1a544b702ece01747639a7bb96e/4. Pattern/out/production/4. Pattern/ButterflyPattern.class -------------------------------------------------------------------------------- /4. Pattern/out/production/4. Pattern/Diamond.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashprasad234/Java-Basics/411547f5c993e1a544b702ece01747639a7bb96e/4. Pattern/out/production/4. Pattern/Diamond.class -------------------------------------------------------------------------------- /4. Pattern/out/production/4. Pattern/FloydsTriangle.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashprasad234/Java-Basics/411547f5c993e1a544b702ece01747639a7bb96e/4. Pattern/out/production/4. Pattern/FloydsTriangle.class -------------------------------------------------------------------------------- /4. Pattern/out/production/4. Pattern/HalfPyramid.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashprasad234/Java-Basics/411547f5c993e1a544b702ece01747639a7bb96e/4. Pattern/out/production/4. Pattern/HalfPyramid.class -------------------------------------------------------------------------------- /4. Pattern/out/production/4. Pattern/HalfPyramidAlphabets.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashprasad234/Java-Basics/411547f5c993e1a544b702ece01747639a7bb96e/4. Pattern/out/production/4. Pattern/HalfPyramidAlphabets.class -------------------------------------------------------------------------------- /4. Pattern/out/production/4. Pattern/HalfPyramidNum.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashprasad234/Java-Basics/411547f5c993e1a544b702ece01747639a7bb96e/4. Pattern/out/production/4. Pattern/HalfPyramidNum.class -------------------------------------------------------------------------------- /4. Pattern/out/production/4. Pattern/HollowRectangle.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashprasad234/Java-Basics/411547f5c993e1a544b702ece01747639a7bb96e/4. Pattern/out/production/4. Pattern/HollowRectangle.class -------------------------------------------------------------------------------- /4. Pattern/out/production/4. Pattern/HollowRhombus.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashprasad234/Java-Basics/411547f5c993e1a544b702ece01747639a7bb96e/4. Pattern/out/production/4. Pattern/HollowRhombus.class -------------------------------------------------------------------------------- /4. Pattern/out/production/4. Pattern/InvertedHalfPyramid.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashprasad234/Java-Basics/411547f5c993e1a544b702ece01747639a7bb96e/4. Pattern/out/production/4. Pattern/InvertedHalfPyramid.class -------------------------------------------------------------------------------- /4. Pattern/out/production/4. Pattern/InvertedHalfPyramidNum.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashprasad234/Java-Basics/411547f5c993e1a544b702ece01747639a7bb96e/4. Pattern/out/production/4. Pattern/InvertedHalfPyramidNum.class -------------------------------------------------------------------------------- /4. Pattern/out/production/4. Pattern/NumberPyramid.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashprasad234/Java-Basics/411547f5c993e1a544b702ece01747639a7bb96e/4. Pattern/out/production/4. Pattern/NumberPyramid.class -------------------------------------------------------------------------------- /4. Pattern/out/production/4. Pattern/Rhombus.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashprasad234/Java-Basics/411547f5c993e1a544b702ece01747639a7bb96e/4. Pattern/out/production/4. Pattern/Rhombus.class -------------------------------------------------------------------------------- /4. Pattern/out/production/4. Pattern/RotatedHalfPyramid.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashprasad234/Java-Basics/411547f5c993e1a544b702ece01747639a7bb96e/4. Pattern/out/production/4. Pattern/RotatedHalfPyramid.class -------------------------------------------------------------------------------- /4. Pattern/src/BinaryHalfPyramid.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class BinaryHalfPyramid { 4 | public static void main(String[] args) { 5 | Scanner sc = new Scanner(System.in); 6 | System.out.print("Enter the height of pyramid : "); 7 | int n = sc.nextInt(); 8 | for(int i = 0; i < n; i++) { 9 | for(int j = 0; j <= i; j++) { 10 | if((i+j) % 2 == 0) 11 | System.out.print("1 "); 12 | else 13 | System.out.print("0 "); 14 | } 15 | System.out.println(); 16 | } 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /4. Pattern/src/ButterflyPattern.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class ButterflyPattern { 4 | public static void main(String[] args) { 5 | Scanner sc = new Scanner(System.in); 6 | System.out.print("Enter a number : "); 7 | int n = sc.nextInt(); 8 | for (int i = 1; i <= n; i++) { 9 | for (int j = 1; j < 2*n; j++) { 10 | if(j > i && j < 2*n-i) { 11 | System.out.print(" "); 12 | }else { 13 | System.out.print("* "); 14 | } 15 | } 16 | System.out.println(); 17 | } 18 | for (int i = n; i >= 1; i--) { 19 | for (int j = 1; j < 2*n; j++) { 20 | if(j > i && j < 2*n-i) { 21 | System.out.print(" "); 22 | }else { 23 | System.out.print("* "); 24 | } 25 | } 26 | System.out.println(); 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /4. Pattern/src/Diamond.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class Diamond { 4 | public static void main(String[] args) { 5 | Scanner sc = new Scanner(System.in); 6 | System.out.print("Enter the side of diamond : "); 7 | int n = sc.nextInt(); 8 | for (int i = 0; i < n; i++) { 9 | for (int j = 0; j < n+i; j++) { 10 | if(j < n-i-1) 11 | System.out.print(" "); 12 | else 13 | System.out.print("* "); 14 | } 15 | System.out.println(); 16 | } 17 | for (int i = n-1; i >= 0; i--) { 18 | for (int j = 0; j < n+i; j++) { 19 | if(j < n-i-1) 20 | System.out.print(" "); 21 | else 22 | System.out.print("* "); 23 | } 24 | System.out.println(); 25 | } 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /4. Pattern/src/FloydsTriangle.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class FloydsTriangle { 4 | public static void main(String[] args) { 5 | Scanner sc = new Scanner(System.in); 6 | System.out.print("Enter the height of Floyd's triangle : "); 7 | int n = sc.nextInt(); 8 | int a = 1; 9 | for(int i = 0; i < n; i++) { 10 | for(int j = 0; j <= i; j++) { 11 | System.out.print(a + " "); 12 | a++; 13 | } 14 | System.out.println(); 15 | } 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /4. Pattern/src/HalfPyramid.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class HalfPyramid { 4 | public static void main(String[] args) { 5 | Scanner sc = new Scanner(System.in); 6 | System.out.print("Enter any number : "); 7 | int n = sc.nextInt(); 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 | -------------------------------------------------------------------------------- /4. Pattern/src/HalfPyramidAlphabets.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class HalfPyramidAlphabets { 4 | public static void main(String[] args) { 5 | Scanner sc = new Scanner(System.in); 6 | System.out.print("Enter the height of pyramid : "); 7 | int n = sc.nextInt(); 8 | char ch = 'A'; 9 | for (int i = 0; i < n; i++) { 10 | for (int j = 0; j < i+1; j++) { 11 | System.out.print(ch + " "); 12 | ch++; 13 | } 14 | System.out.println(); 15 | } 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /4. Pattern/src/HalfPyramidNum.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class HalfPyramidNum { 4 | public static void main(String[] args) { 5 | Scanner in = new Scanner(System.in); 6 | System.out.print("Enter any number : "); 7 | int n = in.nextInt(); 8 | for (int i = 0; i < n; i++) { 9 | for (int j = 1; j <= i+1; j++) { 10 | System.out.print(j + " "); 11 | } 12 | System.out.println(); 13 | } 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /4. Pattern/src/HollowRectangle.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class HollowRectangle { 4 | public static void main(String[] args) { 5 | Scanner sc = new Scanner(System.in); 6 | System.out.print("Enter the breadth of rectangle : "); 7 | int m = sc.nextInt(); 8 | System.out.print("Enter the length of rectangle : "); 9 | int n = sc.nextInt(); 10 | for (int i = 0; i < m; i++) { 11 | for (int j = 0; j < n; j++) { 12 | if(i == 0 || i == m-1) { 13 | System.out.print("* "); 14 | } 15 | else if(j== 0 || j == n-1) { 16 | System.out.print("* "); 17 | } 18 | else 19 | System.out.print(" "); 20 | } 21 | System.out.println(); 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /4. Pattern/src/HollowRhombus.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class HollowRhombus { 4 | public static void main(String[] args) { 5 | Scanner sc = new Scanner(System.in); 6 | System.out.print("Enter the side length of rhombus : "); 7 | int n = sc.nextInt(); 8 | for (int i = 0; i < n; i++) { 9 | for (int j = 0; j <= 2*n-1; j++) { 10 | if(j < n-i || j > 2*n-i-1) 11 | System.out.print(" "); 12 | else { 13 | if(i > 0 && i < n-1){ 14 | if(j > n-i && j < 2 * n - i - 1) 15 | System.out.print(" "); 16 | else 17 | System.out.print("* "); 18 | }else { 19 | System.out.print("* "); 20 | } 21 | } 22 | } 23 | System.out.println(); 24 | } 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /4. Pattern/src/InvertedHalfPyramid.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class InvertedHalfPyramid { 4 | public static void main(String[] args) { 5 | Scanner in = new Scanner(System.in); 6 | System.out.print("Enter any number : "); 7 | int n = in.nextInt(); 8 | for (int i = 0; i < n; i++) { 9 | for (int j = n-1; j >= i; j--) { 10 | System.out.print("* "); 11 | } 12 | System.out.println(); 13 | } 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /4. Pattern/src/InvertedHalfPyramidNum.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class InvertedHalfPyramidNum { 4 | public static void main(String[] args) { 5 | Scanner sc = new Scanner(System.in); 6 | System.out.print("Enter the height of pyramid : "); 7 | int n = sc.nextInt(); 8 | for (int i = 0; i < n; i++) { 9 | for (int j = 1; j < n-i+1; j++) { 10 | System.out.print(j + " "); 11 | } 12 | System.out.println(); 13 | } 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /4. Pattern/src/NumberPyramid.java: -------------------------------------------------------------------------------- 1 | public class NumberPyramid { 2 | public static void main(String[] args) { 3 | int n = 5; 4 | for (int i = 0; i < n; i++) { 5 | for(int j = 0; j < n-i; j++) { 6 | System.out.print(" "); 7 | } 8 | for(int k = 0; k <= i; k++) { 9 | System.out.print(i+1 + " "); 10 | } 11 | System.out.println(); 12 | } 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /4. Pattern/src/Rhombus.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class Rhombus { 4 | public static void main(String[] args) { 5 | Scanner sc = new Scanner(System.in); 6 | System.out.print("Enter the side length of rhombus : "); 7 | int n = sc.nextInt(); 8 | for(int i = 0; i < n; i++) { 9 | for(int j = 0; j < 2*n; j++) { 10 | if(j < n-i || j > 2*n - i -1) 11 | System.out.print(" "); 12 | else 13 | System.out.print("* "); 14 | } 15 | System.out.println(); 16 | } 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /4. Pattern/src/RotatedHalfPyramid.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class RotatedHalfPyramid { 4 | public static void main(String[] args) { 5 | Scanner sc = new Scanner(System.in); 6 | System.out.print("Enter the height of pyramid : "); 7 | int n = sc.nextInt(); 8 | for (int i = 0; i < n; i++) { 9 | for (int j = n-1; j >= 0; j--) { 10 | if(j > i) 11 | System.out.print(" "); 12 | else 13 | System.out.print("* "); 14 | } 15 | System.out.println(); 16 | } 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /5. Functions and Methods/.idea/.gitignore: -------------------------------------------------------------------------------- 1 | # Default ignored files 2 | /shelf/ 3 | /workspace.xml 4 | -------------------------------------------------------------------------------- /5. Functions and Methods/.idea/.name: -------------------------------------------------------------------------------- 1 | Functions and Methods -------------------------------------------------------------------------------- /5. Functions and Methods/.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /5. Functions and Methods/.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /5. Functions and Methods/.idea/runConfigurations.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 9 | 10 | -------------------------------------------------------------------------------- /5. Functions and Methods/.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /5. Functions and Methods/Functions and Methods.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /5. Functions and Methods/out/production/Functions and Methods/AverageOfThreeNums.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashprasad234/Java-Basics/411547f5c993e1a544b702ece01747639a7bb96e/5. Functions and Methods/out/production/Functions and Methods/AverageOfThreeNums.class -------------------------------------------------------------------------------- /5. Functions and Methods/out/production/Functions and Methods/BinaryToDecimal.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashprasad234/Java-Basics/411547f5c993e1a544b702ece01747639a7bb96e/5. Functions and Methods/out/production/Functions and Methods/BinaryToDecimal.class -------------------------------------------------------------------------------- /5. Functions and Methods/out/production/Functions and Methods/BinomialCoefficient.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashprasad234/Java-Basics/411547f5c993e1a544b702ece01747639a7bb96e/5. Functions and Methods/out/production/Functions and Methods/BinomialCoefficient.class -------------------------------------------------------------------------------- /5. Functions and Methods/out/production/Functions and Methods/CheckEven.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashprasad234/Java-Basics/411547f5c993e1a544b702ece01747639a7bb96e/5. Functions and Methods/out/production/Functions and Methods/CheckEven.class -------------------------------------------------------------------------------- /5. Functions and Methods/out/production/Functions and Methods/DecimalToBinary.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashprasad234/Java-Basics/411547f5c993e1a544b702ece01747639a7bb96e/5. Functions and Methods/out/production/Functions and Methods/DecimalToBinary.class -------------------------------------------------------------------------------- /5. Functions and Methods/out/production/Functions and Methods/Palindrome.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashprasad234/Java-Basics/411547f5c993e1a544b702ece01747639a7bb96e/5. Functions and Methods/out/production/Functions and Methods/Palindrome.class -------------------------------------------------------------------------------- /5. Functions and Methods/out/production/Functions and Methods/PrimeOrNot.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashprasad234/Java-Basics/411547f5c993e1a544b702ece01747639a7bb96e/5. Functions and Methods/out/production/Functions and Methods/PrimeOrNot.class -------------------------------------------------------------------------------- /5. Functions and Methods/out/production/Functions and Methods/PrimesInRange.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashprasad234/Java-Basics/411547f5c993e1a544b702ece01747639a7bb96e/5. Functions and Methods/out/production/Functions and Methods/PrimesInRange.class -------------------------------------------------------------------------------- /5. Functions and Methods/out/production/Functions and Methods/ProductOfAandB.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashprasad234/Java-Basics/411547f5c993e1a544b702ece01747639a7bb96e/5. Functions and Methods/out/production/Functions and Methods/ProductOfAandB.class -------------------------------------------------------------------------------- /5. Functions and Methods/out/production/Functions and Methods/Solution.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashprasad234/Java-Basics/411547f5c993e1a544b702ece01747639a7bb96e/5. Functions and Methods/out/production/Functions and Methods/Solution.class -------------------------------------------------------------------------------- /5. Functions and Methods/out/production/Functions and Methods/SumOfDigits.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashprasad234/Java-Basics/411547f5c993e1a544b702ece01747639a7bb96e/5. Functions and Methods/out/production/Functions and Methods/SumOfDigits.class -------------------------------------------------------------------------------- /5. Functions and Methods/src/AverageOfThreeNums.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class AverageOfThreeNums { 4 | public static void main(String[] args) { 5 | Scanner sc = new Scanner(System.in); 6 | System.out.println("Enter three numbers : "); 7 | int a = sc.nextInt(); 8 | int b = sc.nextInt(); 9 | int c = sc.nextInt(); 10 | System.out.println("The average of the three numbers is : " + average(a, b, c)); 11 | } 12 | 13 | static float average(int a, int b, int c) { 14 | int sum = a + b + c; 15 | return (float) sum / 3; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /5. Functions and Methods/src/BinaryToDecimal.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class BinaryToDecimal { 4 | public static void main(String[] args) { 5 | Scanner sc = new Scanner(System.in); 6 | System.out.print("Enter a binary Number : "); 7 | int num = sc.nextInt(); 8 | System.out.println("The decimal equivalent of this binary number is : " + binaryToDecimal(num)); 9 | } 10 | 11 | static int binaryToDecimal(int a) { 12 | int ans = 0, rem = 0, k = 1; 13 | while(a > 0) { 14 | rem = a % 10; 15 | ans += rem * k; 16 | a /= 10; 17 | k *= 2; 18 | } 19 | return ans; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /5. Functions and Methods/src/BinomialCoefficient.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class BinomialCoefficient { 4 | public static void main(String[] args) { 5 | Scanner sc = new Scanner(System.in); 6 | System.out.print("Enter n : "); 7 | int n = sc.nextInt(); 8 | System.out.print("Enter r : "); 9 | int r = sc.nextInt(); 10 | System.out.println("The binomial coefficient of n,r is : " + binomialCoeff(n,r)); 11 | } 12 | 13 | static int binomialCoeff(int n, int r) { 14 | int ans = fact(n)/fact(r); 15 | ans /= fact(n-r); 16 | return ans; 17 | } 18 | 19 | static int fact(int a) { 20 | int res = 1; 21 | for(int i = 1; i <= a; i++) { 22 | res *= i; 23 | } 24 | return res; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /5. Functions and Methods/src/CheckEven.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class CheckEven { 4 | public static void main(String[] args) { 5 | Scanner sc = new Scanner(System.in); 6 | System.out.print("Enter a number : "); 7 | int num = sc.nextInt(); 8 | if(isEven(num)) 9 | System.out.println(num + " is Even"); 10 | else 11 | System.out.println(num + " is Odd"); 12 | } 13 | 14 | static boolean isEven(int a) { 15 | if(a % 2 == 0) 16 | return true; 17 | return false; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /5. Functions and Methods/src/DecimalToBinary.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class DecimalToBinary { 4 | public static void main(String[] args) { 5 | Scanner sc = new Scanner(System.in); 6 | System.out.print("Enter a decimal number : "); 7 | int num = sc.nextInt(); 8 | System.out.println("The binary equivalent of " + num + " is : " + decimalToBinary(num)); 9 | } 10 | 11 | static int decimalToBinary(int a) { 12 | int ans = 0, rem = 0; 13 | while(a > 0) { 14 | rem = a % 2; 15 | ans = ans * 10 + rem; 16 | a /= 2; 17 | } 18 | return reverse(ans); 19 | } 20 | 21 | static int reverse(int a) { 22 | int rem = 0, ans = 0; 23 | while(a > 0) { 24 | rem = a % 10; 25 | ans = ans * 10 + rem; 26 | a /= 10; 27 | } 28 | return ans; 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /5. Functions and Methods/src/Palindrome.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class Palindrome { 4 | public static void main(String[] args) { 5 | Scanner sc = new Scanner(System.in); 6 | System.out.print("Enter a number : "); 7 | int num = sc.nextInt(); 8 | // int num = 121; 9 | if(isPalindrome(num)) 10 | System.out.println(num + " is a palindrome"); 11 | else 12 | System.out.println(num + " is not a palindrome"); 13 | } 14 | 15 | static boolean isPalindrome (int a) { 16 | int res = 0, rem = 0; 17 | int orig = a; 18 | while(a > 0) { 19 | rem = a % 10; 20 | res = res * 10 + rem; 21 | a /= 10; 22 | } 23 | if(res == orig) 24 | return true; 25 | else 26 | return false; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /5. Functions and Methods/src/PrimeOrNot.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class PrimeOrNot { 4 | public static void main(String[] args) { 5 | Scanner sc = new Scanner(System.in); 6 | System.out.print("Enter a number : "); 7 | int a = sc.nextInt(); 8 | if(isPrime(a)) { 9 | System.out.println(a + " is a prime"); 10 | } else 11 | System.out.println(a + " is not a prime"); 12 | } 13 | 14 | static boolean isPrime(int a) { 15 | if(a < 2) 16 | return false; 17 | for(int i = 2; i <= Math.sqrt(a); i++) { 18 | if(a%i == 0) 19 | return false; 20 | } 21 | return true; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /5. Functions and Methods/src/PrimesInRange.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class PrimesInRange { 4 | public static void main(String[] args) { 5 | Scanner sc = new Scanner(System.in); 6 | System.out.print("Enter the first number : "); 7 | int a = sc.nextInt(); 8 | System.out.print("Enter the second number : "); 9 | int b = sc.nextInt(); 10 | for (int i = a; i <= b; i++) { 11 | if(isPrime(i)) 12 | System.out.print(i + " "); 13 | } 14 | } 15 | 16 | static boolean isPrime(int a) { 17 | if(a < 2) 18 | return false; 19 | for (int i = 2; i <= Math.sqrt(a); i++) { 20 | if(a % i == 0) 21 | return false; 22 | } 23 | return true; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /5. Functions and Methods/src/ProductOfAandB.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class ProductOfAandB { 4 | public static void main(String[] args) { 5 | Scanner sc = new Scanner (System.in); 6 | int a = sc.nextInt(); 7 | int b = sc.nextInt(); 8 | System.out.println("Product of " + a + ", " + b + " is : " + product(a, b)); 9 | } 10 | 11 | static int product (int a, int b) { 12 | return a*b; 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /5. Functions and Methods/src/Solution.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class Solution { 4 | public static void main(String[] args) { 5 | Scanner sc = new Scanner(System.in); 6 | System.out.print("Enter the first number : "); 7 | int a = sc.nextInt(); 8 | System.out.print("Enter the second number : "); 9 | int b = sc.nextInt(); 10 | for (int i = a; i <= b; i++) { 11 | if(isPrime(i)) 12 | System.out.print(i + " "); 13 | } 14 | } 15 | 16 | static boolean isPrime(int a) { 17 | if(a < 2) 18 | return false; 19 | for (int i = 2; i <= Math.sqrt(a); i++) { 20 | if(a % i == 0) 21 | return false; 22 | } 23 | return true; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /5. Functions and Methods/src/SumOfDigits.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class SumOfDigits { 4 | public static void main(String[] args) { 5 | Scanner sc = new Scanner(System.in); 6 | System.out.print("Enter a number : "); 7 | int num = sc.nextInt(); 8 | System.out.println("The sum of the digits of " + num + " is : " + sumOfDigits(num)); 9 | } 10 | 11 | static int sumOfDigits (int a) { 12 | int sum = 0, rem = 0; 13 | while(a > 0) { 14 | rem = a % 10; 15 | sum += rem; 16 | a /= 10; 17 | } 18 | return sum; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /6. Arrays/.idea/.gitignore: -------------------------------------------------------------------------------- 1 | # Default ignored files 2 | /shelf/ 3 | /workspace.xml 4 | -------------------------------------------------------------------------------- /6. Arrays/.idea/.name: -------------------------------------------------------------------------------- 1 | Arrays -------------------------------------------------------------------------------- /6. Arrays/.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /6. Arrays/.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /6. Arrays/.idea/runConfigurations.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 9 | 10 | -------------------------------------------------------------------------------- /6. Arrays/.idea/uiDesigner.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | -------------------------------------------------------------------------------- /6. Arrays/.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /6. Arrays/Arrays.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /6. Arrays/out/production/Arrays/BinarySearch.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashprasad234/Java-Basics/411547f5c993e1a544b702ece01747639a7bb96e/6. Arrays/out/production/Arrays/BinarySearch.class -------------------------------------------------------------------------------- /6. Arrays/out/production/Arrays/Ceiling.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashprasad234/Java-Basics/411547f5c993e1a544b702ece01747639a7bb96e/6. Arrays/out/production/Arrays/Ceiling.class -------------------------------------------------------------------------------- /6. Arrays/out/production/Arrays/CheckForDuplicate.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashprasad234/Java-Basics/411547f5c993e1a544b702ece01747639a7bb96e/6. Arrays/out/production/Arrays/CheckForDuplicate.class -------------------------------------------------------------------------------- /6. Arrays/out/production/Arrays/ChocolateDistribution.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashprasad234/Java-Basics/411547f5c993e1a544b702ece01747639a7bb96e/6. Arrays/out/production/Arrays/ChocolateDistribution.class -------------------------------------------------------------------------------- /6. Arrays/out/production/Arrays/FindPivot.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashprasad234/Java-Basics/411547f5c993e1a544b702ece01747639a7bb96e/6. Arrays/out/production/Arrays/FindPivot.class -------------------------------------------------------------------------------- /6. Arrays/out/production/Arrays/FirstAndLastPosition.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashprasad234/Java-Basics/411547f5c993e1a544b702ece01747639a7bb96e/6. Arrays/out/production/Arrays/FirstAndLastPosition.class -------------------------------------------------------------------------------- /6. Arrays/out/production/Arrays/Floor.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashprasad234/Java-Basics/411547f5c993e1a544b702ece01747639a7bb96e/6. Arrays/out/production/Arrays/Floor.class -------------------------------------------------------------------------------- /6. Arrays/out/production/Arrays/LargestAndSmallestInArray.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashprasad234/Java-Basics/411547f5c993e1a544b702ece01747639a7bb96e/6. Arrays/out/production/Arrays/LargestAndSmallestInArray.class -------------------------------------------------------------------------------- /6. Arrays/out/production/Arrays/MaxAndMin.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashprasad234/Java-Basics/411547f5c993e1a544b702ece01747639a7bb96e/6. Arrays/out/production/Arrays/MaxAndMin.class -------------------------------------------------------------------------------- /6. Arrays/out/production/Arrays/MaxSubarraySum.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashprasad234/Java-Basics/411547f5c993e1a544b702ece01747639a7bb96e/6. Arrays/out/production/Arrays/MaxSubarraySum.class -------------------------------------------------------------------------------- /6. Arrays/out/production/Arrays/Pairs.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashprasad234/Java-Basics/411547f5c993e1a544b702ece01747639a7bb96e/6. Arrays/out/production/Arrays/Pairs.class -------------------------------------------------------------------------------- /6. Arrays/out/production/Arrays/PeakIndexInMountainArray.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashprasad234/Java-Basics/411547f5c993e1a544b702ece01747639a7bb96e/6. Arrays/out/production/Arrays/PeakIndexInMountainArray.class -------------------------------------------------------------------------------- /6. Arrays/out/production/Arrays/PrintSubarrays.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashprasad234/Java-Basics/411547f5c993e1a544b702ece01747639a7bb96e/6. Arrays/out/production/Arrays/PrintSubarrays.class -------------------------------------------------------------------------------- /6. Arrays/out/production/Arrays/RainwaterTrapping.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashprasad234/Java-Basics/411547f5c993e1a544b702ece01747639a7bb96e/6. Arrays/out/production/Arrays/RainwaterTrapping.class -------------------------------------------------------------------------------- /6. Arrays/out/production/Arrays/ReverseArray.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashprasad234/Java-Basics/411547f5c993e1a544b702ece01747639a7bb96e/6. Arrays/out/production/Arrays/ReverseArray.class -------------------------------------------------------------------------------- /6. Arrays/out/production/Arrays/SearchInInfiniteArray.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashprasad234/Java-Basics/411547f5c993e1a544b702ece01747639a7bb96e/6. Arrays/out/production/Arrays/SearchInInfiniteArray.class -------------------------------------------------------------------------------- /6. Arrays/out/production/Arrays/SearchInMountainArray.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashprasad234/Java-Basics/411547f5c993e1a544b702ece01747639a7bb96e/6. Arrays/out/production/Arrays/SearchInMountainArray.class -------------------------------------------------------------------------------- /6. Arrays/out/production/Arrays/SearchInRotated.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashprasad234/Java-Basics/411547f5c993e1a544b702ece01747639a7bb96e/6. Arrays/out/production/Arrays/SearchInRotated.class -------------------------------------------------------------------------------- /6. Arrays/out/production/Arrays/SearchInRotatedWIthDuplicates.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashprasad234/Java-Basics/411547f5c993e1a544b702ece01747639a7bb96e/6. Arrays/out/production/Arrays/SearchInRotatedWIthDuplicates.class -------------------------------------------------------------------------------- /6. Arrays/out/production/Arrays/SmallestLetterGreaterThanTarget.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashprasad234/Java-Basics/411547f5c993e1a544b702ece01747639a7bb96e/6. Arrays/out/production/Arrays/SmallestLetterGreaterThanTarget.class -------------------------------------------------------------------------------- /6. Arrays/out/production/Arrays/Solution.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashprasad234/Java-Basics/411547f5c993e1a544b702ece01747639a7bb96e/6. Arrays/out/production/Arrays/Solution.class -------------------------------------------------------------------------------- /6. Arrays/out/production/Arrays/SplitArrayLargestSum.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashprasad234/Java-Basics/411547f5c993e1a544b702ece01747639a7bb96e/6. Arrays/out/production/Arrays/SplitArrayLargestSum.class -------------------------------------------------------------------------------- /6. Arrays/out/production/Arrays/StockBuySell.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashprasad234/Java-Basics/411547f5c993e1a544b702ece01747639a7bb96e/6. Arrays/out/production/Arrays/StockBuySell.class -------------------------------------------------------------------------------- /6. Arrays/src/BinarySearch.java: -------------------------------------------------------------------------------- 1 | public class BinarySearch { 2 | public static void main(String[] args) { 3 | int[] arr = {2,4,6,8,10,12,14}; 4 | int target = 10; 5 | System.out.println("The target is at index " + search(arr, target)); 6 | } 7 | 8 | static int search(int[] arr, int target) { 9 | int st = 0, end = arr.length-1; 10 | while(st <= end) { 11 | int mid = st + (end - st) / 2; 12 | if(arr[mid] == target) 13 | return mid; 14 | else if(arr[mid] < target) 15 | st = mid + 1; 16 | else 17 | end = mid - 1; 18 | } 19 | return -1; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /6. Arrays/src/Ceiling.java: -------------------------------------------------------------------------------- 1 | public class Ceiling { 2 | public static void main(String[] args) { 3 | int[] arr = {2, 3, 5, 9, 14, 16, 18}; 4 | System.out.println(ceilingOfNum(arr, 19)); 5 | } 6 | 7 | static int ceilingOfNum (int[] arr, int target) { 8 | if(target > arr[arr.length-1]) 9 | return Integer.MAX_VALUE; 10 | int st = 0, end = arr.length-1; 11 | while (st <= end) { 12 | int mid = st + (end - st) / 2; 13 | if(arr[mid] == target) 14 | return arr[mid]; 15 | else if(arr[mid] > target) 16 | end = mid - 1; 17 | else 18 | st = mid + 1; 19 | } 20 | return arr[st]; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /6. Arrays/src/CheckForDuplicate.java: -------------------------------------------------------------------------------- 1 | import java.util.Arrays; 2 | 3 | public class CheckForDuplicate { 4 | public static void main(String[] args) { 5 | int[] arr = {1, 1, 1, 3, 3, 4, 3, 2, 4, 2}; 6 | System.out.println(hasDuplicate(arr)); 7 | } 8 | 9 | static boolean hasDuplicate(int[] arr) { 10 | Arrays.sort(arr); 11 | for (int i = 1; i < arr.length; i++) { 12 | if(arr[i] == arr[i-1]) 13 | return true; 14 | } 15 | return false; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /6. Arrays/src/ChocolateDistribution.java: -------------------------------------------------------------------------------- 1 | //https://www.geeksforgeeks.org/chocolate-distribution-problem/ 2 | 3 | import java.util.Arrays; 4 | 5 | public class ChocolateDistribution { 6 | public static void main(String[] args) { 7 | int[] arr = {3, 4, 1, 9, 56, 7, 9, 12}; 8 | System.out.println(findMinDiff(arr, 5)); 9 | } 10 | 11 | static int findMinDiff(int[] arr, int m) { 12 | if(m == 0 || arr.length == 0) 13 | return 0; 14 | Arrays.sort(arr); 15 | int ans = Integer.MAX_VALUE; 16 | for (int i = 0; i <= arr.length-m; i++) { 17 | ans = Math.min(arr[i+m-1] - arr[i], ans); 18 | } 19 | return ans; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /6. Arrays/src/FindPivot.java: -------------------------------------------------------------------------------- 1 | public class FindPivot { 2 | public static void main(String[] args) { 3 | int[] arr = {4,5,6,7,0,1,2}; 4 | System.out.println(findPivot(arr)); 5 | } 6 | 7 | static int findPivot(int[] arr) { 8 | int ind = 0; 9 | for(int i = 0; i < arr.length-1; i++) { 10 | if(arr[i+1] < arr[i]) 11 | return i+1; 12 | } 13 | return -1; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /6. Arrays/src/FirstAndLastPosition.java: -------------------------------------------------------------------------------- 1 | //https://leetcode.com/problems/find-first-and-last-position-of-element-in-sorted-array/ 2 | 3 | //Facebook 4 | 5 | import java.util.*; 6 | 7 | public class FirstAndLastPosition { 8 | public static void main(String[] args) { 9 | int[] arr = {5,7,7,8,8,10}; 10 | System.out.println(Arrays.toString(searchRange(arr, 8))); 11 | } 12 | 13 | static int[] searchRange(int[] arr, int target) { 14 | int[] ans = {-1,-1}; 15 | ans[0] = binarySearch(arr, target, true); 16 | ans[1] = binarySearch(arr, target, false); 17 | return ans; 18 | } 19 | 20 | static int binarySearch(int[] arr, int target, boolean fIndex) { 21 | int st = 0, end = arr.length-1, ans = -1; 22 | while (st <= end) { 23 | int mid = st + (end - st) / 2; 24 | if(arr[mid] == target) { 25 | ans = mid; 26 | if(fIndex) 27 | end = mid - 1; 28 | else 29 | st = mid + 1; 30 | } else if(arr[mid] > target) 31 | end = mid - 1; 32 | else 33 | st = mid + 1; 34 | } 35 | return ans; 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /6. Arrays/src/Floor.java: -------------------------------------------------------------------------------- 1 | public class Floor { 2 | public static void main(String[] args) { 3 | int[] arr = {2, 3, 5, 9, 14, 16, 18}; 4 | System.out.println(floorOfNum(arr, 8)); 5 | } 6 | 7 | static int floorOfNum(int[] arr, int target) { 8 | if(target < arr[0]) 9 | return Integer.MIN_VALUE; 10 | int st = 0, end = arr.length-1; 11 | while (st <= end) { 12 | int mid = st + (end - st) / 2; 13 | if(arr[mid] == target) 14 | return arr[mid]; 15 | else if(arr[mid] > target) 16 | end = mid - 1; 17 | else 18 | st = mid + 1; 19 | } 20 | return arr[end]; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /6. Arrays/src/LargestAndSmallestInArray.java: -------------------------------------------------------------------------------- 1 | public class LargestAndSmallestInArray { 2 | public static void main(String[] args) { 3 | int[] arr = {1,2,5,3,4}; 4 | largestSmallest(arr); 5 | } 6 | 7 | static void largestSmallest(int[] arr) { 8 | int max = Integer.MIN_VALUE; 9 | int min = Integer.MAX_VALUE; 10 | for(int i = 0; i < arr.length; i++) { 11 | if(arr[i] > max) 12 | max = arr[i]; 13 | if(arr[i] < min) 14 | min = arr[i]; 15 | } 16 | System.out.println("The largest element in the array is : " + max); 17 | System.out.println("The smallest element in the array is : " + min); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /6. Arrays/src/MaxAndMin.java: -------------------------------------------------------------------------------- 1 | public class MaxAndMin { 2 | public static void main(String[] args) { 3 | int[] arr = {3, 5, 4, 1, 9}; 4 | int min = Integer.MAX_VALUE; 5 | int max = Integer.MIN_VALUE; 6 | for(int a: arr) { 7 | min = Math.min(a, min); 8 | max = Math.max(a, max); 9 | } 10 | System.out.println("Min in the array is : " + min + " and Max in the array is : " + max); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /6. Arrays/src/MaxSubarraySum.java: -------------------------------------------------------------------------------- 1 | public class MaxSubarraySum { 2 | public static void main(String[] args) { 3 | int[] arr = {1,-2,6,-1,3}; 4 | System.out.println("The maximum subarray sum is : " + maxSubarraySum(arr)); 5 | } 6 | 7 | static int maxSubarraySum(int[] arr) { 8 | int currSum = arr[0], maxSum = arr[0]; 9 | for (int i = 1; i < arr.length; i++) { 10 | currSum = Math.max(currSum+arr[i], arr[i]); 11 | maxSum = Math.max(maxSum, currSum); 12 | } 13 | return maxSum; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /6. Arrays/src/Pairs.java: -------------------------------------------------------------------------------- 1 | public class Pairs { 2 | public static void main(String[] args) { 3 | int[] arr = {1,2,3,4,5}; 4 | pairs(arr); 5 | } 6 | 7 | static void pairs(int[] arr) { 8 | int tp = ((arr.length)*(arr.length-1))/2; 9 | System.out.println("Number of unique pairs that can be formed using the elements in the array is : " + tp); 10 | for (int i = 0; i < arr.length; i++) { 11 | for (int j = i+1; j < arr.length; j++) { 12 | System.out.print("(" + arr[i] + "," + arr[j] + ") "); 13 | } 14 | System.out.println(); 15 | } 16 | return; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /6. Arrays/src/PeakIndexInMountainArray.java: -------------------------------------------------------------------------------- 1 | //https://leetcode.com/problems/peak-index-in-a-mountain-array/ 2 | 3 | public class PeakIndexInMountainArray { 4 | public static void main(String[] args) { 5 | int[] arr = {0,10,5,2}; 6 | System.out.println(peakIndexInMountainArray(arr)); 7 | } 8 | 9 | static int peakIndexInMountainArray(int[] arr) { 10 | int st = 0, end = arr.length - 1; 11 | while (st < end) { 12 | int mid = st + (end - st) / 2; 13 | if (arr[mid] < arr[mid + 1]) 14 | st = mid + 1; 15 | else 16 | end = mid; 17 | } 18 | return st; 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /6. Arrays/src/PrintSubarrays.java: -------------------------------------------------------------------------------- 1 | public class PrintSubarrays { 2 | public static void main(String[] args) { 3 | int[] arr = {1,2,3,4,5}; 4 | printSubarrays(arr); 5 | } 6 | 7 | static void printSubarrays(int[] arr) { 8 | int minSum = arr[0], maxSum = arr[0]; 9 | for (int i = 1; i < arr.length; i++) { 10 | if(arr[i] < minSum) 11 | minSum = arr[i]; 12 | maxSum += arr[i]; 13 | } 14 | for (int i = 0; i < arr.length; i++) { 15 | System.out.print("["); 16 | for (int j = 0; j <= i; j++) { 17 | if(j != i) { 18 | System.out.print(arr[j] + ","); 19 | } else 20 | System.out.print(arr[j]); 21 | } 22 | System.out.print("]"); 23 | System.out.println(); 24 | } 25 | System.out.println("The minimum subarray sum is : " + minSum); 26 | System.out.println("The maximum subarray sum is : " + maxSum); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /6. Arrays/src/RainwaterTrapping.java: -------------------------------------------------------------------------------- 1 | public class RainwaterTrapping { 2 | public static void main(String[] args) { 3 | int[] arr = {4,2,0,6,3,2,5}; 4 | System.out.println(rainwater(arr)); 5 | } 6 | 7 | static int rainwater(int[] arr) { 8 | int[] lmax = new int[arr.length]; 9 | int[] rmax = new int[arr.length]; 10 | int max = 0; 11 | for(int i = arr.length - 1; i >= 0; i--) { 12 | max = Math.max(max, arr[i]); 13 | rmax[i] = max; 14 | } 15 | max = 0; 16 | for (int i = 0; i < arr.length; i++) { 17 | max = Math.max(max, arr[i]); 18 | lmax[i] = max; 19 | } 20 | int water = 0; 21 | for (int i = 1; i < arr.length - 1; i++) { 22 | int high = Math.min(lmax[i], rmax[i]); 23 | water += (high - arr[i]); 24 | } 25 | return water; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /6. Arrays/src/ReverseArray.java: -------------------------------------------------------------------------------- 1 | import java.util.Arrays; 2 | 3 | public class ReverseArray { 4 | public static void main(String[] args) { 5 | int[] arr = {2,4,6,8,10}; 6 | reverse(arr); 7 | System.out.println(Arrays.toString(arr)); 8 | } 9 | 10 | static void reverse(int[] arr) { 11 | int st = 0, end = arr.length-1; 12 | while(st < end) { 13 | int temp = arr[st]; 14 | arr[st] = arr[end]; 15 | arr[end] = temp; 16 | st++; 17 | end--; 18 | } 19 | return; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /6. Arrays/src/SearchInInfiniteArray.java: -------------------------------------------------------------------------------- 1 | public class SearchInInfiniteArray { 2 | public static void main(String[] args) { 3 | int arr[] = new int[]{3, 5, 7, 9, 10, 90, 4 | 100, 130, 140, 160, 170}; 5 | int ans = findPos(arr,10); 6 | System.out.println(ans); 7 | } 8 | 9 | static int findPos(int[] arr, int target) { 10 | int st = 0, end = 1; 11 | while (target > arr[end]) { 12 | int temp = end+1; 13 | end = end + (end - st + 1) * 2; 14 | st = temp; 15 | } 16 | return binarySearch(arr, target, st, end); 17 | } 18 | 19 | static int binarySearch(int[] arr, int target, int st, int end) { 20 | while (st <= end) { 21 | int mid = st + (end - st) / 2; 22 | if(arr[mid] == target) 23 | return mid; 24 | else if(arr[mid] < target) 25 | st = mid + 1; 26 | else 27 | end = mid - 1; 28 | } 29 | return -1; 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /6. Arrays/src/SearchInMountainArray.java: -------------------------------------------------------------------------------- 1 | //https://leetcode.com/problems/find-in-mountain-array 2 | 3 | public class SearchInMountainArray { 4 | public static void main(String[] args) { 5 | int[] arr = {1,2,3,4,5,3,1}; 6 | 7 | } 8 | 9 | static int findInMountainArray(int target, int[] arr) { 10 | int peak = findPeak(arr); 11 | int ans = binarySearch(target, arr, 0, peak); 12 | if(ans != -1) 13 | return ans; 14 | return binarySearch(target, arr, peak+1, arr.length-1); 15 | } 16 | 17 | static int findPeak(int[] arr) { 18 | int st = 0, end = arr.length - 1; 19 | while(st < end) { 20 | int mid = st + (end - st) / 2; 21 | if(arr[mid] < arr[mid+1]) 22 | st = mid+1; 23 | else 24 | end = mid; 25 | } 26 | return st; 27 | } 28 | 29 | static int binarySearch(int target, int[] arr, int st, int end) { 30 | // find whether the array is sorted in ascending or descending 31 | boolean isAsc = (arr[st] < arr[end]); 32 | 33 | while(st <= end) { 34 | // find the middle element 35 | // int mid = (start + end) / 2; // might be possible that (start + end) exceeds the range of int in java 36 | int mid = st + (end - st) / 2; 37 | 38 | if (arr[mid] == target) { 39 | return mid; 40 | } 41 | 42 | if (isAsc) { 43 | if (target < arr[mid]) { 44 | end = mid - 1; 45 | } else { 46 | st = mid + 1; 47 | } 48 | } else { 49 | if (target > arr[mid]) { 50 | end = mid - 1; 51 | } else { 52 | st = mid + 1; 53 | } 54 | } 55 | } 56 | return -1; 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /6. Arrays/src/SearchInRotated.java: -------------------------------------------------------------------------------- 1 | public class SearchInRotated { 2 | public static void main(String[] args) { 3 | int[] arr = {4,5,6,7,0,1,2}; 4 | System.out.println(search(arr, 0)); 5 | } 6 | 7 | static int findPivot(int[] arr) { 8 | int st = 0, end = arr.length-1; 9 | while(st < end) { 10 | int mid = st + (end - st)/2; 11 | if(mid > 0 && arr[mid] < arr[mid-1] || mid <= arr.length-1 && arr[mid] > arr[mid+1]) 12 | return arr[mid] < arr[mid-1] ? mid-1 : mid; 13 | if(arr[mid] > arr[0]) 14 | st = mid + 1; 15 | else 16 | end = mid - 1; 17 | } 18 | return 0; 19 | } 20 | 21 | static int rotatedSearch(int[] arr, int target) { 22 | int pivot = findPivot(arr); 23 | if(target == arr[pivot]) 24 | return pivot; 25 | else if(target > arr[0] || pivot == 0) 26 | return bsearch(arr, target, pivot+1, arr.length-1); 27 | else 28 | return bsearch(arr, target, 0, pivot-1); 29 | } 30 | 31 | static int bsearch(int[] arr, int target, int st, int end) { 32 | while (st <= end) { 33 | int mid = st + (end - st) / 2; 34 | if(arr[mid] == target) 35 | return mid; 36 | else if(arr[mid] > target) 37 | end = mid - 1; 38 | else 39 | st = mid + 1; 40 | } 41 | return -1; 42 | } 43 | 44 | static int search(int[] arr, int target) { 45 | int st = 0, end = arr.length-1; 46 | while (st <= end) { 47 | int mid = st + (end - st) / 2; 48 | if(arr[mid] == target) 49 | return mid; 50 | if(arr[mid] >= arr[0]) { // To determine which half of the array we are in i.e, increasing half or decreasing half 51 | if(target < arr[mid] && target >= arr[st]) 52 | end = mid - 1; 53 | else 54 | st = mid + 1; 55 | } else { 56 | if(target > arr[mid] && target <= arr[end]) 57 | st = mid + 1; 58 | else 59 | end = mid - 1; 60 | } 61 | } 62 | return -1; 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /6. Arrays/src/SearchInRotatedWIthDuplicates.java: -------------------------------------------------------------------------------- 1 | public class SearchInRotatedWIthDuplicates { 2 | public static void main(String[] args) { 3 | int[] arr = {3,1}; 4 | System.out.println(search(arr, 1)); 5 | } 6 | 7 | // In this question find out which side of mid is sorted and write the if condition for when the item is in the 8 | // sorted side from mid then there will be no problemo 9 | static boolean search(int[] arr, int target) { 10 | int st = 0, end = arr.length - 1; 11 | while (st <= end) { 12 | int mid = st + (end - st) / 2; 13 | if(arr[mid] == target) 14 | return true; 15 | 16 | // left side of mid is sorted (mid is also part of the sorted array half) 17 | if(arr[mid] > arr[st] || arr[mid] > arr[end]) { 18 | // end = mid - 1; 19 | if(target < arr[mid] && target >= arr[st]) 20 | end = mid - 1; 21 | // st = mid + 1; 22 | else 23 | st = mid + 1; 24 | } 25 | // right side of mid is sorted (mid is also part of the sorted array half) 26 | else if(arr[mid] < arr[st] || arr[mid] < arr[end]) { 27 | // st = mid + 1; 28 | if(target > arr[mid] && target <= arr[end]) 29 | st = mid + 1; 30 | // end = mid - 1; 31 | else 32 | end = mid - 1; 33 | } 34 | // when arr[mid] == arr[st] == arr[end] 35 | else { 36 | end--; 37 | } 38 | } 39 | return false; 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /6. Arrays/src/SmallestLetterGreaterThanTarget.java: -------------------------------------------------------------------------------- 1 | //https://leetcode.com/problems/find-smallest-letter-greater-than-target/ 2 | 3 | public class SmallestLetterGreaterThanTarget { 4 | public static void main(String[] args) { 5 | char[] letters = {'c', 'f', 'j'}; 6 | System.out.println(nextGreaterLetter(letters, 'k')); 7 | } 8 | 9 | static char nextGreaterLetter(char[] arr, char target) { 10 | int n = arr.length; 11 | int st = 0, end = n - 1; 12 | while (st <= end) { 13 | int mid = st + (end - st) / 2; 14 | if(arr[mid] <= target) 15 | st = mid + 1; 16 | else if(arr[mid] > target) 17 | end = mid - 1; 18 | } 19 | return arr[st % n]; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /6. Arrays/src/Solution.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class Solution { 4 | public static void main(String[] args) { 5 | int[] arr = {4,5,6,7,0,1,2}; 6 | System.out.println(rotatedSearch(arr, 1)); 7 | } 8 | 9 | static int findPivot(int[] arr) { 10 | int st = 0, end = arr.length-1; 11 | while (st <= end) { 12 | int mid = st + (end - st) / 2; 13 | if(mid > 0 && arr[mid] < arr[mid-1]) 14 | return mid; 15 | if(mid < arr.length-1 && arr[mid] > arr[mid+1]) 16 | return mid+1; 17 | if(arr[mid] < arr[0]) 18 | end = mid - 1; 19 | else 20 | st = mid + 1; 21 | } 22 | return -1; 23 | } 24 | 25 | static int rotatedSearch(int[] arr, int target) { 26 | int pivot = findPivot(arr); 27 | if(arr[pivot] == target) 28 | return pivot; 29 | else if(target > arr[pivot]) 30 | return bsearch(arr, target, 0, pivot-1); 31 | else 32 | return bsearch(arr, target, pivot+1, arr.length-1); 33 | } 34 | 35 | static int bsearch(int[] arr, int target, int st, int end) { 36 | while (st <= end) { 37 | int mid = st + (end - st) / 2; 38 | if(arr[mid] == target) 39 | return mid; 40 | else if(arr[mid] > target) 41 | end = mid - 1; 42 | else 43 | st = mid + 1; 44 | } 45 | return -1; 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /6. Arrays/src/SplitArrayLargestSum.java: -------------------------------------------------------------------------------- 1 | public class SplitArrayLargestSum { 2 | public static void main(String[] args) { 3 | int[] arr = {7,2,5,10,8}; 4 | System.out.println(splitArrayOptimised(arr, 2)); 5 | } 6 | 7 | // Naive approach using backtracking 8 | 9 | static int splitArray(int[] arr, int k, int ind, int sum, int maxSum, int ans) { 10 | // K=1 is the base Case 11 | if (k == 1) { 12 | maxSum = Math.max(maxSum, sum); 13 | sum = 0; 14 | for (int i = ind; i < arr.length; i++) { 15 | sum += arr[i]; 16 | } 17 | // we update maxSum 18 | maxSum = Math.max(maxSum, sum); 19 | // the answer is stored in ans 20 | ans = Math.min(ans, maxSum); 21 | return ans; 22 | } 23 | sum = 0; 24 | // using for loop to divide the array into 25 | // K-subarray 26 | for (int i = ind; i < arr.length; i++) { 27 | sum += arr[i]; 28 | // for each subarray we calculate sum ans update 29 | // maxSum 30 | maxSum = Math.max(maxSum, sum); 31 | // calling function again 32 | ans = splitArray(arr, k - 1, i + 1, sum, maxSum, ans); 33 | } 34 | return ans; 35 | } 36 | 37 | // Optimized solution using binary search 38 | 39 | static int splitArrayOptimised(int[] arr, int k) { 40 | int st = 0, end = 0; 41 | for(int a: arr) { 42 | st = Math.max(0, a); 43 | end += a; 44 | } 45 | while (st < end) { 46 | int mid = st + (end - st) / 2; 47 | int sum = 0, pieces = 1; 48 | for(int a: arr) { 49 | if(sum + a <= mid) 50 | sum += a; 51 | else { 52 | pieces++; 53 | sum = a; 54 | } 55 | } 56 | if(pieces <= k) 57 | end = mid; 58 | else 59 | st = mid + 1; 60 | } 61 | return end; // return st; also works here because at this point st == end 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /6. Arrays/src/StockBuySell.java: -------------------------------------------------------------------------------- 1 | public class StockBuySell { 2 | public static void main(String[] args) { 3 | int[] arr = {7,2,5,3,6,4,1,8}; 4 | System.out.println(profit(arr)); 5 | } 6 | 7 | static int profit(int[] arr) { 8 | int buy = Integer.MAX_VALUE, profit = 0; 9 | for (int i = 0; i < arr.length; i++) { 10 | if(arr[i] < buy) 11 | buy = arr[i]; 12 | profit = Math.max(profit, arr[i] - buy); 13 | } 14 | return profit; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /7. OOPS/.idea/.gitignore: -------------------------------------------------------------------------------- 1 | # Default ignored files 2 | /shelf/ 3 | /workspace.xml 4 | -------------------------------------------------------------------------------- /7. OOPS/.idea/.name: -------------------------------------------------------------------------------- 1 | OOPS -------------------------------------------------------------------------------- /7. OOPS/.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /7. OOPS/.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /7. OOPS/.idea/runConfigurations.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 9 | 10 | -------------------------------------------------------------------------------- /7. OOPS/OOPS.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /7. OOPS/out/production/OOPS/Aneemal.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashprasad234/Java-Basics/411547f5c993e1a544b702ece01747639a7bb96e/7. OOPS/out/production/OOPS/Aneemal.class -------------------------------------------------------------------------------- /7. OOPS/out/production/OOPS/Animal.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashprasad234/Java-Basics/411547f5c993e1a544b702ece01747639a7bb96e/7. OOPS/out/production/OOPS/Animal.class -------------------------------------------------------------------------------- /7. OOPS/out/production/OOPS/Animals.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashprasad234/Java-Basics/411547f5c993e1a544b702ece01747639a7bb96e/7. OOPS/out/production/OOPS/Animals.class -------------------------------------------------------------------------------- /7. OOPS/out/production/OOPS/Animilia.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashprasad234/Java-Basics/411547f5c993e1a544b702ece01747639a7bb96e/7. OOPS/out/production/OOPS/Animilia.class -------------------------------------------------------------------------------- /7. OOPS/out/production/OOPS/Bear.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashprasad234/Java-Basics/411547f5c993e1a544b702ece01747639a7bb96e/7. OOPS/out/production/OOPS/Bear.class -------------------------------------------------------------------------------- /7. OOPS/out/production/OOPS/Birds.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashprasad234/Java-Basics/411547f5c993e1a544b702ece01747639a7bb96e/7. OOPS/out/production/OOPS/Birds.class -------------------------------------------------------------------------------- /7. OOPS/out/production/OOPS/Bishop.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashprasad234/Java-Basics/411547f5c993e1a544b702ece01747639a7bb96e/7. OOPS/out/production/OOPS/Bishop.class -------------------------------------------------------------------------------- /7. OOPS/out/production/OOPS/BlueCar.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashprasad234/Java-Basics/411547f5c993e1a544b702ece01747639a7bb96e/7. OOPS/out/production/OOPS/BlueCar.class -------------------------------------------------------------------------------- /7. OOPS/out/production/OOPS/Calculator.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashprasad234/Java-Basics/411547f5c993e1a544b702ece01747639a7bb96e/7. OOPS/out/production/OOPS/Calculator.class -------------------------------------------------------------------------------- /7. OOPS/out/production/OOPS/Car.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashprasad234/Java-Basics/411547f5c993e1a544b702ece01747639a7bb96e/7. OOPS/out/production/OOPS/Car.class -------------------------------------------------------------------------------- /7. OOPS/out/production/OOPS/Carnivore.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashprasad234/Java-Basics/411547f5c993e1a544b702ece01747639a7bb96e/7. OOPS/out/production/OOPS/Carnivore.class -------------------------------------------------------------------------------- /7. OOPS/out/production/OOPS/ChessPiece.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashprasad234/Java-Basics/411547f5c993e1a544b702ece01747639a7bb96e/7. OOPS/out/production/OOPS/ChessPiece.class -------------------------------------------------------------------------------- /7. OOPS/out/production/OOPS/Complex.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashprasad234/Java-Basics/411547f5c993e1a544b702ece01747639a7bb96e/7. OOPS/out/production/OOPS/Complex.class -------------------------------------------------------------------------------- /7. OOPS/out/production/OOPS/Constructors.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashprasad234/Java-Basics/411547f5c993e1a544b702ece01747639a7bb96e/7. OOPS/out/production/OOPS/Constructors.class -------------------------------------------------------------------------------- /7. OOPS/out/production/OOPS/Deer.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashprasad234/Java-Basics/411547f5c993e1a544b702ece01747639a7bb96e/7. OOPS/out/production/OOPS/Deer.class -------------------------------------------------------------------------------- /7. OOPS/out/production/OOPS/Dog.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashprasad234/Java-Basics/411547f5c993e1a544b702ece01747639a7bb96e/7. OOPS/out/production/OOPS/Dog.class -------------------------------------------------------------------------------- /7. OOPS/out/production/OOPS/ExceptionHandling.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashprasad234/Java-Basics/411547f5c993e1a544b702ece01747639a7bb96e/7. OOPS/out/production/OOPS/ExceptionHandling.class -------------------------------------------------------------------------------- /7. OOPS/out/production/OOPS/Fish.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashprasad234/Java-Basics/411547f5c993e1a544b702ece01747639a7bb96e/7. OOPS/out/production/OOPS/Fish.class -------------------------------------------------------------------------------- /7. OOPS/out/production/OOPS/Fishes.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashprasad234/Java-Basics/411547f5c993e1a544b702ece01747639a7bb96e/7. OOPS/out/production/OOPS/Fishes.class -------------------------------------------------------------------------------- /7. OOPS/out/production/OOPS/Herbivore.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashprasad234/Java-Basics/411547f5c993e1a544b702ece01747639a7bb96e/7. OOPS/out/production/OOPS/Herbivore.class -------------------------------------------------------------------------------- /7. OOPS/out/production/OOPS/HierachialInheritance.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashprasad234/Java-Basics/411547f5c993e1a544b702ece01747639a7bb96e/7. OOPS/out/production/OOPS/HierachialInheritance.class -------------------------------------------------------------------------------- /7. OOPS/out/production/OOPS/Horse.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashprasad234/Java-Basics/411547f5c993e1a544b702ece01747639a7bb96e/7. OOPS/out/production/OOPS/Horse.class -------------------------------------------------------------------------------- /7. OOPS/out/production/OOPS/InterfaceInJava.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashprasad234/Java-Basics/411547f5c993e1a544b702ece01747639a7bb96e/7. OOPS/out/production/OOPS/InterfaceInJava.class -------------------------------------------------------------------------------- /7. OOPS/out/production/OOPS/King.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashprasad234/Java-Basics/411547f5c993e1a544b702ece01747639a7bb96e/7. OOPS/out/production/OOPS/King.class -------------------------------------------------------------------------------- /7. OOPS/out/production/OOPS/Knight.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashprasad234/Java-Basics/411547f5c993e1a544b702ece01747639a7bb96e/7. OOPS/out/production/OOPS/Knight.class -------------------------------------------------------------------------------- /7. OOPS/out/production/OOPS/Main.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashprasad234/Java-Basics/411547f5c993e1a544b702ece01747639a7bb96e/7. OOPS/out/production/OOPS/Main.class -------------------------------------------------------------------------------- /7. OOPS/out/production/OOPS/Mammal.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashprasad234/Java-Basics/411547f5c993e1a544b702ece01747639a7bb96e/7. OOPS/out/production/OOPS/Mammal.class -------------------------------------------------------------------------------- /7. OOPS/out/production/OOPS/Mammals.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashprasad234/Java-Basics/411547f5c993e1a544b702ece01747639a7bb96e/7. OOPS/out/production/OOPS/Mammals.class -------------------------------------------------------------------------------- /7. OOPS/out/production/OOPS/Mane.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashprasad234/Java-Basics/411547f5c993e1a544b702ece01747639a7bb96e/7. OOPS/out/production/OOPS/Mane.class -------------------------------------------------------------------------------- /7. OOPS/out/production/OOPS/MultiLevelInheritance.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashprasad234/Java-Basics/411547f5c993e1a544b702ece01747639a7bb96e/7. OOPS/out/production/OOPS/MultiLevelInheritance.class -------------------------------------------------------------------------------- /7. OOPS/out/production/OOPS/MultipleInheritanceThroughInterfaces.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashprasad234/Java-Basics/411547f5c993e1a544b702ece01747639a7bb96e/7. OOPS/out/production/OOPS/MultipleInheritanceThroughInterfaces.class -------------------------------------------------------------------------------- /7. OOPS/out/production/OOPS/OOPS.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashprasad234/Java-Basics/411547f5c993e1a544b702ece01747639a7bb96e/7. OOPS/out/production/OOPS/OOPS.class -------------------------------------------------------------------------------- /7. OOPS/out/production/OOPS/Pawn.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashprasad234/Java-Basics/411547f5c993e1a544b702ece01747639a7bb96e/7. OOPS/out/production/OOPS/Pawn.class -------------------------------------------------------------------------------- /7. OOPS/out/production/OOPS/Pen.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashprasad234/Java-Basics/411547f5c993e1a544b702ece01747639a7bb96e/7. OOPS/out/production/OOPS/Pen.class -------------------------------------------------------------------------------- /7. OOPS/out/production/OOPS/Polymorphism.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashprasad234/Java-Basics/411547f5c993e1a544b702ece01747639a7bb96e/7. OOPS/out/production/OOPS/Polymorphism.class -------------------------------------------------------------------------------- /7. OOPS/out/production/OOPS/Queen.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashprasad234/Java-Basics/411547f5c993e1a544b702ece01747639a7bb96e/7. OOPS/out/production/OOPS/Queen.class -------------------------------------------------------------------------------- /7. OOPS/out/production/OOPS/Rook.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashprasad234/Java-Basics/411547f5c993e1a544b702ece01747639a7bb96e/7. OOPS/out/production/OOPS/Rook.class -------------------------------------------------------------------------------- /7. OOPS/out/production/OOPS/SingleLevelInheritance.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashprasad234/Java-Basics/411547f5c993e1a544b702ece01747639a7bb96e/7. OOPS/out/production/OOPS/SingleLevelInheritance.class -------------------------------------------------------------------------------- /7. OOPS/out/production/OOPS/Student.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashprasad234/Java-Basics/411547f5c993e1a544b702ece01747639a7bb96e/7. OOPS/out/production/OOPS/Student.class -------------------------------------------------------------------------------- /7. OOPS/out/production/OOPS/SuperKeywordImplementation.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashprasad234/Java-Basics/411547f5c993e1a544b702ece01747639a7bb96e/7. OOPS/out/production/OOPS/SuperKeywordImplementation.class -------------------------------------------------------------------------------- /7. OOPS/src/BlueCar.java: -------------------------------------------------------------------------------- 1 | abstract class Car { 2 | static { 3 | System.out.print("1"); 4 | } 5 | 6 | Car(String name) { 7 | System.out.print("2"); 8 | } 9 | 10 | { 11 | System.out.print("3"); 12 | } 13 | } 14 | 15 | public class BlueCar extends Car { 16 | { 17 | System.out.print("4"); 18 | } 19 | 20 | public BlueCar() { 21 | super("blue"); 22 | System.out.print(5); 23 | } 24 | 25 | public static void main(String[] args) { 26 | new BlueCar(); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /7. OOPS/src/Constructors.java: -------------------------------------------------------------------------------- 1 | public class Constructors { 2 | public static void main(String[] args) { 3 | int[] marks = {70, 65, 73}; 4 | Student s1 = new Student("Yash", 71, marks); 5 | Student s2 = new Student(s1); 6 | s2.marks[0] = 30; 7 | System.out.println("s1 marks"); 8 | for (int i = 0; i < 3; i++) { 9 | System.out.println(s1.marks[i]); 10 | } 11 | System.out.println(); 12 | System.out.println("s2 marks"); 13 | for (int i = 0; i < 3; i++) { 14 | System.out.println(s2.marks[i]); 15 | } 16 | } 17 | } 18 | 19 | class Student { 20 | String name; 21 | int roll; 22 | int[] marks; 23 | 24 | Student(String name, int roll, int[] marks) { 25 | marks = new int[3]; 26 | this.name = name; 27 | this.roll = roll; 28 | this.marks = marks; 29 | } 30 | 31 | Student(Student old) { 32 | this.name = old.name; 33 | this.roll = old.roll; 34 | this.marks = old.marks; 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /7. OOPS/src/ExceptionHandling.java: -------------------------------------------------------------------------------- 1 | public class ExceptionHandling { 2 | public static void main(String[] args) { 3 | 4 | //try keyword 5 | try { 6 | int[] num = {1,2,3}; 7 | System.out.println(num[10]); 8 | } 9 | 10 | //catch keyword 11 | catch (Exception e) { 12 | System.out.println("Something went wrong"); 13 | } 14 | 15 | //finally keyword 16 | finally { 17 | System.out.println("The 'try catch' has finished."); 18 | } 19 | 20 | checkAge(17); 21 | } 22 | 23 | //throw keyword 24 | static void checkAge(int n) { 25 | if(n < 18) 26 | throw new ArithmeticException("Access Denied"); 27 | else 28 | System.out.println("Access Granted"); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /7. OOPS/src/HierachialInheritance.java: -------------------------------------------------------------------------------- 1 | public class HierachialInheritance { 2 | public static void main(String[] args) { 3 | 4 | } 5 | } 6 | 7 | class Animilia { 8 | void eats() { 9 | System.out.println("Eats anything"); 10 | } 11 | 12 | void breathes() { 13 | System.out.println("breathes"); 14 | } 15 | } 16 | 17 | class Fishes { 18 | void swims() { 19 | System.out.println("swims"); 20 | } 21 | } 22 | 23 | class Mammals { 24 | void walks() { 25 | System.out.println("walks"); 26 | } 27 | } 28 | 29 | class Birds { 30 | void fly() { 31 | System.out.println("flies"); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /7. OOPS/src/InterfaceInJava.java: -------------------------------------------------------------------------------- 1 | public class InterfaceInJava { 2 | public static void main(String[] args) { 3 | Queen q = new Queen(); 4 | q.move(); 5 | } 6 | } 7 | 8 | interface ChessPiece { 9 | void move(); 10 | } 11 | 12 | class Queen implements ChessPiece { 13 | public void move() { 14 | System.out.println("up, down, left, right and diagonally (in any direction)"); 15 | } 16 | } 17 | 18 | class Rook implements ChessPiece { 19 | public void move() { 20 | System.out.println("up, down, left, right"); 21 | } 22 | } 23 | 24 | class Bishop implements ChessPiece { 25 | public void move() { 26 | System.out.println("diagonally (in any direction)"); 27 | } 28 | } 29 | 30 | class Knight implements ChessPiece { 31 | public void move() { 32 | System.out.println("two places in any direction and one place perpendicular to that direction (like an L)"); 33 | } 34 | } 35 | 36 | class Pawn implements ChessPiece { 37 | public void move() { 38 | System.out.println("up one places and diagonally upwards one place when capturing"); 39 | } 40 | } 41 | 42 | class King implements ChessPiece { 43 | public void move() { 44 | System.out.println("up, down, left, right and diagonally (in any direction) one place"); 45 | } 46 | } -------------------------------------------------------------------------------- /7. OOPS/src/Main.java: -------------------------------------------------------------------------------- 1 | public class Main { 2 | public static void main(String[] args) { 3 | Complex num1 = new Complex(5, 4); 4 | Complex num2 = new Complex(4, 2); 5 | Complex.printComplex(Complex.sum(num1, num2)); 6 | Complex.printComplex(Complex.diff(num1, num2)); 7 | Complex.printComplex(Complex.product(num1, num2)); 8 | } 9 | } 10 | 11 | class Complex { 12 | int real; 13 | int imaginary; 14 | 15 | Complex(int real, int imaginary) { 16 | this.real = real; 17 | this.imaginary = imaginary; 18 | } 19 | 20 | static Complex sum (Complex num1, Complex num2) { 21 | return new Complex(num1.real + num2.real, num1.imaginary + num2.imaginary); 22 | } 23 | 24 | static Complex diff (Complex num1, Complex num2) { 25 | return new Complex(num1.real - num2.real, num1.imaginary - num2.imaginary); 26 | } 27 | 28 | static Complex product (Complex num1, Complex num2) { 29 | return new Complex((num1.real*num2.real) - (num2.imaginary* num1.imaginary), num1.imaginary* num2.real + num2.imaginary*num1.real); 30 | } 31 | 32 | static void printComplex(Complex ans) { 33 | System.out.print(ans.real); 34 | if(ans.imaginary > 0) { 35 | System.out.println(" + " + ans.imaginary+"i"); 36 | } 37 | if(ans.imaginary < 0) { 38 | System.out.println(" - " + ans.imaginary+"i"); 39 | } 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /7. OOPS/src/Mane.java: -------------------------------------------------------------------------------- 1 | import java.time.LocalDate; 2 | import java.time.Period; 3 | 4 | public class Mane { 5 | public static void main(String[] args) { 6 | // Parse the start and end dates 7 | LocalDate startDate = LocalDate.parse("2022-01-01"); 8 | LocalDate endDate = LocalDate.parse("2022-01-31"); 9 | 10 | // Calculate the difference between the two dates 11 | Period difference = Period.between(startDate, endDate); 12 | 13 | // Print the number of days 14 | System.out.println(difference.getDays()); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /7. OOPS/src/MultiLevelInheritance.java: -------------------------------------------------------------------------------- 1 | public class MultiLevelInheritance { 2 | public static void main(String[] args) { 3 | Dog dobby = new Dog(); 4 | dobby.legs = 4; 5 | dobby.breed = "Labrador"; 6 | System.out.println(dobby.breed); 7 | } 8 | } 9 | 10 | class Animals { 11 | void eats() { 12 | System.out.println("Eats anything"); 13 | } 14 | 15 | void breathes() { 16 | System.out.println("breathes"); 17 | } 18 | } 19 | 20 | class Mammal extends Animal{ 21 | int legs; 22 | } 23 | 24 | class Dog extends Mammal { 25 | String breed; 26 | } 27 | -------------------------------------------------------------------------------- /7. OOPS/src/MultipleInheritanceThroughInterfaces.java: -------------------------------------------------------------------------------- 1 | public class MultipleInheritanceThroughInterfaces { 2 | public static void main(String[] args) { 3 | Bear polarBear = new Bear(); 4 | polarBear.eats(); 5 | } 6 | } 7 | 8 | interface Herbivore { 9 | void eats(); 10 | } 11 | 12 | interface Carnivore { 13 | void eats(); 14 | } 15 | 16 | class Bear implements Herbivore, Carnivore { 17 | public void eats() { 18 | System.out.println("Eats other animals and plants"); 19 | } 20 | } -------------------------------------------------------------------------------- /7. OOPS/src/OOPS.java: -------------------------------------------------------------------------------- 1 | public class OOPS { 2 | public static void main(String[] args) { 3 | Pen p1 = new Pen(); 4 | p1.setColor("Aasmaani"); 5 | System.out.println(p1.getColor()); 6 | p1.setTip(7); 7 | System.out.println(p1.getTip()); 8 | } 9 | } 10 | 11 | class Pen { 12 | private String color; 13 | private int tip; 14 | 15 | String getColor() { 16 | return color; 17 | } 18 | 19 | int getTip() { 20 | return tip; 21 | } 22 | 23 | void setColor(String color) { 24 | this.color = color; 25 | } 26 | 27 | void setTip(int tip) { 28 | this.tip = tip; 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /7. OOPS/src/Polymorphism.java: -------------------------------------------------------------------------------- 1 | public class Polymorphism { 2 | public static void main(String[] args) { 3 | // Calculator calc = new Calculator(); 4 | // System.out.println(calc.sum(3,4)); 5 | // System.out.println(calc.sum(2.5f,4.5f)); 6 | // System.out.println(calc.sum(2,4, 1)); 7 | Deer thehiran = new Deer(); 8 | thehiran.eats(); 9 | } 10 | } 11 | 12 | //Compile time Polymorphism (method overloading) 13 | class Calculator { 14 | int sum(int a, int b) { 15 | return a + b; 16 | } 17 | 18 | float sum(float a, float b) { 19 | return a + b; 20 | } 21 | 22 | int sum(int a, int b, int c) { 23 | return a + b + c; 24 | } 25 | } 26 | 27 | //Run time polymorphism method overriding 28 | class Deer extends Animal { //class animal is defined in a different file within the same package 29 | void eats() { 30 | System.out.println("eats grass"); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /7. OOPS/src/SingleLevelInheritance.java: -------------------------------------------------------------------------------- 1 | public class SingleLevelInheritance { 2 | public static void main(String[] args) { 3 | Fish sharks = new Fish(); 4 | sharks.eats(); 5 | } 6 | 7 | } 8 | 9 | //Base or Parent Class 10 | class Animal { 11 | void eats() { 12 | System.out.println("Eats anything"); 13 | } 14 | 15 | void breathes() { 16 | System.out.println("Breathes"); 17 | } 18 | } 19 | 20 | //Derived or Child Class 21 | class Fish extends Animal { 22 | int fins; 23 | void swims() { 24 | System.out.println("Swims"); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /7. OOPS/src/SuperKeywordImplementation.java: -------------------------------------------------------------------------------- 1 | public class SuperKeywordImplementation { 2 | public static void main(String[] args) { 3 | Horse h = new Horse(); 4 | System.out.println(h.color); 5 | } 6 | } 7 | 8 | class Aneemal { 9 | String color; 10 | Aneemal() { 11 | System.out.println("animal constructor called"); 12 | } 13 | } 14 | 15 | class Horse extends Aneemal { 16 | Horse() { 17 | super(); //done automatically by java 18 | System.out.println("horse constructor called"); 19 | super.color = "brown"; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Java-Basics 2 | 3 | ## This repository contains all the concepts I learned while I was learning the basics of the programming language Java 4 | --------------------------------------------------------------------------------