├── src └── main │ └── java │ └── org │ └── example │ ├── exceptions │ ├── README.md │ ├── NullPointException.java │ ├── StringIndexOutOfBoundsException.java │ ├── ArrayIndexOutOfBound.java │ └── DivideByZeroException.java │ ├── files │ ├── README.md │ ├── WriteToFile.java │ ├── Files_api.java │ ├── ReadLinebyLine.java │ └── CopyFileWithoutBuffer.java │ ├── oop │ ├── README.md │ ├── abstraction │ │ ├── arrayofobjects │ │ │ ├── Shape.java │ │ │ ├── Area.java │ │ │ └── Main.java │ │ ├── TestInterface2.java │ │ └── ReptileInterface.java │ ├── functions │ │ ├── Circumference.java │ │ ├── Vote.java │ │ ├── Table.java │ │ ├── Even.java │ │ ├── Prime.java │ │ └── Factorial.java │ ├── classandobject │ │ ├── Student.java │ │ └── StudentInfo.java │ ├── encapsulation │ │ ├── Areas.java │ │ └── Encapsulation.java │ ├── statickeyword │ │ └── JavaStaticKeyword.java │ ├── polymorphism │ │ ├── CompileTimePolymorphism.java │ │ ├── RunTimePolymorphism.java │ │ └── SumPoly.java │ └── inheritance │ │ ├── InheritanceExample.java │ │ └── InheritanceInInterfaces.java │ ├── collections │ ├── java-collection-hierarchy.png │ ├── VectorExample.java │ ├── ListExample.java │ ├── HashSetExample.java │ ├── ArrayListExample.java │ ├── MapExample.java │ ├── PriorityQueueExample.java │ └── StackExample.java │ ├── designpatterns │ ├── factorymethod │ │ ├── Notification.java │ │ ├── SMSNotification.java │ │ ├── EmailNotification.java │ │ ├── PushNotification.java │ │ ├── NotificationFactory.java │ │ └── Test.java │ ├── abstractfactory │ │ ├── Abstract-Factory-Pattern.png │ │ ├── ComputerAbstractFactory.java │ │ ├── ComputerFactory.java │ │ ├── Computer.java │ │ ├── PCFactory.java │ │ ├── ServerFactory.java │ │ ├── TestDesignPatterns.java │ │ ├── PC.java │ │ └── Server.java │ ├── singleton │ │ ├── README.md │ │ ├── Singleton.java │ │ └── Demo.java │ └── prototype │ │ ├── PrototypePatternTest.java │ │ ├── Employees.java │ │ └── README.md │ ├── threads │ └── README.md │ ├── enums │ ├── Day.java │ ├── SchedulingApp.java │ ├── README.md │ └── Schedule.java │ ├── compressiontechniques │ ├── huffmanencoder │ │ ├── Node.java │ │ ├── NodeComparator.java │ │ ├── OutputSingleton.java │ │ └── README.md │ ├── runlengthencoder │ │ ├── README.md │ │ └── RunLengthEncoder.java │ ├── CompressionApp.java │ └── README.md │ ├── datastructures │ ├── linkedlist │ │ ├── Node.java │ │ └── algo │ │ │ ├── DeleteNodeFromLinkedList.java │ │ │ ├── DetectingLoops.java │ │ │ ├── RemoveDuplicateNodesFromSortedList.java │ │ │ ├── RemoveNthNodeFromEnd.java │ │ │ └── LinkedListCycleDetection.java │ ├── queue │ │ ├── Node.java │ │ ├── Queue.java │ │ └── DequeDemo.java │ ├── arrays │ │ ├── LargestNumOfAnArray.java │ │ ├── array.java │ │ ├── LongestName.java │ │ ├── ReverseInteger.java │ │ └── README.md │ └── stack │ │ ├── Stack.java │ │ └── StackArrayList.java │ ├── generics │ ├── BoundedTypesExample.java │ ├── GenericsMethodExample.java │ ├── GenericsClassExample.java │ └── Main.java │ ├── bitmanipulation │ ├── MultiplyingByThreePointFive.java │ ├── UseOfBitwiseXor.java │ ├── UseOfLeftShiftOperator.java │ ├── UseOfRightShiftOperator.java │ ├── UseOfBitwiseAND.java │ ├── UseOfBitwiseOR.java │ ├── UseOfBitwiseComplement.java │ ├── CountTotalSetBits.java │ ├── OppositeSigns.java │ ├── CheckKthBit.java │ ├── MissingNumberUsingXor.java │ ├── GetFirstSetBit.java │ ├── OddEvenUsingBitManipulation.java │ ├── LongestConsectiveOne.java │ └── GetElementAppearingOnce.java │ ├── reflection │ ├── BestFruit.java │ ├── Student.java │ └── FieldModificationDemo.java │ ├── basics │ ├── ClearBit.java │ ├── GetBit.java │ ├── CodeForFinal.java │ ├── SumOfTwoNumber.java │ ├── keith │ │ └── README.md │ ├── ReversedString.java │ ├── Armstrong.java │ ├── PatternPyramid.java │ ├── PatternRhombus.java │ ├── Reversesb.java │ ├── GCD.java │ ├── ReverseArray.java │ ├── PrintHelloWorld.java │ ├── Permutation.java │ ├── UpdateBit.java │ ├── Pascal.java │ ├── FactorsOfANumber.java │ ├── PatternPalinPyramid.java │ ├── CodeForThis.java │ ├── MethodDemo.java │ ├── PowerOfTwo.java │ ├── ValidPangram.java │ ├── AddNaturalNumbersWithoutLoop.java │ ├── UpperCase.java │ ├── DecimalToBinary.java │ ├── EvenUsingFor.java │ ├── Series.java │ ├── EvenOdd.java │ ├── Transpose.java │ ├── TwoDArray.java │ ├── FibonacciSeries.java │ ├── LinearSearch.java │ ├── RemoveDuplicate.java │ ├── LeapYear.java │ ├── ReverseDigits.java │ ├── Tables.java │ ├── PatternDiamond.java │ ├── SwapNumbers.java │ ├── ArrayMinMax.java │ ├── GuessNumber.java │ ├── ValidVotingAge.java │ ├── VariablesDemo.java │ ├── PrintAlphabets.java │ ├── ArrayCheckSort.java │ ├── DublicateArrayCount.java │ ├── PrimeNumbers.java │ ├── PosiNegaZero.java │ ├── TwoDArraySearch.java │ ├── MaxAmongThree.java │ ├── ArmstrongNumber.java │ ├── Raisedto.java │ ├── Formatting.java │ ├── Palindrome.java │ ├── SumOfArray.java │ ├── DudeneyNumber.java │ ├── Election.java │ ├── ArrayDemo.java │ ├── SumOfEvenInArray.java │ ├── AddBinary.java │ ├── PrintName.java │ └── Calculator.java │ ├── lambda │ ├── Printable.java │ ├── Summable.java │ ├── Performer.java │ └── README.md │ ├── strings │ ├── StringLengthExample.java │ ├── StringLowerCaseExample.java │ ├── StringUpperCaseExample.java │ ├── CharReplaceInString.java │ ├── StringConcat.java │ ├── StringTrimExample.java │ ├── StringPrintLines.java │ ├── StringReplaceExample.java │ ├── SubstringExample.java │ ├── StringReverse.java │ ├── StringSubstring.java │ ├── StringStripExample.java │ ├── StringBlankCheckExample.java │ ├── StringEqualsIgnoreCase.java │ ├── StringRepeatExample.java │ ├── StringInternExample.java │ ├── Readme.md │ ├── ValidAnagramString.java │ └── PalindromeString.java │ ├── algorithms │ ├── CelciusToFarenheitMethod.java │ ├── treestore │ │ └── README.md │ ├── FibonacciSeries.java │ ├── recursion │ │ ├── Reverse.java │ │ ├── Fabonacci.java │ │ ├── CalcPower.java │ │ ├── KthGrammar.java │ │ ├── TowerOfHanoi.java │ │ ├── JosephusUsingRecursion.java │ │ └── FindOccurance.java │ ├── FactorialNNumber.java │ ├── backtracking │ │ ├── TilesProblem.java │ │ └── RatInMaze.java │ ├── KaddensAlgo.java │ ├── CheckArraySortedNot.java │ ├── FindingExtremeValues.java │ ├── sorting │ │ ├── CyclicSort.java │ │ ├── InsertionSort.java │ │ ├── BubbleSort.java │ │ ├── SelectionSort.java │ │ ├── ShellSort.java │ │ ├── QuickSort.java │ │ └── PancakeSort.java │ └── searching │ │ └── LinearSearch.java │ ├── streams │ └── Readme.md │ ├── leetcode │ ├── MoveZeroes.java │ ├── ClimbingStairs.java │ ├── SingleNumber.java │ ├── ValidAnagram.java │ ├── RemoveDuplicates.java │ ├── DiagonalMatrixSum.java │ ├── LongestValidParentheses.java │ ├── MaximumSubArray.java │ ├── BuyAndSellStock.java │ ├── TrappingRainWater.java │ ├── TwoSum.java │ ├── GenerateParenthesis.java │ ├── ProductArrayExceptSelf.java │ ├── RomanToInteger.java │ ├── CombinationSum.java │ ├── RansomNote.java │ ├── MinimumSumGreedy.java │ ├── CloneGraph.java │ ├── MergeKSortedList.java │ ├── MedianSortedArrays.java │ └── MaxStackSum.java │ ├── switchexpression │ ├── Readme.md │ ├── SizeChecker.java │ └── SwitchExample.java │ ├── deflate │ └── README.md │ ├── patterns │ ├── CharacterPattern.java │ ├── HalfPyramid.java │ ├── NumberPyramid.java │ ├── InvertedHalfPyramid.java │ ├── SolidRectangle.java │ ├── RightTrianglePattern.java │ ├── FloydsTriangle.java │ ├── Descendingorder.java │ ├── RotatedHalfPyramid.java │ ├── ReversedPyramidStarPattern.java │ ├── ZeroOneTriangle.java │ ├── Palindromic.java │ ├── NumberRectangle.java │ └── HollowRectangle.java │ ├── functionalprogramming │ ├── SupplierExample.java │ └── PredicateExample.java │ ├── javatexteditor │ └── README.md │ └── socket │ └── Client.java ├── hacktoberfest-banner.jpg ├── .gitignore ├── pom.xml └── README.md /src/main/java/org/example/exceptions/README.md: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /src/main/java/org/example/files/README.md: -------------------------------------------------------------------------------- 1 | Hi Welcome To Open Source Contribution -------------------------------------------------------------------------------- /hacktoberfest-banner.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BimalRajGyawali/hacktoberfest-java/HEAD/hacktoberfest-banner.jpg -------------------------------------------------------------------------------- /src/main/java/org/example/oop/README.md: -------------------------------------------------------------------------------- 1 | The file consists of the various object-oriented programming concepts which will help the beginners to 2 | go through their concepts. -------------------------------------------------------------------------------- /src/main/java/org/example/collections/java-collection-hierarchy.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BimalRajGyawali/hacktoberfest-java/HEAD/src/main/java/org/example/collections/java-collection-hierarchy.png -------------------------------------------------------------------------------- /src/main/java/org/example/designpatterns/factorymethod/Notification.java: -------------------------------------------------------------------------------- 1 | package org.example.designpatterns.factorymethod; 2 | 3 | public interface Notification { 4 | void notifyUser(); 5 | } 6 | -------------------------------------------------------------------------------- /src/main/java/org/example/threads/README.md: -------------------------------------------------------------------------------- 1 | # Threads 2 | 3 | This subpackage demonstrates the basic use of `Threads` in Java. 4 | 5 | And discussed how executing code on a different thread can affect the result of the program. -------------------------------------------------------------------------------- /src/main/java/org/example/enums/Day.java: -------------------------------------------------------------------------------- 1 | package org.example.enums; 2 | 3 | public enum Day { 4 | SUNDAY, 5 | MONDAY, 6 | TUESDAY, 7 | WEDNESDAY, 8 | THURSDAY, 9 | FRIDAY, 10 | SATURDAY; 11 | } 12 | -------------------------------------------------------------------------------- /src/main/java/org/example/designpatterns/abstractfactory/Abstract-Factory-Pattern.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BimalRajGyawali/hacktoberfest-java/HEAD/src/main/java/org/example/designpatterns/abstractfactory/Abstract-Factory-Pattern.png -------------------------------------------------------------------------------- /src/main/java/org/example/designpatterns/abstractfactory/ComputerAbstractFactory.java: -------------------------------------------------------------------------------- 1 | package org.example.designpatterns.abstractfactory; 2 | 3 | public interface ComputerAbstractFactory { 4 | 5 | public Computer createComputer(); 6 | 7 | } -------------------------------------------------------------------------------- /src/main/java/org/example/compressiontechniques/huffmanencoder/Node.java: -------------------------------------------------------------------------------- 1 | package org.example.compressiontechniques.huffmanencoder; 2 | 3 | public class Node { 4 | Node left; 5 | Node right; 6 | int data; 7 | char c; 8 | 9 | } 10 | -------------------------------------------------------------------------------- /src/main/java/org/example/datastructures/linkedlist/Node.java: -------------------------------------------------------------------------------- 1 | package org.example.datastructures.linkedlist; 2 | 3 | // Node will be object in memory having data and next node reference (address) 4 | public class Node 5 | { 6 | int data; 7 | Node next; 8 | } 9 | -------------------------------------------------------------------------------- /src/main/java/org/example/generics/BoundedTypesExample.java: -------------------------------------------------------------------------------- 1 | package org.example.generics; 2 | 3 | class BoundedTypesExample { 4 | 5 | public void display() { 6 | System.out.println("This is a bounded type generics class."); 7 | } 8 | } -------------------------------------------------------------------------------- /src/main/java/org/example/designpatterns/factorymethod/SMSNotification.java: -------------------------------------------------------------------------------- 1 | package org.example.designpatterns.factorymethod; 2 | 3 | public class SMSNotification implements Notification { 4 | public void notifyUser(){ 5 | System.out.println("SMS notif"); 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /src/main/java/org/example/designpatterns/abstractfactory/ComputerFactory.java: -------------------------------------------------------------------------------- 1 | package org.example.designpatterns.abstractfactory; 2 | 3 | public class ComputerFactory { 4 | 5 | public static Computer getComputer(ComputerAbstractFactory factory){ 6 | return factory.createComputer(); 7 | } 8 | } -------------------------------------------------------------------------------- /src/main/java/org/example/designpatterns/factorymethod/EmailNotification.java: -------------------------------------------------------------------------------- 1 | package org.example.designpatterns.factorymethod; 2 | 3 | public class EmailNotification implements Notification { 4 | public void notifyUser() { 5 | System.out.println("Email notif"); 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /src/main/java/org/example/designpatterns/factorymethod/PushNotification.java: -------------------------------------------------------------------------------- 1 | package org.example.designpatterns.factorymethod; 2 | 3 | public class PushNotification implements Notification { 4 | public void notifyUser() { 5 | System.out.println("Push notif"); 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /src/main/java/org/example/bitmanipulation/MultiplyingByThreePointFive.java: -------------------------------------------------------------------------------- 1 | package org.example.bitmanipulation; 2 | class MultiplyingByThreePointFive { 3 | public static void main(String[] args) { 4 | int x = 5; 5 | int ans = (x<<1) + x + (x>>1);//will print 17 6 | System.out.println(ans); 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /src/main/java/org/example/bitmanipulation/UseOfBitwiseXor.java: -------------------------------------------------------------------------------- 1 | package org.example.bitmanipulation; 2 | public class UseOfBitwiseXor { 3 | public static void main(String[] args) { 4 | int operand1 = 15, operand2 = 27; 5 | int output = operand1 ^ operand2; 6 | System.out.println(output); 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /src/main/java/org/example/bitmanipulation/UseOfLeftShiftOperator.java: -------------------------------------------------------------------------------- 1 | package org.example.bitmanipulation; 2 | public class UseOfLeftShiftOperator { 3 | public static void main(String[] args) { 4 | int operand1 = 5 5 | int output = operand1 << 1; 6 | System.out.println(output);//will print 10 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /src/main/java/org/example/bitmanipulation/UseOfRightShiftOperator.java: -------------------------------------------------------------------------------- 1 | package org.example.bitmanipulation; 2 | public class UseOfRightShiftOperator { 3 | public static void main(String[] args) { 4 | int operand = 10 5 | int output = operand >> 1; 6 | System.out.println(output);//will print 5 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /src/main/java/org/example/enums/SchedulingApp.java: -------------------------------------------------------------------------------- 1 | package org.example.enums; 2 | 3 | public class SchedulingApp { 4 | public static void main(String args[]){ 5 | String str = "MONDAY"; 6 | Schedule schedule = new Schedule(); 7 | schedule.getRoutine(Day.valueOf(str)); 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /src/main/java/org/example/reflection/BestFruit.java: -------------------------------------------------------------------------------- 1 | package org.example.reflection; 2 | 3 | public class BestFruit { 4 | 5 | private String name; 6 | 7 | public BestFruit(String name) { 8 | this.name = name; 9 | } 10 | 11 | public String getName() { 12 | return name; 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /src/main/java/org/example/basics/ClearBit.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | class ClearBit 3 | { 4 | public static void main(String args[]) 5 | { 6 | int n=5;//0101 7 | int pos=2; 8 | int bitmask=1< { 6 | public int compare(Node x, Node y) { 7 | 8 | return x.data - y.data; 9 | } 10 | 11 | } 12 | -------------------------------------------------------------------------------- /src/main/java/org/example/generics/GenericsMethodExample.java: -------------------------------------------------------------------------------- 1 | package org.example.generics; 2 | 3 | class GenericsMethodExample { 4 | 5 | // creae a generics method 6 | public void genericsMethod(T data) { 7 | System.out.println(" this is Generics Method:"); 8 | System.out.println("Data Passed: " + data); 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /src/main/java/org/example/lambda/Summable.java: -------------------------------------------------------------------------------- 1 | /* 2 | A functional interface can only have one abstract method. 3 | */ 4 | package org.example.lambda; 5 | 6 | /** 7 | * @author Narendra 8 | * @version 1.0 9 | * @since 2022-10-04 10 | */ 11 | @FunctionalInterface 12 | public interface Summable { 13 | Integer sum(Integer a, Integer b); 14 | } 15 | -------------------------------------------------------------------------------- /src/main/java/org/example/strings/StringLengthExample.java: -------------------------------------------------------------------------------- 1 | package org.example.strings; 2 | 3 | public class StringLengthExample{ 4 | 5 | public static void main(String args[]){ 6 | 7 | String txt = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; 8 | 9 | System.out.println(txt.length()); // the length of the string is 26 10 | 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /src/main/java/org/example/strings/StringLowerCaseExample.java: -------------------------------------------------------------------------------- 1 | package org.example.strings; 2 | 3 | public class StringLowerCaseExample{ 4 | 5 | public static void main(String args[]){ 6 | 7 | String s1="HELLO stRIng"; 8 | 9 | String s1lower=s1.toLowerCase(); 10 | 11 | System.out.println(s1lower); 12 | 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /src/main/java/org/example/strings/StringUpperCaseExample.java: -------------------------------------------------------------------------------- 1 | package org.example.strings; 2 | 3 | public class StringUpperCaseExample{ 4 | 5 | public static void main(String args[]){ 6 | 7 | String s1="hello string"; 8 | 9 | String s1upper=s1.toUpperCase(); 10 | 11 | System.out.println(s1upper); 12 | 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /src/main/java/org/example/algorithms/CelciusToFarenheitMethod.java: -------------------------------------------------------------------------------- 1 | public class CelciusToFarenheitMethod { 2 | static float result =0; 3 | static float conv(float n){ 4 | result = n*(9/5f)+32; 5 | return result; 6 | } 7 | 8 | public static void main(String[] args) { 9 | System.out.println(conv(10)); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /src/main/java/org/example/lambda/Performer.java: -------------------------------------------------------------------------------- 1 | /* 2 | A functional interface can only have one abstract method. 3 | */ 4 | package org.example.lambda; 5 | 6 | /** 7 | * @author Narendra 8 | * @version 1.0 9 | * @since 2022-10-04 10 | */ 11 | @FunctionalInterface 12 | public interface Performer { 13 | Integer performTask(Integer a, Integer b); 14 | } 15 | -------------------------------------------------------------------------------- /src/main/java/org/example/strings/CharReplaceInString.java: -------------------------------------------------------------------------------- 1 | package org.example.strings; 2 | class CharReplaceInString{ 3 | public static void main(String args[]){ 4 | String s1="Github is a very good Platform"; 5 | String replaceString=s1.replace('a','e');//replaces all occurrences of 'a' to 'e' 6 | System.out.println(replaceString); 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /src/main/java/org/example/strings/StringConcat.java: -------------------------------------------------------------------------------- 1 | package org.example.strings; 2 | class StringConcat{ 3 | public static void main(String args[]){ 4 | String s1="Virat "; 5 | String s2="Kohli"; 6 | //using concat() method 7 | String s3=s1.concat(s2); 8 | System.out.println(s3);//Virat Kohli will be printed 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /src/main/java/org/example/datastructures/queue/Node.java: -------------------------------------------------------------------------------- 1 | 2 | package org.example.datastructures.queue; 3 | public class Node 4 | { 5 | int data; 6 | Node next; 7 | 8 | public Node(int data) 9 | { 10 | this.data = data; 11 | next = null; 12 | } 13 | 14 | public String toString() 15 | { 16 | return "("+data+")"; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /src/main/java/org/example/oop/functions/Circumference.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | class Circumference 3 | { 4 | public static double circum(double r) 5 | { 6 | return 2*3.14*r; 7 | } 8 | public static void main(String args[]) 9 | { 10 | Scanner sc=new Scanner(System.in); 11 | double r=sc.nextDouble(); 12 | System.out.println(circum(r)); 13 | } 14 | } -------------------------------------------------------------------------------- /src/main/java/org/example/bitmanipulation/CountTotalSetBits.java: -------------------------------------------------------------------------------- 1 | package org.example.bitmanipulation; 2 | class CountTotalSetBits { 3 | public static void main(String args[]) 4 | { 5 | int i = 9; 6 | int count =0; 7 | while(i>0) 8 | { 9 | count += i&1; 10 | i= i>>1; 11 | } 12 | System.out.println(count); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /src/main/java/org/example/generics/GenericsClassExample.java: -------------------------------------------------------------------------------- 1 | package org.example.generics; 2 | 3 | class GenericsClassExample { 4 | 5 | // variable of T type 6 | private T data; 7 | 8 | public GenericsClassExample(T data) { 9 | this.data = data; 10 | } 11 | 12 | // method that return T type variable 13 | public T getData() { 14 | return this.data; 15 | } 16 | } -------------------------------------------------------------------------------- /src/main/java/org/example/strings/StringTrimExample.java: -------------------------------------------------------------------------------- 1 | package org.example.strings; 2 | 3 | public class StringTrimExample{ 4 | 5 | public static void main(String args[]){ 6 | 7 | String s1=" hello string "; 8 | 9 | System.out.println(s1+"Hacktober Fest");//without trim() 10 | 11 | System.out.println(s1.trim()+"Hacktober Fest");//with trim() 12 | 13 | } 14 | 15 | } 16 | -------------------------------------------------------------------------------- /src/main/java/org/example/basics/CodeForFinal.java: -------------------------------------------------------------------------------- 1 | package org.example.basics; 2 | 3 | public class CodeForFinal { 4 | 5 | public static void main(String[] args) { 6 | //final keyword is use to define variable with constant value 7 | //so that we cannot change the value of the variable in future 8 | final int x = 10; 9 | //x = 20 is will give error 10 | 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /src/main/java/org/example/strings/StringPrintLines.java: -------------------------------------------------------------------------------- 1 | package org.example.strings; 2 | 3 | /** 4 | * @author rajeshp 5 | * @Date 10/15/22 6 | */ 7 | public class StringPrintLines { 8 | public static void main(String[] args) { 9 | 10 | String var ="Hacktoberfest\n2022\nis\nLive !!!"; 11 | 12 | //Prints var in separate lines 13 | var.lines().forEach(System.out::println); 14 | 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /src/main/java/org/example/strings/StringReplaceExample.java: -------------------------------------------------------------------------------- 1 | package org.example.strings; 2 | 3 | class StringReplaceExample{ 4 | public static void main(String args[]){ 5 | String s1="This is a new string"; 6 | 7 | //using replace() method 8 | String replacedString=s1.replace('is','was'); 9 | 10 | System.out.println(replacedString);// This was a new string will be printed 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /src/main/java/org/example/strings/SubstringExample.java: -------------------------------------------------------------------------------- 1 | package org.example.strings; 2 | import java.util.*; 3 | public class SubstringExample { 4 | 5 | public static void main(String[] args) { 6 | Scanner in = new Scanner(System.in); 7 | String S = in.next(); 8 | int start = in.nextInt(); 9 | int end = in.nextInt(); 10 | System.out.println(S.substring(start,end)); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /src/main/java/org/example/bitmanipulation/OppositeSigns.java: -------------------------------------------------------------------------------- 1 | package org.example.bitmanipulation; 2 | class OppositeSigns { 3 | public static void main(String[] args) { 4 | int x = 100; 5 | int y = -100; 6 | if ((x ^ y) < 0) 7 | { 8 | System.out.println("Signs are opposite"); //will print this 9 | } 10 | else 11 | { 12 | System.out.println("Signs are not opposite"); 13 | } 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /src/main/java/org/example/strings/StringReverse.java: -------------------------------------------------------------------------------- 1 | package org.example.strings; 2 | import java.util.*; 3 | public class StringReverse { 4 | public static void main(String[] args) { 5 | Scanner sc=new Scanner(System.in); 6 | String A=sc.next(); 7 | String B=""; 8 | for (int i = A.length()-1; i >=0; i--) { 9 | B=B+ A.charAt(i); 10 | } 11 | System.out.println(B); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /src/main/java/org/example/algorithms/treestore/README.md: -------------------------------------------------------------------------------- 1 | # Tree Conversion 2 | 3 | It is a method to store a `Node` or a `Tree` to an array so that it could be written to file and saved in the disk. 4 | 5 | ````java 6 | 7 + 3 * 2 7 | Node('+', 7, Node('*', 3 * 2)) 8 | 9 | // converted into a single array form 10 | [7, 3, 2, *, +] 11 | // now we can write the array to a file 12 | 13 | // revert the step back to get the original array 14 | ```` -------------------------------------------------------------------------------- /src/main/java/org/example/oop/functions/Vote.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | class Vote 3 | { 4 | public static boolean vote(int age) 5 | { 6 | if(age>18) 7 | {return true;} 8 | else 9 | {return false;} 10 | } 11 | public static void main(String args[]) 12 | { 13 | Scanner sc=new Scanner(System.in); 14 | int age=sc.nextInt(); 15 | System.out.println(vote(age)); 16 | } 17 | 18 | } -------------------------------------------------------------------------------- /src/main/java/org/example/streams/Readme.md: -------------------------------------------------------------------------------- 1 | Stream Example class demonstrates some key features of Java Stream class 2 | 3 | Key functionalities demonstrated in the example are 4 | 5 | - Filtering out only a particular field type 6 | - Finding minimum value 7 | - Finding maximum value 8 | - Grouping object by a field type 9 | - Finding sum of a field type 10 | - Compare dates and filter out objects based on the given condition 11 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /src/main/java/org/example/basics/SumOfTwoNumber.java: -------------------------------------------------------------------------------- 1 | package org.example.basics; 2 | 3 | class Main { 4 | 5 | public static void main(String[] args) { 6 | 7 | System.out.println("Enter two numbers"); 8 | int first = 10; 9 | int second = 20; 10 | 11 | System.out.println(first + " " + second); 12 | 13 | // add two numbers 14 | int sum = first + second; 15 | System.out.println("The sum is: " + sum); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /src/main/java/org/example/oop/functions/Table.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | class Table 3 | { 4 | public static void table(int n) 5 | { 6 | int mult=1; 7 | for(int i=1;i<=10;i++) 8 | { 9 | mult=n*i; 10 | System.out.println(n+"*"+i+"="+mult); 11 | 12 | }return; 13 | } 14 | public static void main(String args[]) 15 | { 16 | Scanner sc=new Scanner(System.in); 17 | int n=sc.nextInt(); 18 | table(n); 19 | } 20 | } -------------------------------------------------------------------------------- /src/main/java/org/example/designpatterns/abstractfactory/Computer.java: -------------------------------------------------------------------------------- 1 | package org.example.designpatterns.abstractfactory; 2 | 3 | public abstract class Computer { 4 | 5 | public abstract String getRAM(); 6 | public abstract String getHDD(); 7 | public abstract String getCPU(); 8 | 9 | @Override 10 | public String toString(){ 11 | return "RAM= "+this.getRAM()+", HDD="+this.getHDD()+", CPU="+this.getCPU(); 12 | } 13 | } -------------------------------------------------------------------------------- /src/main/java/org/example/basics/keith/README.md: -------------------------------------------------------------------------------- 1 | We are going to see what is keith number and write a program in Java to find if a given number is Keith Number or not. 2 | What is Keith Number? 3 | A positive n digit number X is called a Keith number if it is arranged in a special number sequence generated using its digits. The special sequence has first n terms as digits of x and other terms are recursively evaluated as the sum of previous n terms. For example, 197, 19, 742, 1537, etc. 4 | -------------------------------------------------------------------------------- /src/main/java/org/example/files/WriteToFile.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | class WriteToFile{ 4 | public static void main(String args[]){ 5 | try{ 6 | 7 | FileWriter f1 = new FileWriter("README.md"); 8 | Scanner sc = new Scanner(System.in); 9 | System.out.println("Enter string to write to file: "); 10 | String s = sc.nextLine(); 11 | f1.write(s); 12 | f1.close(); 13 | } 14 | catch(Exception e){ 15 | System.out.println(e); 16 | } 17 | } 18 | } -------------------------------------------------------------------------------- /src/main/java/org/example/leetcode/MoveZeroes.java: -------------------------------------------------------------------------------- 1 | package org.example.leetcode; 2 | 3 | public class MoveZeroes { 4 | public void moveZeroes(int[] nums) { 5 | 6 | int index = 0; 7 | 8 | for (int i = 0; i < nums.length; i++) { 9 | if (nums[i] != 0) { 10 | nums[index++] = nums[i]; 11 | } 12 | } 13 | 14 | for (int i = index; i < nums.length; i++) { 15 | nums[i] = 0; 16 | } 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /src/main/java/org/example/strings/StringSubstring.java: -------------------------------------------------------------------------------- 1 | package org.example.strings; 2 | 3 | public class StringSubstring { 4 | public static void main(String args[]) 5 | { 6 | 7 | // Initializing String 8 | String str = new String("Hello World.This is a Substring"); 9 | 10 | // using substring() to extract substring 11 | // returns ()geeksforgeeks 12 | 13 | System.out.println("The extracted substring is : "); 14 | System.out.println(str.substring(12)); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /src/main/java/org/example/basics/ReversedString.java: -------------------------------------------------------------------------------- 1 | package org.example.basics; 2 | 3 | public class ReversedString { 4 | 5 | public static void main(String[] args) { 6 | 7 | String myname = "Ghost"; 8 | String reversedStr = ""; 9 | 10 | for (int i = myname.length()-1;i>=0;i--) { 11 | 12 | reversedStr = reversedStr+myname.charAt(i); 13 | } 14 | System.out.println("Original "+myname); 15 | System.out.println("Reversed "+reversedStr); 16 | } 17 | 18 | 19 | 20 | 21 | } -------------------------------------------------------------------------------- /src/main/java/org/example/strings/StringStripExample.java: -------------------------------------------------------------------------------- 1 | package org.example.strings; 2 | 3 | /** 4 | * @author rajeshp 5 | * @Date 10/15/22 6 | */ 7 | public class StringStripExample { 8 | 9 | public static void main(String[] args) { 10 | 11 | String var =" HacktoberFest 2022 "; 12 | 13 | System.out.println(var.strip()); 14 | 15 | System.out.println(var.stripTrailing()); 16 | 17 | System.out.println(var.stripLeading()); 18 | 19 | 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /src/main/java/org/example/bitmanipulation/CheckKthBit.java: -------------------------------------------------------------------------------- 1 | package org.example.bitmanipulation; 2 | class CheckKthBit { 3 | public static void main(String args[]) 4 | { 5 | int n = 4; 6 | int k = 2; 7 | n = n>>k; 8 | int newNum = n&1; 9 | if(newNum==0) 10 | { 11 | System.out.println(k +" bit is not a Setbit"); 12 | } 13 | else{ 14 | System.out.println(k + " bit is a Setbit");//will execute 15 | } 16 | } 17 | } 18 | 19 | -------------------------------------------------------------------------------- /src/main/java/org/example/leetcode/ClimbingStairs.java: -------------------------------------------------------------------------------- 1 | package org.example.leetcode; 2 | public class ClimbingStairs { 3 | 4 | public static void main(String[] args) { 5 | int n = 3; 6 | int dp[] = new int[n+1]; 7 | 8 | 9 | dp[0] = 1; 10 | for(int i = 1 ; i<=n; i++){ 11 | dp[i] = dp[i-1]; 12 | if(i-2 >=0) dp[i] += dp[i-2]; 13 | } 14 | 15 | System.out.print(dp[n]); 16 | } 17 | 18 | } 19 | -------------------------------------------------------------------------------- /src/main/java/org/example/oop/functions/Even.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | class Even 3 | { 4 | public static void even(int n) 5 | { 6 | if(n%2==0) 7 | {System.out.println("Number is even"); 8 | return;} 9 | else{ 10 | System.out.println("Number is odd"); 11 | return; 12 | } 13 | 14 | } 15 | public static void main(String args[]) 16 | { 17 | Scanner sc=new Scanner(System.in); 18 | int n=sc.nextInt(); 19 | even(n); 20 | } 21 | } 22 | 23 | -------------------------------------------------------------------------------- /src/main/java/org/example/basics/Armstrong.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | class Armstrong 3 | {public static void main(String args[]) 4 | { 5 | Scanner sc=new Scanner(System.in); 6 | int rem; 7 | System.out.println("The armstrong numbers are"); 8 | for(int j=1;j<=500;j++) 9 | {int x=0; 10 | for(int l=j;l!=0;l/=10) 11 | { 12 | rem=l%10; 13 | x=x+(rem*rem*rem); 14 | } 15 | if(x==j) 16 | {System.out.println(j);} 17 | } 18 | 19 | }} -------------------------------------------------------------------------------- /src/main/java/org/example/designpatterns/abstractfactory/PCFactory.java: -------------------------------------------------------------------------------- 1 | package org.example.designpatterns.abstractfactory; 2 | 3 | public class PCFactory implements ComputerAbstractFactory { 4 | 5 | private String ram; 6 | private String hdd; 7 | private String cpu; 8 | 9 | public PCFactory(String ram, String hdd, String cpu){ 10 | this.ram=ram; 11 | this.hdd=hdd; 12 | this.cpu=cpu; 13 | } 14 | @Override 15 | public Computer createComputer() { 16 | return new PC(ram,hdd,cpu); 17 | } 18 | 19 | } -------------------------------------------------------------------------------- /src/main/java/org/example/strings/StringBlankCheckExample.java: -------------------------------------------------------------------------------- 1 | package org.example.strings; 2 | 3 | /** 4 | * @author rajeshp 5 | * @Date 10/15/22 6 | */ 7 | public class StringBlankCheckExample { 8 | 9 | public static void main(String[] args) { 10 | 11 | String var =" "; 12 | 13 | //Checks if a string is empty 14 | System.out.println(var.isEmpty()); 15 | 16 | //Checks if a string is blank 17 | System.out.println(var.isBlank()); 18 | 19 | 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /src/main/java/org/example/basics/PatternPyramid.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | class PatternPyramid 3 | { 4 | public static void main(String args[]) 5 | { 6 | int n=5; 7 | for(int i=1;i<=n;i++) 8 | { 9 | for(int j=1;j<=(n-i);j++) 10 | { 11 | System.out.print(" "); 12 | } 13 | for(int j=1;j<=i;j++) 14 | { 15 | System.out.print(i+" "); 16 | }System.out.println(); 17 | } 18 | } 19 | } -------------------------------------------------------------------------------- /src/main/java/org/example/basics/PatternRhombus.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | class PatternRhombus 3 | { 4 | public static void main (String args[]) 5 | { 6 | int n=5; 7 | for(int i=1;i<=n;i++) 8 | { 9 | for(int j=1;j<=(n-i);j++) 10 | { 11 | System.out.print(" "); 12 | } 13 | for(int j=1;j<=n;j++) 14 | { 15 | System.out.print("*"); 16 | }System.out.println(); 17 | } 18 | } 19 | } -------------------------------------------------------------------------------- /src/main/java/org/example/datastructures/arrays/LargestNumOfAnArray.java: -------------------------------------------------------------------------------- 1 | package org.example.datastructures.arrays; 2 | public class LargestNumOfAnArray { 3 | 4 | public static void main(String[] args) { 5 | 6 | int[] numbers = {5, 9, 8, 3, 6, 10, 15}; // create an integer array and called it as numbers 7 | int largest = numbers[0]; 8 | 9 | for (int num : numbers) { 10 | if (largest < num) 11 | largest = num; 12 | } 13 | System.out.println("Largest Number of the Array " + largest); 14 | 15 | } 16 | } -------------------------------------------------------------------------------- /src/main/java/org/example/basics/Reversesb.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | class Reversesb 3 | { 4 | public static void main(String args[]) 5 | { 6 | Scanner sc=new Scanner(System.in); 7 | System.out.println("Enter the string"); 8 | String s=sc.nextLine(); 9 | StringBuilder sb=new StringBuilder(s); 10 | String c=""; 11 | int l=sb.length(); 12 | for(int i=0;in2) { 12 | n1 = n1 - n2; 13 | } else { 14 | n2 = n2 - n1; 15 | } 16 | } 17 | System.out.println("GCD is : "+ n2); 18 | } 19 | 20 | } 21 | -------------------------------------------------------------------------------- /src/main/java/org/example/designpatterns/abstractfactory/ServerFactory.java: -------------------------------------------------------------------------------- 1 | package org.example.designpatterns.abstractfactory; 2 | 3 | public class ServerFactory implements ComputerAbstractFactory { 4 | 5 | private String ram; 6 | private String hdd; 7 | private String cpu; 8 | 9 | public ServerFactory(String ram, String hdd, String cpu){ 10 | this.ram=ram; 11 | this.hdd=hdd; 12 | this.cpu=cpu; 13 | } 14 | 15 | @Override 16 | public Computer createComputer() { 17 | return new Server(ram,hdd,cpu); 18 | } 19 | 20 | } -------------------------------------------------------------------------------- /src/main/java/org/example/switchexpression/Readme.md: -------------------------------------------------------------------------------- 1 | This Example class demonstrates new feature introduced for switch expression in Java 14 2 | 3 | Java 14 adds a new form of switch label “case L ->” which allows multiple constants per case and returns a value for the whole switch-case block so it can be used in expressions (switch expressions). And the yield keyword is used to return value from a switch expression. 4 | 5 | For background information about the design of switch expressions, see [JEP 361](https://openjdk.java.net/jeps/361). 6 | -------------------------------------------------------------------------------- /src/main/java/org/example/algorithms/FactorialNNumber.java: -------------------------------------------------------------------------------- 1 | package org.example.algorithms; 2 | public class FactorialNNumber { 3 | 4 | public static int factorial(int n) 5 | { 6 | 7 | if(n == 0) 8 | { 9 | return 1; 10 | } 11 | 12 | int factorialNM1 = factorial(n-1); //---> factorial(n-1); 13 | 14 | int factorialN = n * factorialNM1; 15 | 16 | return factorialN; 17 | 18 | 19 | } 20 | public static void main(String[] args) { 21 | 22 | int n = 5; 23 | 24 | System.out.println( factorial(n)); 25 | 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /src/main/java/org/example/bitmanipulation/GetFirstSetBit.java: -------------------------------------------------------------------------------- 1 | package org.example.bitmanipulation; 2 | class CheckKthBit { 3 | public static void main(String args[]) { 4 | int count = 1; 5 | int n = 18; 6 | while (n != 0) { 7 | int newn = n & 1; 8 | if (newn == 0) { 9 | count++; 10 | } 11 | else { 12 | System.out.println(count);//2 wil be printed as 18 is 010010,the first set bit from the right side is at position 2. 13 | break; 14 | } 15 | n = n >> 1; 16 | } 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /src/main/java/org/example/datastructures/arrays/array.java: -------------------------------------------------------------------------------- 1 | class StudentMarksArray 2 | { 3 | public static void main(String arg[]) 4 | { 5 | int student_marks[] = new int[5]; 6 | student_marks[2] = 25; // LINE A 7 | System.out.println("Third Element = " + student_marks[2]); // LINE B 8 | System.out.println("Fourth Element = " + student_marks[3]); // LINE C 9 | 10 | // student_marks[-3] = 45; // Won't work // LINE D 11 | // student_marks[5] = 32; // Won't work // LINE E 12 | 13 | } 14 | } -------------------------------------------------------------------------------- /src/main/java/org/example/designpatterns/factorymethod/NotificationFactory.java: -------------------------------------------------------------------------------- 1 | package org.example.designpatterns.factorymethod; 2 | 3 | public class NotificationFactory { 4 | Notification createNotification(String type) { 5 | switch (type) { 6 | case "SMS": 7 | return new SMSNotification(); 8 | case "Email": 9 | return new EmailNotification(); 10 | case "Push": 11 | return new PushNotification(); 12 | } 13 | return null; 14 | } 15 | } -------------------------------------------------------------------------------- /src/main/java/org/example/deflate/README.md: -------------------------------------------------------------------------------- 1 | # Deflate 2 | 3 | Deflate is one of the popular loss-less compression algorithm, it combines the use of Huffman Coding techniques and LZ77. 4 |
5 | 6 | Read more about it: [Wikipedia](https://en.wikipedia.org/wiki/Deflate) 7 | 8 |
9 | 10 | Java provides us with APIs to work with Deflate compression and decompression. 11 | 12 | `Deflater` - Takes in the given input to produce output `byte[]`. 13 |
14 | `Inflater` - Similarly, it takes in the compressed input and produces decompressed or original data `byte[]`. -------------------------------------------------------------------------------- /src/main/java/org/example/designpatterns/factorymethod/Test.java: -------------------------------------------------------------------------------- 1 | package org.example.designpatterns.factorymethod; 2 | 3 | public class Test { 4 | public static void main(String[] args) { 5 | NotificationFactory nf = new NotificationFactory(); 6 | Notification sms = nf.createNotification("SMS"); 7 | Notification email = nf.createNotification("Email"); 8 | Notification push = nf.createNotification("Push"); 9 | sms.notifyUser(); 10 | email.notifyUser(); 11 | push.notifyUser(); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /src/main/java/org/example/leetcode/SingleNumber.java: -------------------------------------------------------------------------------- 1 | package org.example.leetcode; 2 | 3 | public class SingleNumber { 4 | public int singleNumber(int[] nums) { 5 | 6 | int lone = 0; 7 | 8 | for(int i : nums){ 9 | lone ^= i; 10 | } 11 | 12 | return lone; 13 | } 14 | 15 | public static void main(String ... args) { 16 | SingleNumber single = new SingleNumber(); 17 | int nums[] = {2,2,1}; 18 | String b = "aab"; 19 | System.out.println(single.singleNumber(nums)); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /src/main/java/org/example/patterns/CharacterPattern.java: -------------------------------------------------------------------------------- 1 | package org.example.patterns; 2 | 3 | public class CharacterPattern { 4 | 5 | public static void main(String[] args) { 6 | int n = 4; 7 | char ch = 'A'; 8 | 9 | for(int line = 1; line <= n ; line++) 10 | { 11 | for(int charcter=1; charcter <= line ; charcter++) 12 | { 13 | System.out.print(ch); 14 | ch++; 15 | } 16 | System.out.println(); 17 | } 18 | 19 | } 20 | 21 | } 22 | 23 | // Output: 24 | // Enter the numbers of rows:4 25 | // A 26 | // BC 27 | // DEF 28 | // GHIJ 29 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | HELP.md 2 | target/ 3 | !.mvn/wrapper/maven-wrapper.jar 4 | !**/src/main/**/target/ 5 | !**/src/test/**/target/ 6 | 7 | ### STS ### 8 | .apt_generated 9 | .classpath 10 | .factorypath 11 | .project 12 | .settings 13 | .springBeans 14 | .sts4-cache 15 | 16 | ### IntelliJ IDEA ### 17 | .idea 18 | *.iws 19 | *.iml 20 | *.ipr 21 | 22 | ### NetBeans ### 23 | /nbproject/private/ 24 | /nbbuild/ 25 | /dist/ 26 | /nbdist/ 27 | /.nb-gradle/ 28 | build/ 29 | !**/src/main/**/build/ 30 | !**/src/test/**/build/ 31 | 32 | ### VS Code ### 33 | .vscode/.DS_Store 34 | .DS_Store 35 | -------------------------------------------------------------------------------- /src/main/java/org/example/datastructures/queue/Queue.java: -------------------------------------------------------------------------------- 1 | /* 2 | * To change this license header, choose License Headers in Project Properties. 3 | * To change this template file, choose Tools | Templates 4 | * and open the template in the editor. 5 | */ 6 | package org.example.datastructures.queue; 7 | /** 8 | * 9 | * @author Zohaib Hassan Soomro 10 | */ 11 | public interface Queue { 12 | public Object first(); 13 | public Object remove(); 14 | public void add(Object obj); 15 | public int size(); 16 | public boolean isEmpty(); 17 | } 18 | -------------------------------------------------------------------------------- /src/main/java/org/example/exceptions/NullPointException.java: -------------------------------------------------------------------------------- 1 | /*When a Java application tries to process an object that has a null value, a NullPointerException is raised.*/ 2 | 3 | public class NullPointException { 4 | public static void main(String[] args) { 5 | Integer number = null; 6 | 7 | if (number > 0) { 8 | System.out.println("Positive number"); 9 | } 10 | } 11 | } 12 | 13 | /*A basic evaluation in the example above would fail since the number (Integer) object is null, 14 | resulting in a NullPointerException.*/ 15 | -------------------------------------------------------------------------------- /src/main/java/org/example/strings/StringEqualsIgnoreCase.java: -------------------------------------------------------------------------------- 1 | package org.example.strings; 2 | class StringEqualsIgnoreCase{ 3 | public static void main(String args[]){ 4 | String s1="Aditya"; 5 | String s2="Aditya"; 6 | String s3="AdiTya"; 7 | String s4="java"; 8 | System.out.println(s1.equalsIgnoreCase(s2));//true because content and case both are same 9 | System.out.println(s1.equalsIgnoreCase(s3));//true because case is ignored 10 | System.out.println(s1.equalsIgnoreCase(s4));//false because content is not same 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /src/main/java/org/example/collections/VectorExample.java: -------------------------------------------------------------------------------- 1 | package org.example.collections; 2 | 3 | import java.util.*; 4 | 5 | public class VectorExample{ 6 | 7 | public static void main(String args[]){ 8 | 9 | Vector v=new Vector(); 10 | 11 | v.add("Ayush"); 12 | 13 | v.add("Amit"); 14 | 15 | v.add("Ashish"); 16 | 17 | v.add("Garima"); 18 | 19 | Iterator itr=v.iterator(); 20 | 21 | while(itr.hasNext()){ 22 | 23 | System.out.println(itr.next()); 24 | 25 | } 26 | 27 | } 28 | 29 | } 30 | -------------------------------------------------------------------------------- /src/main/java/org/example/exceptions/StringIndexOutOfBoundsException.java: -------------------------------------------------------------------------------- 1 | // StringIndexOutOfBoundsException is caught 2 | package org.example.exception; 3 | 4 | public class StringIndexOutOfBoundsException { 5 | 6 | public static void main(String args[]) { 7 | try { 8 | String string = "Welcome to my world "; // length is 20 9 | char c = string.charAt(24); // accessing 25th element 10 | System.out.println("Char: "+c); 11 | } catch (StringIndexOutOfBoundsException e) { 12 | System.out.println("Exception StringIndexOutOfBoundsException is caught!!!"); 13 | } 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /src/main/java/org/example/strings/StringRepeatExample.java: -------------------------------------------------------------------------------- 1 | package org.example.strings; 2 | 3 | /** 4 | * @author rajeshp 5 | * @Date 10/15/22 6 | */ 7 | public class StringRepeatExample { 8 | 9 | public static void main(String[] args) { 10 | 11 | String var ="HacktoberFest 2022"; 12 | 13 | //Concatenate the var 2 times 14 | System.out.println(var.repeat(2)); 15 | 16 | //Prints empty string 17 | System.out.println(var.repeat(0)); 18 | 19 | //Print the var 20 | System.out.println(var.repeat(1)); 21 | 22 | 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /src/main/java/org/example/basics/ReverseArray.java: -------------------------------------------------------------------------------- 1 | 2 | package org.example.basics; 3 | import java.util.Arrays; 4 | 5 | 6 | public class ReverseArray { 7 | public static void main(String[] args) { 8 | int[] arr = {1,2,3,4,5}; 9 | int start = 0; 10 | int end = arr.length - 1; 11 | while(start<=end){ 12 | int temp = arr[start]; 13 | arr[start] = arr[end]; 14 | arr[end] = temp; 15 | start++; 16 | end--; 17 | } 18 | System.out.println(Arrays.toString(arr)); 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /src/main/java/org/example/oop/functions/Prime.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | class Prime 3 | { 4 | public static void prime(int n) 5 | { 6 | int c=0; 7 | for(int i=1;i<=n;i++) 8 | { 9 | if(n%i==0) 10 | {c++;} 11 | } 12 | if(c==2) 13 | {System.out.println("Number is prime"); 14 | return;} 15 | else{ 16 | System.out.println("Number is not prime"); 17 | return; 18 | } 19 | } 20 | public static void main(String args[]) 21 | { 22 | Scanner sc=new Scanner (System.in); 23 | int n=sc.nextInt(); 24 | prime(n); 25 | } 26 | } -------------------------------------------------------------------------------- /src/main/java/org/example/datastructures/stack/Stack.java: -------------------------------------------------------------------------------- 1 | /* 2 | * To change this license header, choose License Headers in Project Properties. 3 | * To change this template file, choose Tools | Templates 4 | * and open the template in the editor. 5 | */ 6 | package org.example.datastructures.stack; 7 | 8 | /** 9 | * 10 | * @author Zohaib Hassan Soomro 11 | */ 12 | public interface Stack { 13 | public Object peek(); 14 | public Object pop(); 15 | public void push(Object obj); 16 | public int size(); 17 | public boolean isEmpty(); 18 | 19 | } 20 | -------------------------------------------------------------------------------- /src/main/java/org/example/strings/StringInternExample.java: -------------------------------------------------------------------------------- 1 | package org.example.strings; 2 | 3 | public class StringInternExample{ 4 | 5 | public static void main(String args[]){ 6 | 7 | String s1=new String("hello"); 8 | 9 | String s2="hello"; 10 | 11 | String s3=s1.intern();//returns string from pool, now it will be same as s2 12 | 13 | System.out.println(s1==s2);//false because reference variables are pointing to different instance 14 | 15 | System.out.println(s2==s3);//true because reference variables are pointing to same instance 16 | 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /src/main/java/org/example/algorithms/backtracking/TilesProblem.java: -------------------------------------------------------------------------------- 1 | package org.example.algorithms.backtracking; 2 | public class TilesProblem{ 3 | public static int placeTiles(int n,int m) { 4 | if (n==m) { 5 | return 2; 6 | } 7 | if(njavac PrintHelloWorld.java 20 | 21 | C:\Java\bin>java PrintHelloWorld 22 | Hello World 23 | 24 | ========================================== */ -------------------------------------------------------------------------------- /src/main/java/org/example/strings/Readme.md: -------------------------------------------------------------------------------- 1 | String Example class demonstrates some new features of Java String class introduced in Java 11 2 | 3 | Key functionalities demonstrated in the example are 4 | 5 | - Repeat feature of String which allows concatenating a String with itself a given number of times 6 | - Blank which can we can check if a String instance is empty or contains whitespace 7 | - Strip which can be used to get rid of all leading and trailing whitespace from each String now 8 | - Lines Using this new method, we can easily split a String instance into a Stream of separate lines 9 | 10 | -------------------------------------------------------------------------------- /src/main/java/org/example/algorithms/recursion/Fabonacci.java: -------------------------------------------------------------------------------- 1 | package org.example.algorithms.recursion; 2 | 3 | public class Fabonacci { 4 | public static void printFib(int a,int b,int n) { 5 | if(n==0){ 6 | return; 7 | } 8 | int c=a+b; 9 | System.out.println(c); 10 | // a=b; 11 | // b=c; 12 | printFib(b, c, n-1); 13 | } 14 | public static void main(String args[]) { 15 | int a=0,b=1; 16 | System.out.println(a); 17 | System.err.println(b); 18 | int n=7; 19 | printFib(a, b, n-2); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /src/main/java/org/example/basics/Permutation.java: -------------------------------------------------------------------------------- 1 | package org.example.basics; 2 | public class Permutation { 3 | public static void printper(String str , String ans) { 4 | if (str.length()==0) { 5 | System.out.println(ans); 6 | return; 7 | } 8 | 9 | for (int i = 0; i < str.length(); i++) { 10 | char current= str.charAt(i); 11 | String newstr = str.substring(0, i)+str.substring(i+1); 12 | printper(newstr, ans+current); 13 | } 14 | } 15 | public static void main(String[] args) { 16 | String s = "abc"; 17 | printper(s, ""); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /src/main/java/org/example/bitmanipulation/OddEvenUsingBitManipulation.java: -------------------------------------------------------------------------------- 1 | package org.example.bitmanipulation; 2 | class OddEvenUsingBitManipulation 3 | { 4 | 5 | // Returns true if n is even, else odd 6 | public static boolean isEven(int n) 7 | { 8 | 9 | // n^1 is n+1, then even, else odd 10 | if ((n ^ 1) == n + 1) 11 | return true; 12 | else 13 | return false; 14 | } 15 | 16 | 17 | public static void main(String[] args) 18 | { 19 | int n = 100; 20 | System.out.print(isEven(n) == true ? "Even" : "Odd"); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /src/main/java/org/example/collections/ListExample.java: -------------------------------------------------------------------------------- 1 | package org.example.collections; 2 | 3 | import java.util.Arrays; 4 | import java.util.List; 5 | 6 | public class ListExample { 7 | 8 | public static void main(String[] args) { 9 | List jvmLanguages = List.of("Java", "Kotlin", "Groovy"); 10 | 11 | jvmLanguages.forEach(System.out::println); 12 | System.out.println(jvmLanguages.size()); 13 | 14 | List javaFrameworks = Arrays.asList("Spring", "Micronaut", "Quarkus", "Javalin"); 15 | 16 | javaFrameworks.forEach(System.out::println); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /src/main/java/org/example/lambda/README.md: -------------------------------------------------------------------------------- 1 | Lambda expression provides implementation of functional interface. 2 | An interface which has only one abstract method is called functional interface. 3 | Java provides an annotation `@FunctionalInterface`, which is used to declare an interface as functional interface.

4 | 5 | **Why use Lambda expressions?**
6 | 1. To provide the implementation of Functional interface. 7 | 2. Less coding 8 |

9 | 10 |

Syntax

11 | `(argument-list) -> {body}` 12 |

13 | Java lambda expression is treated as a function, so compiler does not create _**.class**_ file. -------------------------------------------------------------------------------- /src/main/java/org/example/basics/UpdateBit.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | class UpdateBit 3 | { 4 | public static void main(String args[]) 5 | { 6 | Scanner sc=new Scanner(System.in); 7 | int n=5;//0101 8 | int pos=1; 9 | int oper=sc.nextInt();//update bit ot 1 else update bit to 0 10 | //oper=1:set oper=0:clear 11 | int bitmask=1< array[i+1]) 15 | { 16 | return false; 17 | } 18 | 19 | return arraySorted(array, i+1); 20 | 21 | 22 | 23 | } 24 | public static void main(String[] args) { 25 | 26 | int array[] = {2,4,9,8}; 27 | 28 | System.out.println(arraySorted(array, 0)); 29 | 30 | } 31 | 32 | 33 | } 34 | -------------------------------------------------------------------------------- /src/main/java/org/example/basics/Pascal.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | class Pascal 3 | { 4 | public static void main(String args[]) 5 | { 6 | Scanner sc=new Scanner(System.in); 7 | System.out.println("Enter the number of rows"); 8 | int n=sc.nextInt(); 9 | for(int i=0;i<=n;i++) 10 | { 11 | for(int s=1;s<=n-i;s++) 12 | {System.out.print(" "); 13 | } 14 | int num=1; 15 | for(int j=0;j<=i;j++) 16 | { 17 | System.out.print(num+" "); 18 | num=num*(i-j)/(j+1); 19 | }System.out.println(); 20 | } 21 | } 22 | } -------------------------------------------------------------------------------- /src/main/java/org/example/algorithms/recursion/CalcPower.java: -------------------------------------------------------------------------------- 1 | package org.example.algorithms.recursion; 2 | 3 | public class CalcPower { 4 | public static int calcPower(int x,int n) { 5 | if(n==0)//base case 1 6 | { 7 | return 1; 8 | } 9 | if(x==0){//base case 2 10 | return 0; 11 | } 12 | int xPownm1=calcPower(x, n-1);//kaam 13 | int xPown=x*xPownm1; 14 | return xPown; 15 | } 16 | public static void main(String args[]) { 17 | int x=2,n=5; 18 | int ans=calcPower(x, n); 19 | System.out.println(ans); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /src/main/java/org/example/basics/FactorsOfANumber.java: -------------------------------------------------------------------------------- 1 | package org.example.basics; 2 | // A program to display the factors of the given number 3 | 4 | 5 | 6 | import java.util.Scanner; // importing Scanner for taking input 7 | 8 | 9 | class FactorsOfANumber{ 10 | public static void main(String[] args) { 11 | Scanner in = new Scanner(System.in); 12 | int num = in.nextInt(); 13 | System.out.println("The factors of "+num +" are : " ); 14 | for(int i = 1; i<=num; i++){ 15 | if(num%i == 0){ 16 | System.out.print(i+" "); 17 | } 18 | } 19 | in.close(); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /src/main/java/org/example/basics/PatternPalinPyramid.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | class PatternPalinPyramid 3 | { 4 | public static void main(String args[]) 5 | { 6 | int n=5; 7 | for(int i=1;i<=n;i++) 8 | { 9 | for(int j=1;j<=(n-i);j++) 10 | { 11 | System.out.print(" "); 12 | } 13 | for(int j=i;j>=1;j--) 14 | { 15 | System.out.print(j); 16 | } 17 | for(int j=2;j<=i;j++) 18 | { 19 | System.out.print(j); 20 | }System.out.println(); 21 | } 22 | } 23 | } -------------------------------------------------------------------------------- /src/main/java/org/example/designpatterns/abstractfactory/TestDesignPatterns.java: -------------------------------------------------------------------------------- 1 | package org.example.designpatterns.abstractfactory; 2 | 3 | public class TestDesignPatterns { 4 | 5 | public static void main(String[] args) { 6 | testAbstractFactory(); 7 | } 8 | 9 | private static void testAbstractFactory() { 10 | Computer pc = ComputerFactory.getComputer(new PCFactory("2 GB","500 GB","2.4 GHz")); 11 | Computer server = ComputerFactory.getComputer(new ServerFactory("16 GB","1 TB","2.9 GHz")); 12 | System.out.println("AbstractFactory PC Config::"+pc); 13 | System.out.println("AbstractFactory Server Config::"+server); 14 | } 15 | } -------------------------------------------------------------------------------- /src/main/java/org/example/oop/abstraction/TestInterface2.java: -------------------------------------------------------------------------------- 1 | package org.example.oop.abstraction; 2 | 3 | interface Bank{ 4 | float rateOfInterest(); 5 | } 6 | class SBI implements Bank{ 7 | public float rateOfInterest(){return 9.15f;} 8 | } 9 | class PNB implements Bank{ 10 | public float rateOfInterest(){return 9.7f;} 11 | } 12 | public class TestInterface2 { 13 | public static void main(String[] args) { 14 | SBI b = new SBI(); 15 | System.out.println("ROI: " + b.rateOfInterest()); 16 | PNB c = new PNB(); 17 | System.out.println("ROI: " + c.rateOfInterest()); 18 | } 19 | } 20 | 21 | -------------------------------------------------------------------------------- /src/main/java/org/example/oop/functions/Factorial.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | class Factorial 3 | { 4 | public static void factorial(int n) 5 | { int mult=1; 6 | if(n>0) 7 | for(int i=1;i<=n;i++) 8 | { 9 | mult*=i; 10 | } 11 | else 12 | { 13 | System.out.println("Invalid input"); 14 | return; 15 | } 16 | System.out.println("Factorial of number is "+mult); 17 | return; 18 | } 19 | public static void main(String args[]) 20 | { 21 | Scanner sc=new Scanner(System.in); 22 | int n=sc.nextInt(); 23 | factorial(n); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /src/main/java/org/example/leetcode/ValidAnagram.java: -------------------------------------------------------------------------------- 1 | package org.example.leetcode; 2 | 3 | import java.util.Arrays; 4 | 5 | public class ValidAnagram { 6 | public boolean isAnagram(String s, String t) { 7 | int[] str=new int[26]; 8 | int[] temp=new int[26]; 9 | for(int i=0;i set=new HashSet(); //Creating HashSet and adding elements 10 | 11 | set.add("Kirna"); 12 | 13 | set.add("Vijay"); 14 | 15 | set.add("Kirna"); 16 | 17 | set.add("Ajay"); 18 | 19 | Iterator itr=set.iterator(); //Traversing elements 20 | 21 | while(itr.hasNext()){ 22 | 23 | System.out.println(itr.next()); 24 | 25 | } 26 | 27 | } 28 | 29 | } 30 | -------------------------------------------------------------------------------- /src/main/java/org/example/bitmanipulation/LongestConsectiveOne.java: -------------------------------------------------------------------------------- 1 | package org.example.bitmanipulation; 2 | class LongestConsectiveOne { 3 | public static void main(String args[]) 4 | { 5 | int max = 0; 6 | int count = 0; 7 | int N = 14; 8 | while(N!=0) 9 | { 10 | int temp = N&1; 11 | if(temp == 1) 12 | { 13 | count+=1; 14 | if(count>max) 15 | { 16 | max = count; 17 | } 18 | } 19 | else 20 | { 21 | count =0; 22 | } 23 | N = N>>1; 24 | } 25 | System.out.println(count);//will print 3 as Binary representation of 14 is 1110 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /src/main/java/org/example/algorithms/FindingExtremeValues.java: -------------------------------------------------------------------------------- 1 | import java.util.Collections; 2 | import java.util.ArrayList; 3 | 4 | class Main { 5 | public static void main(String[] args) { 6 | // Creating an ArrayList 7 | ArrayList numbers = new ArrayList<>(); 8 | numbers.add(1); 9 | numbers.add(2); 10 | numbers.add(3); 11 | 12 | // Using min() 13 | int min = Collections.min(numbers); 14 | System.out.println("Minimum Element: " + min); 15 | 16 | // Using max() 17 | int max = Collections.max(numbers); 18 | System.out.println("Maximum Element: " + max); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /src/main/java/org/example/bitmanipulation/GetElementAppearingOnce.java: -------------------------------------------------------------------------------- 1 | package org.example.bitmanipulation; 2 | import java.util.*; 3 | public class GetElementAppearingOnce 4 | { 5 | public static void main(String[] args) 6 | { 7 | int n =4; 8 | int[] arr = new int[]{1, 10, 1, 1}; 9 | Arrays.sort(arr); 10 | for(int i =0;i=0){ 15 | if(row != col) { 16 | sum += mat[row][col];} 17 | row++; 18 | col--; 19 | } 20 | System.out.print(sum); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /src/main/java/org/example/patterns/HalfPyramid.java: -------------------------------------------------------------------------------- 1 | package org.example.patterns; 2 | 3 | import java.util.*; 4 | public class HalfPyramid { 5 | public static void main(String[] args){ 6 | 7 | // HALF PYRAMID 8 | System.out.print("Enter the numbers of rows:"); 9 | Scanner sc = new Scanner(System.in); 10 | int n= sc.nextInt(); 11 | 12 | for(int i=1; i<=n; i++){ 13 | for(int j=1; j<=i; j++){ 14 | System.out.print("*"); 15 | } 16 | System.out.println(); 17 | } 18 | } 19 | } 20 | 21 | // OUTPUT: 22 | // Enter the numbers of rows:5 23 | // * 24 | // ** 25 | // *** 26 | // **** 27 | // ***** -------------------------------------------------------------------------------- /src/main/java/org/example/basics/MethodDemo.java: -------------------------------------------------------------------------------- 1 | /* 2 | Write a method to add two numbers. 3 | */ 4 | package org.example.basics; 5 | 6 | class MethodDemo 7 | { 8 | 9 | static int add(int a,int b) // Method to add two numbers 10 | { 11 | int c; 12 | c=a+b; 13 | return c; 14 | } 15 | 16 | public static void main(String args[ ]) 17 | { 18 | System.out.println("Addition is : "+add(5,10)); 19 | } 20 | 21 | } 22 | 23 | /* ============ OUTPUT ============ 24 | 25 | C:\Java\bin>javac Methoddemo.java 26 | 27 | C:\Java\bin>java Methoddemo 28 | Addition is : 15 29 | 30 | ================================ */ 31 | 32 | -------------------------------------------------------------------------------- /src/main/java/org/example/collections/ArrayListExample.java: -------------------------------------------------------------------------------- 1 | package org.example.collections; 2 | 3 | import java.util.*; 4 | 5 | class ArrayListExample{ 6 | 7 | public static void main(String args[]){ 8 | 9 | ArrayList list=new ArrayList();//Creating arraylist 10 | 11 | list.add("Ravi");//Adding object in arraylist 12 | 13 | list.add("Vijay"); 14 | 15 | list.add("Ravi"); 16 | 17 | list.add("Ajay"); 18 | //Traversing list through Iterator 19 | 20 | Iterator itr=list.iterator(); 21 | 22 | while(itr.hasNext()){ 23 | 24 | System.out.println(itr.next()); 25 | 26 | } 27 | 28 | } 29 | 30 | } 31 | -------------------------------------------------------------------------------- /src/main/java/org/example/files/Files_api.java: -------------------------------------------------------------------------------- 1 | import java.io.IOException; 2 | import java.nio.file.Files; 3 | import java.nio.file.Path; 4 | import java.util.stream.Stream; 5 | 6 | public class Files_api { 7 | 8 | public static void main(String[] args) { 9 | 10 | Path filePath = Path.of( 11 | "" 12 | ); 13 | 14 | Stream stream = null; 15 | try { 16 | stream = Files.lines(filePath); 17 | } catch (IOException e) { 18 | e.printStackTrace(); 19 | } 20 | 21 | stream.forEach(System.out::println); 22 | 23 | } 24 | 25 | } 26 | -------------------------------------------------------------------------------- /src/main/java/org/example/designpatterns/abstractfactory/PC.java: -------------------------------------------------------------------------------- 1 | package org.example.designpatterns.abstractfactory; 2 | 3 | public class PC extends Computer { 4 | 5 | private String ram; 6 | private String hdd; 7 | private String cpu; 8 | 9 | public PC(String ram, String hdd, String cpu){ 10 | this.ram=ram; 11 | this.hdd=hdd; 12 | this.cpu=cpu; 13 | } 14 | @Override 15 | public String getRAM() { 16 | return this.ram; 17 | } 18 | 19 | @Override 20 | public String getHDD() { 21 | return this.hdd; 22 | } 23 | 24 | @Override 25 | public String getCPU() { 26 | return this.cpu; 27 | } 28 | 29 | } -------------------------------------------------------------------------------- /src/main/java/org/example/oop/classandobject/Student.java: -------------------------------------------------------------------------------- 1 | package org.example.oop.classandobject; 2 | 3 | //Let us see another example of default constructor 4 | //which displays the default values 5 | public class Student { 6 | int id; 7 | String name; 8 | 9 | //method to display the value of id and name 10 | void display() { 11 | System.out.println(id + " " + name); 12 | } 13 | 14 | public static void main(String[] args) { 15 | 16 | 17 | //creating objects 18 | Student s1 = new Student(); 19 | Student s2 = new Student(); 20 | 21 | //displaying values of the object 22 | s1.display(); 23 | s2.display(); 24 | } 25 | } -------------------------------------------------------------------------------- /src/main/java/org/example/patterns/NumberPyramid.java: -------------------------------------------------------------------------------- 1 | package org.example.patterns; 2 | 3 | import java.util.*; 4 | public class NumberPyramid { 5 | public static void main(String[] args){ 6 | 7 | // HALF PYRAMID WITH NUMBERS 8 | System.out.print("Enter the numbers of rows:"); 9 | Scanner sc = new Scanner(System.in); 10 | int n= sc.nextInt(); 11 | for(int i=1; i<=n; i++){ 12 | for(int j=1; j<=i; j++){ 13 | System.out.print(j); 14 | } 15 | System.out.println(); 16 | } 17 | } 18 | } 19 | 20 | // OUTPUT: 21 | // Enter the numbers of rows:5 22 | // 1 23 | // 12 24 | // 123 25 | // 1234 26 | // 12345 27 | -------------------------------------------------------------------------------- /src/main/java/org/example/compressiontechniques/huffmanencoder/OutputSingleton.java: -------------------------------------------------------------------------------- 1 | package org.example.compressiontechniques.huffmanencoder; 2 | 3 | public final class OutputSingleton { 4 | private static OutputSingleton INSTANCE; 5 | private String output = ""; 6 | 7 | private OutputSingleton() { 8 | } 9 | 10 | public static OutputSingleton getInstance() { 11 | if (INSTANCE == null) { 12 | INSTANCE = new OutputSingleton(); 13 | } 14 | 15 | return INSTANCE; 16 | } 17 | 18 | public void update(String c) { 19 | output = this.output + c; 20 | } 21 | 22 | public String getOutputString() { 23 | return output; 24 | } 25 | } -------------------------------------------------------------------------------- /src/main/java/org/example/basics/PowerOfTwo.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | class PowerOfTwo 3 | { 4 | public static void main(String args[]) 5 | { 6 | Scanner sc=new Scanner(System.in); 7 | System.out.println("Enter the number"); 8 | int n=sc.nextInt(); 9 | int f=0,c=0; 10 | int a[]=new int[32]; 11 | for(int i=n;i!=0;i/=2) 12 | { 13 | a[c++]=i%2; 14 | } 15 | for(int i=c-1;i>=0;i--) 16 | { 17 | System.out.print(a[i]); 18 | if(a[i]==1){f++;} 19 | }System.out.println(); 20 | if(a[c-1]==1&&f==1) 21 | {System.out.println("Yes the number is a power of two");} 22 | else{ 23 | System.out.println("No the number is not a power of two"); 24 | } 25 | } 26 | } -------------------------------------------------------------------------------- /src/main/java/org/example/basics/ValidPangram.java: -------------------------------------------------------------------------------- 1 | package org.example.basics; 2 | 3 | public class ValidPangram 4 | { 5 | public static boolean isPangram(String sentence) 6 | { 7 | Set unique = new HashSet<>(); 8 | for(int i=0; i=1; i--){ 13 | for(int j=i; j>=1; j--){ 14 | System.out.print("*"); 15 | } 16 | System.out.println(); 17 | } 18 | } 19 | } 20 | 21 | // OUTPUT: 22 | // Enter the numbers of rows:5 23 | // ***** 24 | // **** 25 | // *** 26 | // ** 27 | // * 28 | -------------------------------------------------------------------------------- /src/main/java/org/example/reflection/Student.java: -------------------------------------------------------------------------------- 1 | package org.example.reflection; 2 | 3 | /** 4 | * @author Narendra 5 | * @version 1.0 6 | * @since 2022-10-02 7 | */ 8 | public class Student { 9 | private Integer id; 10 | 11 | private String name; 12 | private String email; 13 | 14 | public String getName() { 15 | return name; 16 | } 17 | 18 | public void setName(String name) { 19 | this.name = name; 20 | } 21 | 22 | public void setId(Integer id) { 23 | this.id = id; 24 | } 25 | 26 | public void setEmail(String email) { 27 | this.email = email; 28 | } 29 | 30 | public void anotherMethod() { 31 | //code here 32 | } 33 | 34 | } 35 | -------------------------------------------------------------------------------- /src/main/java/org/example/functionalprogramming/SupplierExample.java: -------------------------------------------------------------------------------- 1 | package org.example.functionalprogramming; 2 | 3 | import java.util.List; 4 | import java.util.function.Supplier; 5 | 6 | public class SupplierExample { 7 | public static void main(String[] args) { 8 | System.out.println(getDBConnectionURL.get()); 9 | System.out.println(namesOfMonth.get()); 10 | } 11 | 12 | static Supplier getDBConnectionURL = () -> "jdbc://localhost:5432/users"; 13 | 14 | static Supplier> namesOfMonth = () -> 15 | List.of("January", "February", "March", "April", "May", "June", "July", 16 | "August", "September", "October", "November", "December"); 17 | 18 | } 19 | -------------------------------------------------------------------------------- /src/main/java/org/example/algorithms/recursion/KthGrammar.java: -------------------------------------------------------------------------------- 1 | package org.example.algorithms.recursion; 2 | 3 | public class KthGrammar { 4 | 5 | public static void main(String[] args){ 6 | kthGrammar(3,3); 7 | } 8 | public static int kthGrammar(int n, int k) { 9 | 10 | if(n==1 && k==1){ 11 | return 0; 12 | } 13 | int mid=(int)Math.pow(2,n-1)/2; 14 | if(k<=mid){ 15 | return kthGrammar(n-1,k); 16 | }else{ 17 | int l=kthGrammar(n-1,k-mid); 18 | if(l==0) { 19 | l=1; 20 | }else { 21 | l=0; 22 | } 23 | return l; 24 | } 25 | 26 | } 27 | 28 | 29 | } 30 | 31 | -------------------------------------------------------------------------------- /src/main/java/org/example/exceptions/ArrayIndexOutOfBound.java: -------------------------------------------------------------------------------- 1 | // Catching exception when array index is out of bound 2 | 3 | import java.util.Scanner; 4 | 5 | public class ArrayIndexOutOfBound { 6 | 7 | public static void main(String[] args) { 8 | 9 | int[] days = { 1, 2, 3, 4, 5, 6, 7 }; 10 | 11 | try { 12 | Scanner s = new Scanner(System.in); 13 | System.out.println("Enter valid day in terms of number(1-7): "); 14 | int a = s.nextInt(); 15 | s.close(); 16 | 17 | System.out.println("Your choose right day: " + days[a-1]); 18 | } 19 | 20 | catch (ArrayIndexOutOfBoundsException e) { 21 | System.out.println("Wrong input! " + e.getMessage()); 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /src/main/java/org/example/patterns/SolidRectangle.java: -------------------------------------------------------------------------------- 1 | package org.example.patterns; 2 | 3 | import java.util.*; 4 | public class SolidRectangle { 5 | public static void main(String[] args){ 6 | // SOLID RECTANGLE 7 | System.out.print("Enter no. of rows and columns:"); 8 | Scanner sc = new Scanner(System.in); 9 | int n= sc.nextInt(); 10 | int m = sc.nextInt(); 11 | 12 | for(int i=0; ijavac AddNaturalNumbersWithoutLoop.java 22 | 23 | C:\Java\bin>java AddNaturalNumbersWithoutLoop 10 24 | Sum of natural numbers from 1 to 10 is 55 25 | 26 | ==================================== */ 27 | 28 | 29 | -------------------------------------------------------------------------------- /src/main/java/org/example/leetcode/LongestValidParentheses.java: -------------------------------------------------------------------------------- 1 | package org.example.leetcode; 2 | 3 | public class LongestValidParentheses { 4 | public int longestValidParentheses(String s) { 5 | int[] dp = new int[s.length()]; 6 | int result = 0; 7 | int leftCount = 0; 8 | for (int i = 0; i < s.length(); i++) { 9 | if (s.charAt(i) == '(') { 10 | leftCount++; 11 | } else if (leftCount > 0){ 12 | dp[i] = dp[i - 1] + 2; 13 | dp[i] += (i - dp[i]) >= 0 ? dp[i - dp[i]] : 0; 14 | result = Math.max(result, dp[i]); 15 | leftCount--; 16 | } 17 | } 18 | return result; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /src/main/java/org/example/basics/UpperCase.java: -------------------------------------------------------------------------------- 1 | /* 2 | Write a program to check whether entered charecter is a upper case letter or not 3 | */ 4 | package org.example.basics; 5 | 6 | import java.util.*; 7 | public class UpperCase 8 | { 9 | 10 | public static void main(String args[ ]) 11 | { 12 | 13 | Scanner sc = new Scanner(System.in); 14 | char chr=sc.next().charAt(0); 15 | 16 | if(chr>='A' && chr<='Z') 17 | System.out.println("Upper Case Letter"); 18 | } 19 | 20 | } 21 | 22 | 23 | /* ============= OUTPUT =============== 24 | 25 | C:\Java\bin>javac UpperCase.java 26 | 27 | C:\Java\bin>java UpperCase 28 | A 29 | Upper Case Letter 30 | 31 | ==================================== */ 32 | -------------------------------------------------------------------------------- /src/main/java/org/example/collections/MapExample.java: -------------------------------------------------------------------------------- 1 | package org.example.collections; 2 | 3 | import java.util.Map; 4 | 5 | public class MapExample { 6 | 7 | /** 8 | * Small program to demonstrate the creation and functionality of a Map in Java. Displays 9 | * Key-Value pairs and can be initialized (immutable) via the static factory method {@link 10 | * Map#of(Object, Object, Object, Object)}. 11 | */ 12 | public static void main(String[] args) { 13 | Map phoneBook = Map.of("Alex", "+12345678", "Jim", "+98765432"); 14 | System.out.printf("Phonebook of size %d:%n", phoneBook.size()); 15 | phoneBook.forEach((String key, String value) -> System.out.printf("{%s: %s}%n", key, value)); 16 | } 17 | 18 | } 19 | -------------------------------------------------------------------------------- /src/main/java/org/example/compressiontechniques/huffmanencoder/README.md: -------------------------------------------------------------------------------- 1 | ### Huffman coding 2 | 3 | Huffman Coding is a lossless compression technique developed by David Huffman. 4 | 5 | The output from Huffman's algorithm can be viewed as a variable-length code table for encoding a source symbol (such as a character in a file). 6 | 7 | Huffman coding algorithm determines the optimal code using the minimum number of bits. 8 | where , shortest code is assigned to those characters that occuer most frequently. 9 | 10 | Example : 11 | Original String - > ABCCCCCCCCDEFGGG
12 | Huffman coding output -> C:0   F:100   A:1010   B:1011   G:110   D:1110   E:1111
13 | EncodedString - >101010110000000011101111100110110110 -------------------------------------------------------------------------------- /src/main/java/org/example/algorithms/recursion/TowerOfHanoi.java: -------------------------------------------------------------------------------- 1 | package org.example.algorithms.recursion; 2 | 3 | public class TowerOfHanoi { 4 | public static void towerOfHanoi(int n, String src, String helper, String dest) { 5 | if (n == 1) { 6 | System.out.println("Transfer disk " + n + " from " + src + " to " + dest); 7 | return; 8 | } 9 | towerOfHanoi(n - 1, src, dest, helper);//1st step 10 | System.out.println("Transfer disk " + n + " from " + src + " to " + dest);//2nd step 11 | towerOfHanoi(n - 1, helper, src, dest);//3rd step 12 | } 13 | 14 | public static void main(String args[]) { 15 | int n = 3; 16 | towerOfHanoi(n, "S", "H", "D"); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /src/main/java/org/example/basics/DecimalToBinary.java: -------------------------------------------------------------------------------- 1 | /* 2 | Write a program to convert given decimal number to equivalent binary number. 3 | */ 4 | 5 | package org.example.basics; 6 | 7 | class DecimalToBinary 8 | { 9 | 10 | public static void main(String args[ ]) 11 | { 12 | int num; 13 | num=Integer.parseInt(args[0]); 14 | String binnum=Integer.toBinaryString(num); 15 | 16 | System.out.println("Binary equivalent of "+num+" is "+binnum); 17 | } 18 | } 19 | 20 | /* ============ OUTPUT ============= 21 | 22 | C:\Java\bin>javac DecimalToBinary.java 23 | 24 | C:\Java\bin>java DecimalToBinary 45 25 | Binary equivalent of 45 is 101101 26 | 27 | ================================= */ 28 | -------------------------------------------------------------------------------- /src/main/java/org/example/basics/EvenUsingFor.java: -------------------------------------------------------------------------------- 1 | /* 2 | Write a program to print even numbers from 1 to 20 using loop. 3 | */ 4 | package org.example.basics; 5 | 6 | class EvenUsingFor 7 | { 8 | public static void main(String[] args) 9 | { 10 | int i; 11 | 12 | for(i=2;i<=20;i+=2) // Loop to generate even numbers between 1 to 20 13 | { 14 | System.out.println(i); 15 | } 16 | } 17 | } 18 | 19 | 20 | /* ============= OUTPUT ================ 21 | 22 | C:\Java\bin>javac EvenUsingFor.java 23 | 24 | C:\Java\bin>java EvenUsingFor 25 | 2 26 | 4 27 | 6 28 | 8 29 | 10 30 | 12 31 | 14 32 | 16 33 | 18 34 | 20 35 | 36 | ===================================== */ 37 | -------------------------------------------------------------------------------- /src/main/java/org/example/basics/Series.java: -------------------------------------------------------------------------------- 1 | /* 2 | Write a program to calculate the sum of following series where n is input by user. 3 | 1 + 1/2 + 1/3 + 1/4 + 1/5 +…………1/n 4 | */ 5 | 6 | package org.example.basics; 7 | 8 | public class Series 9 | { 10 | public static void main(String[] args) 11 | { 12 | int n=Integer.parseInt(args[0]); 13 | float sum=0; 14 | 15 | for(int i=1;i<=n;i++) 16 | { 17 | sum=sum+ 1.0f/i; 18 | } 19 | 20 | System.out.println(sum); 21 | } 22 | } 23 | 24 | 25 | /* =============== OUTPUT ============ 26 | 27 | C:\Java\bin>javac Series.java 28 | 29 | C:\Java\bin>java Series 3 30 | 1.8333334 31 | 32 | =================================== */ 33 | -------------------------------------------------------------------------------- /src/main/java/org/example/leetcode/MaximumSubArray.java: -------------------------------------------------------------------------------- 1 | package org.example.leetcode; 2 | class MaximumSubArray 3 | { 4 | public int maxSubArray(int[] nums) 5 | { 6 | int currSum = 0; 7 | int maxArrSum = nums[0]; 8 | for (int num : nums) 9 | { 10 | if (currSum < 0) 11 | { 12 | // ignoring the negatrive integers because they do not contribute anything 13 | currSum = 0; 14 | } 15 | currSum += num; 16 | // finding the maximum between max we calculated and the max we will get upon adding the next number in the array 17 | maxArrSum = Math.max(maxArrSum, currSum); 18 | } 19 | return maxArrSum; 20 | } 21 | } -------------------------------------------------------------------------------- /src/main/java/org/example/basics/EvenOdd.java: -------------------------------------------------------------------------------- 1 | /* 2 | Write a program to determine whether a entered number is even or odd. 3 | */ 4 | package org.example.basics; 5 | 6 | class EvenOdd 7 | { 8 | public static void main(String args[ ]) 9 | { 10 | int n=Integer.parseInt(args[0]); 11 | 12 | if(n%2==0) 13 | System.out.println("Entered number is even"); 14 | else 15 | System.out.println("Entered number is odd"); 16 | } 17 | } 18 | 19 | 20 | /* ============= OUTPUT =============== 21 | 22 | C:\Java\bin>javac EvenOdd.java 23 | 24 | C:\Java\bin>java EvenOdd 10 25 | Entered number is even 26 | 27 | C:\Java\bin>java EvenOdd 15 28 | Entered number is odd 29 | 30 | ==================================== */ 31 | -------------------------------------------------------------------------------- /src/main/java/org/example/basics/Transpose.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | class Transpose 3 | { 4 | public static void main(String args[]) 5 | { 6 | Scanner sc = new Scanner(System.in); 7 | int n = sc.nextInt(); 8 | int m = sc.nextInt(); 9 | 10 | int matrix[][] = new int[n][m]; 11 | for(int i=0; ilongestName.length()){ 17 | longestName=names[i]; 18 | } 19 | } 20 | return longestName; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /src/main/java/org/example/patterns/FloydsTriangle.java: -------------------------------------------------------------------------------- 1 | package org.example.patterns; 2 | 3 | import java.util.*; 4 | public class FloydsTriangle { 5 | public static void main(String[] args){ 6 | 7 | // FLOYD's TRIANGLE 8 | System.out.print("Enter the numbers of rows:"); 9 | Scanner sc = new Scanner(System.in); 10 | int n= sc.nextInt(); 11 | int num=1; 12 | for(int i=1; i<=n; i++){ 13 | for(int j =1; j<=i; j++){ 14 | System.out.print(num + " "); 15 | num++; 16 | } 17 | System.out.println(); 18 | } 19 | } 20 | } 21 | 22 | // OUTPUT: 23 | // Enter the numbers of rows:5 24 | // 1 25 | // 2 3 26 | // 4 5 6 27 | // 7 8 9 10 28 | // 11 12 13 14 15 29 | -------------------------------------------------------------------------------- /src/main/java/org/example/datastructures/queue/DequeDemo.java: -------------------------------------------------------------------------------- 1 | package org.example.datastructures.queue; 2 | import java.util.ArrayDeque; 3 | 4 | public class DequeDemo 5 | { 6 | public static void main(String[] args) 7 | { 8 | ArrayDeque dq=new ArrayDeque<>(); 9 | 10 | dq.addLast(10); 11 | dq.addLast(20); 12 | dq.addLast(30); 13 | dq.addFirst(40); 14 | dq.addFirst(50); 15 | dq.addFirst(60); 16 | 17 | System.out.println(dq); 18 | 19 | //dq.removeLast(); 20 | //dq.removeFirst(); 21 | 22 | while(! dq.isEmpty()) 23 | { 24 | dq.removeFirst(); 25 | } 26 | 27 | System.out.println(dq); 28 | } 29 | } 30 | 31 | -------------------------------------------------------------------------------- /src/main/java/org/example/oop/encapsulation/Areas.java: -------------------------------------------------------------------------------- 1 | package org.example.oop.encapsulation; 2 | 3 | public class Areas { 4 | 5 | // Encapsulation is a mechanism of wrapping code and data together in a single unit 6 | 7 | static class Area { 8 | int length; 9 | int breadth; 10 | // constructor to initialize values 11 | Area(int length, int breadth) { 12 | this.length = length; 13 | this.breadth = breadth; 14 | } 15 | public void getArea() { 16 | int area = length * breadth; 17 | System.out.println("Area: " + area); 18 | } 19 | } 20 | public static void main(String[] args) { 21 | Area rectangle = new Area(12, 5); 22 | rectangle.getArea(); 23 | } 24 | } -------------------------------------------------------------------------------- /src/main/java/org/example/basics/TwoDArray.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | class TwoDArray 3 | { 4 | public static void main(String args[]) 5 | { 6 | Scanner sc=new Scanner(System.in); 7 | int rows=sc.nextInt(); 8 | int cols=sc.nextInt(); 9 | int numbers[][]=new int[rows][cols]; 10 | //input 11 | for(int i=0;ijavac FibonacciSeries.java 26 | 27 | C:\Java\bin>java FibonacciSeries 10 28 | The 10 th fibonacci term is 34 29 | 30 | =================================== */ 31 | -------------------------------------------------------------------------------- /src/main/java/org/example/basics/LinearSearch.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | class LinearSearch 3 | { 4 | public static void main(String args[]) 5 | { Scanner sc=new Scanner(System.in); 6 | System.out.println("Enter the size"); 7 | int size=sc.nextInt(); 8 | int numbers[]=new int[size]; 9 | 10 | for(int i=0;ijavac Leapyear.java 25 | 26 | C:\Java\bin>java Leapyear 2000 27 | Leap Year 28 | 29 | C:\Java\bin>java Leapyear 2005 30 | Ordinary Year 31 | 32 | ================================ */ -------------------------------------------------------------------------------- /src/main/java/org/example/basics/ReverseDigits.java: -------------------------------------------------------------------------------- 1 | /* 2 | Write a program that prompts the user to input an integer and then outputs the number with the digits reversed. 3 | For example, if the input is 12345, the output should be 54321. 4 | 5 | */ 6 | 7 | package org.example.basics; 8 | 9 | class ReverseDigits 10 | { 11 | public static void main(String[] args) 12 | { 13 | int n=Integer.parseInt(args[0]); 14 | 15 | while(n>0) 16 | { 17 | System.out.print(n%10); 18 | n=n/10; 19 | } 20 | } 21 | } 22 | 23 | 24 | /* =================== OUTPUT ================= 25 | 26 | C:\Java\bin>javac ReverseDigits.java 27 | 28 | C:\Java\bin>java ReverseDigits 12345 29 | 54321 30 | 31 | ============================================ */ 32 | -------------------------------------------------------------------------------- /src/main/java/org/example/patterns/Descendingorder.java: -------------------------------------------------------------------------------- 1 | package org.example.patterns; 2 | 3 | public class Descendingorder { 4 | 5 | public static void main(String[] args) 6 | { 7 | Scanner sc = new Scanner(System.in); //Taking rows value from the user 8 | System.out.println("Enter the number of rows: "); 9 | int rows = sc.nextInt(); 10 | for (int i = 1; i <= rows; i++) 11 | { 12 | for (int j = 1; j <= i; j++) 13 | { 14 | System.out.print(i+" "); 15 | } 16 | 17 | System.out.println(); 18 | } 19 | sc.close(); 20 | } 21 | } 22 | //output 23 | //Enter the number of rows: 5 24 | 25 | //5 26 | //5 4 27 | //5 4 3 28 | //5 4 3 2 29 | //5 4 3 2 1 30 | -------------------------------------------------------------------------------- /src/main/java/org/example/patterns/RotatedHalfPyramid.java: -------------------------------------------------------------------------------- 1 | package org.example.patterns; 2 | 3 | import java.util.*; 4 | public class RotatedHalfPyramid { 5 | public static void main(String[] args){ 6 | 7 | // ROTATED HALF PYRAMID 8 | System.out.print("Enter the numbers of rows:"); 9 | Scanner sc = new Scanner(System.in); 10 | int n= sc.nextInt(); 11 | for(int i=1; i<=n; i++){ 12 | for(int j=1; j<=n-i; j++){ 13 | System.out.print(" "); 14 | } 15 | for(int j=1; j<=i; j++){ 16 | System.out.print("*"); 17 | } 18 | System.out.println(); 19 | } 20 | } 21 | } 22 | 23 | // OUTPUT: 24 | // Enter the numbers of rows:5 25 | // * 26 | // ** 27 | // *** 28 | // **** 29 | // ***** -------------------------------------------------------------------------------- /src/main/java/org/example/oop/statickeyword/JavaStaticKeyword.java: -------------------------------------------------------------------------------- 1 | package org.example.oop.statickeyword; 2 | public class JavaStaticKeyword { 3 | // Static method 4 | static void myStaticMethod() { 5 | System.out.println("Static methods can be called without creating objects"); 6 | } 7 | 8 | // Public method 9 | public void myPublicMethod() { 10 | System.out.println("Public methods must be called by creating objects"); 11 | } 12 | 13 | // Main method 14 | public static void main(String[ ] args) { 15 | myStaticMethod(); // Call the static method 16 | // myPublicMethod(); This would output an error 17 | 18 | JavaStaticKeyword myObj = new JavaStaticKeyword(); // Create an object of Main 19 | myObj.myPublicMethod(); // Call the public method 20 | } 21 | } -------------------------------------------------------------------------------- /src/main/java/org/example/designpatterns/singleton/README.md: -------------------------------------------------------------------------------- 1 |

Singleton design pattern

2 | Singleton design pattern comes under the category of creational design pattern. 3 | This pattern involves a single class which is responsible to create an object while making sure that only single object gets created. 4 | This class provides a way to access its only object which can be accessed directly without need to instantiate the object of the class. 5 |

6 |

Advantage

7 |
    8 |
  • Saves memory because object is not created at each request. Only single instance is reused again and again.
  • 9 |
10 |

Usage

11 |
    12 |
  • Singleton pattern is mostly used in multi-threaded and database applications. It is used in logging, caching, thread pools, configuration settings etc.
  • 13 |
-------------------------------------------------------------------------------- /src/main/java/org/example/compressiontechniques/runlengthencoder/README.md: -------------------------------------------------------------------------------- 1 | ### Run-length Encoding 2 | 3 | Run-length encoding (RLE) is a form of lossless data compression in which runs of data (sequences in which the same data value occurs in many consecutive data elements) are stored as a single data value and count, rather than as the original run . 4 |
5 | The reduced sequence of bytes is indicated by a flag byte, that is not the same as the byte being reduced. 6 | 7 | Original String   - >   ABCCCCCCCCDEFGGG 8 |
9 | EncodedString  - >   ABC!8DEFGGG 10 | 11 | In above example ,the byte (!) acts as the flag. 12 | 13 | Run length allows the compression of 4 to 259 bytes into three bytes .
14 | But variations of run-length allow it to be used for any length of bytes and without flags as well. 15 | 16 | -------------------------------------------------------------------------------- /src/main/java/org/example/basics/Tables.java: -------------------------------------------------------------------------------- 1 | /* 2 | Write a program to print table of entered number 3 | */ 4 | 5 | package org.example.basics; 6 | 7 | public class Tables 8 | { 9 | public static void main(String args[ ]) 10 | { 11 | int i,n=Integer.parseInt(args[0]); 12 | 13 | System.out.println("The table of "+n+" is as follows: "); 14 | for(i=1;i<=10;i++) 15 | System.out.println(n+" * "+i+" = "+(n*i)); 16 | } 17 | } 18 | 19 | 20 | /* ============== OUTPUT ============== 21 | 22 | C:\Java\bin>javac Tables.java 23 | 24 | C:\Java\bin>java Tables 5 25 | The table of 5 is as follows: 26 | 5 * 1 = 5 27 | 5 * 2 = 10 28 | 5 * 3 = 15 29 | 5 * 4 = 20 30 | 5 * 5 = 25 31 | 5 * 6 = 30 32 | 5 * 7 = 35 33 | 5 * 8 = 40 34 | 5 * 9 = 45 35 | 5 * 10 = 50 36 | 37 | ==================================== */ 38 | -------------------------------------------------------------------------------- /src/main/java/org/example/algorithms/recursion/JosephusUsingRecursion.java: -------------------------------------------------------------------------------- 1 | package org.example.algorithms.recursion; 2 | import java.util.*; 3 | class JosephusUsingRecursion{ 4 | static void Josh(List person, int k, int index) 5 | { 6 | if (person.size() == 1) { 7 | System.out.println(person.get(0));//will print 13 8 | return; 9 | } 10 | index = ((index + k) % person.size()); 11 | person.remove(index); 12 | // recursive call 13 | Josh(person, k, index); 14 | } 15 | public static void main(String [] args) 16 | { 17 | int n = 14; 18 | int k = 2; 19 | k--; // (k-1)th person will be killed 20 | int index = 0; 21 | List person = new ArrayList<>(); 22 | for (int i = 1; i <= n; i++) { 23 | person.add(i); 24 | } 25 | Josh(person, k, index); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /src/main/java/org/example/compressiontechniques/CompressionApp.java: -------------------------------------------------------------------------------- 1 | package org.example.compressiontechniques; 2 | 3 | import org.example.compressiontechniques.huffmanencoder.HuffmanEncoder; 4 | import org.example.compressiontechniques.runlengthencoder.RunLengthEncoder; 5 | 6 | public class CompressionApp { 7 | public static void main(String[] args) { 8 | String rawString = "ABCCCCCCCCDEFGGG"; 9 | RunLengthEncoder runLengthEncoder = new RunLengthEncoder(); 10 | String compressedStringByRunLength = runLengthEncoder.compress(rawString); 11 | System.out.println(compressedStringByRunLength); 12 | HuffmanEncoder huffmanEncoder = new HuffmanEncoder(); 13 | String compressedStringByHuffman = huffmanEncoder.compress(rawString); 14 | System.out.println(compressedStringByHuffman); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /src/main/java/org/example/algorithms/searching/LinearSearch.java: -------------------------------------------------------------------------------- 1 | package org.example.algorithms.searching; 2 | 3 | public class LinearSearch{ 4 | public static int search(int arr[], int x) 5 | { 6 | int N = arr.length; 7 | for (int i = 0; i < N; i++) { 8 | if (arr[i] == x) 9 | return i; 10 | } 11 | return -1; 12 | } 13 | 14 | // Driver's code 15 | public static void main(String args[]) 16 | { 17 | int arr[] = { 2, 3, 4, 10, 40 }; 18 | int x = 10; 19 | 20 | // Function call 21 | int result = search(arr, x); 22 | if (result == -1) 23 | System.out.print( 24 | "Element is not present in array"); 25 | else 26 | System.out.print("Element is present at index " 27 | + result); 28 | } 29 | } -------------------------------------------------------------------------------- /src/main/java/org/example/basics/PatternDiamond.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | class PatternDiamond 3 | { 4 | public static void main(String args[]) 5 | { 6 | int n=4; 7 | for(int i=1;i<=n;i++) 8 | { 9 | for(int j=1;j<=(n-i);j++) 10 | { 11 | System.out.print(" "); 12 | } 13 | for(int j=1;j<=((2*i)-1);j++) 14 | { 15 | System.out.print("*"); 16 | }System.out.println(); 17 | } 18 | for(int i=n;i>=1;i--) 19 | { 20 | for(int j=1;j<=(n-i);j++) 21 | { 22 | System.out.print(" "); 23 | } 24 | for(int j=1;j<=((2*i)-1);j++) 25 | { 26 | System.out.print("*"); 27 | }System.out.println(); 28 | } 29 | } 30 | } -------------------------------------------------------------------------------- /src/main/java/org/example/oop/polymorphism/CompileTimePolymorphism.java: -------------------------------------------------------------------------------- 1 | public class CompileTimePolymorphism { 2 | 3 | 4 | // Method with 2 int parameters 5 | static int multiply(int a, int b) 6 | { 7 | // Returns product of integer numbers 8 | return a * b; 9 | } 10 | 11 | // Method 2 12 | // With same name but with 2 double parameters 13 | static double multiply(double a, double b) 14 | { 15 | // Returns product of double numbers 16 | return a * b; 17 | } 18 | } 19 | 20 | class Application { 21 | 22 | public static void main(String[] args) { 23 | 24 | // Calling method by passing input as in arguments 25 | System.out.println(CompileTimePolymorphism.multiply(8,9)); 26 | System.out.println(CompileTimePolymorphism.multiply(2.5,4.2)); 27 | } 28 | } -------------------------------------------------------------------------------- /src/main/java/org/example/basics/SwapNumbers.java: -------------------------------------------------------------------------------- 1 | /* 2 | Write a program to swap two numbers. 3 | */ 4 | package org.example.basics; 5 | 6 | 7 | class SwapNumbers 8 | { 9 | public static void main(String[] args) 10 | { 11 | int a,b,temp; 12 | 13 | a=Integer.parseInt(args[0]); 14 | b=Integer.parseInt(args[1]); 15 | 16 | System.out.println("Numbers before swapping are a="+a+" b="+b); 17 | temp=a; 18 | a=b; 19 | b=temp; 20 | System.out.println("Numbers after swapping are a="+a+" b="+b); 21 | 22 | } 23 | } 24 | 25 | /* ============= OUTPUT ============= 26 | 27 | C:\Java\bin>javac SwapNumbers.java 28 | 29 | C:\Java\bin>java SwapNumbers 10 12 30 | Numbers before swapping are a=10 b=12 31 | Numbers after swapping are a=12 b=10 32 | 33 | ================================== */ 34 | -------------------------------------------------------------------------------- /src/main/java/org/example/designpatterns/prototype/PrototypePatternTest.java: -------------------------------------------------------------------------------- 1 | package org.example.designpatterns.prototype; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | public class PrototypePatternTest { 7 | 8 | public static void main(String[] args) throws CloneNotSupportedException { 9 | Employees emps = new Employees(); 10 | emps.loadData(); 11 | 12 | //Use the clone method to get the Employee object 13 | Employees empsNew = (Employees) emps.clone(); 14 | Employees empsNew1 = (Employees) emps.clone(); 15 | List list = empsNew.getEmpList(); 16 | list.add("John"); 17 | List list1 = empsNew1.getEmpList(); 18 | list1.remove("Pankaj"); 19 | 20 | System.out.println("emps List: "+emps.getEmpList()); 21 | System.out.println("empsNew List: "+list); 22 | System.out.println("empsNew1 List: "+list1); 23 | } 24 | } -------------------------------------------------------------------------------- /src/main/java/org/example/patterns/ReversedPyramidStarPattern.java: -------------------------------------------------------------------------------- 1 | package org.example.patterns; 2 | 3 | import java.util.Scanner; 4 | public class ReversedPyramidStarPattern 5 | { 6 | public static void main(String[] args) 7 | { 8 | Scanner sc = new Scanner(System.in); 9 | System.out.println("Enter the number of rows: "); 10 | 11 | int rows = sc.nextInt(); 12 | for (int i= 0; i<= rows-1 ; i++) 13 | { 14 | for (int j=0; j<=i; j++) 15 | { 16 | System.out.print(" "); 17 | } 18 | for (int k=0; k<=rows-1-i; k++) 19 | { 20 | System.out.print("*" + " "); 21 | } 22 | System.out.println(); 23 | } 24 | sc.close(); 25 | 26 | } 27 | } 28 | 29 | //output 30 | //Enter Number of Rows:5 31 | // * * * * * 32 | // * * * * 33 | // * * * 34 | // * * 35 | // * 36 | -------------------------------------------------------------------------------- /src/main/java/org/example/oop/abstraction/arrayofobjects/Area.java: -------------------------------------------------------------------------------- 1 | package org.example.oop.abstraction.arrayofobjects; 2 | 3 | public class Area extends Shape{ 4 | private final Float PI = 3.14F; 5 | //prints the area of rectangle with given input parameters 6 | @Override 7 | void rectangleArea(Integer length, Integer breadth) { 8 | System.out.printf("the area of rectangle is %d%n",(length*breadth)); 9 | } 10 | //prints the area of square with given input length value 11 | @Override 12 | void squareArea(Integer length) { 13 | System.out.printf("the area of square is %d%n ",(length*length)); 14 | } 15 | //prints the circle area with given input radius value 16 | @Override 17 | void circleArea(Integer radius) { 18 | System.out.printf("the area of circle is %f%n ",(float)(PI*radius*radius)); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /src/main/java/org/example/oop/polymorphism/RunTimePolymorphism.java: -------------------------------------------------------------------------------- 1 | public class RunTimePolymorphism { 2 | public static void main(String[] args) 3 | { 4 | // Creating object of class 1 5 | Parent a; 6 | 7 | // Now we will be calling print methods inside main() method 8 | 9 | a = new SubClass1(); 10 | a.display(); 11 | 12 | a = new SubClass2(); 13 | a.display(); 14 | } 15 | } 16 | 17 | class Parent { 18 | 19 | // Method of parent class 20 | void display() { System.out.println("Parent Class"); } 21 | } 22 | 23 | // Class 2 24 | class SubClass1 extends Parent { 25 | void display() { System.out.println("SubClass-1"); } 26 | } 27 | 28 | // Class 3 29 | class SubClass2 extends Parent { 30 | 31 | void display() { System.out.println("SubClass-2"); } 32 | } -------------------------------------------------------------------------------- /src/main/java/org/example/basics/ArrayMinMax.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class ArrayMinMax { 4 | public static void main(String args[]) { 5 | Scanner sc = new Scanner(System.in); 6 | int size = sc.nextInt(); 7 | int numbers[] = new int[size]; 8 | 9 | //input 10 | for(int i=0; i max) { 22 | max = numbers[i]; 23 | } 24 | } 25 | System.out.println("Largest number is : " + max); 26 | System.out.println("Smallest number is : " + min); 27 | sc.close(); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/main/java/org/example/leetcode/TrappingRainWater.java: -------------------------------------------------------------------------------- 1 | package org.example.leetcode; 2 | /** 3 | * TrappingRainWater 4 | * https://leetcode.com/problems/trapping-rain-water/ 5 | */ 6 | public class TrappingRainWater { 7 | int trap(int[] height){ 8 | int n = height.length, ans = 0, maxi = 0, mini = 0; 9 | // two pointers 10 | int i = 0, j = n - 1; 11 | // pointer i from the left 12 | // pointer j from the right 13 | while(i <= j){ 14 | mini = Math.min(height[i], height[j]); 15 | maxi = Math.max(maxi, mini); 16 | 17 | // the units of water being tapped is the diffence between max height and min height 18 | ans += maxi - mini; 19 | if(height[i] < height[j])i++; 20 | else j--; 21 | } 22 | return ans; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /src/main/java/org/example/basics/GuessNumber.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | public class GuessNumber { 3 | public static void main(String args[]) 4 | { 5 | Scanner sc=new Scanner(System.in); 6 | int myNumber=(int)(Math.random()*100); 7 | int usernumber=0; 8 | do 9 | { 10 | System.out.println("Guess my number (1-100):"); 11 | usernumber=sc.nextInt(); 12 | if(usernumber==myNumber) 13 | { 14 | System.out.println("WOOHOO..Correct Number"); 15 | break; 16 | } 17 | else if(usernumber>myNumber) 18 | { 19 | System.out.println("Your number is too large"); 20 | } 21 | else 22 | { 23 | System.out.println("Your number is too small"); 24 | } 25 | } 26 | while(usernumber>=0); 27 | System.out.println("My number was "+myNumber); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/main/java/org/example/basics/ValidVotingAge.java: -------------------------------------------------------------------------------- 1 | /* 2 | Write a program which takes age from user as input and determines whether 3 | user is valid for voting or not. 4 | */ 5 | package org.example.basics; 6 | 7 | 8 | public class ValidVotingAge 9 | { 10 | 11 | public static void main(String args[ ]) 12 | { 13 | int age; 14 | age=Integer.parseInt(args[0]); 15 | 16 | if(age>=18) 17 | System.out.println("Your age is valid for voting"); 18 | else 19 | System.out.println("Your age is not valid for voting"); 20 | } 21 | 22 | } 23 | 24 | /* ============ OUTPUT ============== 25 | 26 | C:\Java\bin>javac ValidVotingAge.java 27 | 28 | C:\Java\bin>java ValidVotingAge 18 29 | Your age is valid for voting 30 | 31 | C:\Java\bin>java ValidVotingAge 15 32 | Your age is not valid for voting 33 | 34 | ================================== */ 35 | -------------------------------------------------------------------------------- /src/main/java/org/example/patterns/ZeroOneTriangle.java: -------------------------------------------------------------------------------- 1 | package org.example.patterns; 2 | 3 | import java.util.*; 4 | public class ZeroOneTriangle { 5 | public static void main(String[] args){ 6 | 7 | // 0-1 TRIANGLE 8 | System.out.print("Enter the numbers of rows:"); 9 | Scanner sc = new Scanner(System.in); 10 | int n= sc.nextInt(); 11 | 12 | for(int i=1; i<=n; i++) { 13 | for(int j=1; j<=i; j++){ 14 | int sum=i+j; 15 | if(sum%2==0){ 16 | System.out.print("1 "); 17 | } 18 | else { 19 | System.out.print("0 "); 20 | } 21 | } 22 | System.out.println(); 23 | } 24 | 25 | } 26 | } 27 | 28 | // OUTPUT: 29 | // Enter the numbers of rows:5 30 | // 1 31 | // 0 1 32 | // 1 0 1 33 | // 0 1 0 1 34 | // 1 0 1 0 1 35 | -------------------------------------------------------------------------------- /src/main/java/org/example/datastructures/linkedlist/algo/DetectingLoops.java: -------------------------------------------------------------------------------- 1 | package org.example.datastructures.linkedlist.algo; 2 | 3 | public class DetectingLoops { 4 | class ListNode{ 5 | int val; 6 | ListNode next; 7 | ListNode (int x){ 8 | val=x; 9 | next=null; 10 | } 11 | } 12 | public boolean hasCycle(ListNode head) { 13 | if(head==null){ 14 | return false; 15 | } 16 | ListNode hare=head;//fast 17 | ListNode turtle=head;//slow 18 | while(hare.next!=null && hare!=null) 19 | { 20 | hare=hare.next.next; 21 | turtle=turtle.next; 22 | if(hare==turtle) 23 | { 24 | return true; 25 | } 26 | } 27 | return false; 28 | } 29 | public static void main(String[] args) { 30 | 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/main/java/org/example/exceptions/DivideByZeroException.java: -------------------------------------------------------------------------------- 1 | // Catching a divide by zero exception and Invalid input exception 2 | 3 | import java.util.Scanner; 4 | import java.util.InputMismatchException; 5 | 6 | class DivideByZeroException { 7 | 8 | public static void main(String[] args) { 9 | int a, b; 10 | 11 | Scanner s = new Scanner(System.in); 12 | 13 | try { 14 | System.out.print("Enter 1st Number: "); 15 | a = s.nextInt(); 16 | System.out.print("Enter 2nd Number: "); 17 | b = s.nextInt(); 18 | 19 | int division = a / b; 20 | System.out.println(a + " / " + b + " = " + division); 21 | } 22 | 23 | catch (ArithmeticException e) { 24 | System.out.println("Error! " + e.getMessage()); 25 | } 26 | 27 | catch (InputMismatchException e) { 28 | System.out.println("Error! " + e.getMessage()); 29 | } 30 | 31 | s.close(); 32 | } 33 | } -------------------------------------------------------------------------------- /src/main/java/org/example/patterns/Palindromic.java: -------------------------------------------------------------------------------- 1 | package org.example.patterns; 2 | 3 | import java.util.*; 4 | public class Palindromic { 5 | public static void main(String[] args){ 6 | 7 | // PALINDROMIC PATTERN 8 | System.out.print("Enter the numbers of rows:"); 9 | Scanner sc = new Scanner(System.in); 10 | int n= sc.nextInt(); 11 | for(int i=1; i<=n; i++){ 12 | for(int j=1; j<=(n-i); j++){ 13 | System.out.print(" "); 14 | } 15 | for(int j=i; j>=1; j--){ 16 | System.out.print(j); 17 | } 18 | for(int j=2; j<=i; j++){ 19 | System.out.print(j); 20 | } 21 | System.out.println(); 22 | } 23 | } 24 | } 25 | 26 | // OUTPUT: 27 | // Enter the numbers of rows:5 28 | // 1 29 | // 212 30 | // 32123 31 | // 4321234 32 | // 543212345 33 | -------------------------------------------------------------------------------- /src/main/java/org/example/compressiontechniques/README.md: -------------------------------------------------------------------------------- 1 | ## Data Compression 2 | 3 | In information theory, data compression, source coding, or bit-rate reduction is the process of encoding information 4 | using fewer bits than the original representation. 5 | Any particular compression is either lossy or lossless. 6 | 7 | ### Lossless compression 8 | 9 | Lossless compression reduces bits by identifying and eliminating statistical redundancy. No information is lost in 10 | lossless compression. 11 | Eg - Run-length encoding , Huffman Coding , Arithmetic Coding. 12 | 13 | ### Lossy compression 14 | 15 | Lossy compression reduces bits by removing unnecessary or less important information. 16 | Eg - JPEG, H.263, DVI (RTV & PLV), MPEG-1, MPEG-2, MPEG-4 17 | 18 | Typically, a device that performs data compression is referred to as an encoder, and one that performs the reversal of 19 | the process (decompression) as a decoder. 20 | -------------------------------------------------------------------------------- /src/main/java/org/example/basics/VariablesDemo.java: -------------------------------------------------------------------------------- 1 | /* 2 | Write a program to demonstrate different variables in java 3 | */ 4 | package org.example.basics; 5 | 6 | class Variables 7 | { 8 | int p; // Instance variable 9 | 10 | static int q; // Static variable 11 | } 12 | 13 | class VariablesDemo 14 | { 15 | public static void main(String[] args) 16 | { 17 | 18 | int a=30; // Local variable 19 | Variables var = new Variables(); 20 | 21 | var.p=10; 22 | 23 | Variables.q=20; 24 | 25 | System.out.println(var.p); 26 | System.out.println(Variables.q); 27 | System.out.println(a); 28 | } 29 | } 30 | 31 | /* ============== OUTPUT =============== 32 | 33 | C:\Java\bin>javac VariablesDemo.java 34 | 35 | C:\Java\bin>java VariablesDemo 36 | 10 37 | 20 38 | 30 39 | 40 | ===================================== */ 41 | -------------------------------------------------------------------------------- /src/main/java/org/example/datastructures/arrays/ReverseInteger.java: -------------------------------------------------------------------------------- 1 | //reversing of 32-bit Integer in range [-231, 231 - 1] 2 | package org.example.datastructures.arrays; 3 | 4 | public class ReverseInteger 5 | { 6 | 7 | public static void main(String[] args) { 8 | 9 | Scanner sc = new Scanner(System.in); 10 | int number = sc.nextInt(); 11 | 12 | int result = reverseIntegerNum(number); 13 | System.out.println(result); 14 | 15 | } 16 | 17 | public int reverseIntegerNum(int x) { 18 | 19 | long revNum=0; 20 | while(x!=0){ 21 | 22 | int rem=x%10; 23 | revNum=revNum*10+rem; 24 | x=x/10; 25 | if(revNum>Integer.MAX_VALUE || revNum 2 | 5 | 4.0.0 6 | 7 | org.example 8 | hacktoberfest-java 9 | 1.0-SNAPSHOT 10 | 11 | 12 | 17 13 | 17 14 | UTF-8 15 | 16 | 17 | 18 | mysql 19 | mysql-connector-java 20 | 8.0.30 21 | 22 | 23 | -------------------------------------------------------------------------------- /src/main/java/org/example/oop/polymorphism/SumPoly.java: -------------------------------------------------------------------------------- 1 | package org.example.oop.polymorphism; 2 | 3 | public class SumPoly { 4 | 5 | public static class Sum { 6 | // Polymorphism is the concept in which we can perform a single action in different ways 7 | // Polymorphism using method overloading 8 | 9 | public int sum(int x, int y) 10 | { 11 | return (x + y); 12 | } 13 | public int sum(int x, int y, int z) 14 | { 15 | return (x + y + z); 16 | } 17 | public double sum(double x, double y) 18 | { 19 | return (x + y); 20 | } 21 | 22 | } 23 | public static void main(String[] args) 24 | { 25 | Sum s = new Sum(); 26 | System.out.println(s.sum(10, 20)); 27 | System.out.println(s.sum(10, 20, 30)); 28 | System.out.println(s.sum(10.5, 20.5)); 29 | } 30 | } -------------------------------------------------------------------------------- /src/main/java/org/example/algorithms/recursion/FindOccurance.java: -------------------------------------------------------------------------------- 1 | package org.example.algorithms.recursion; 2 | 3 | public class FindOccurance { 4 | public static int first=-1; 5 | public static int last=-1; 6 | public static void findOccurance(String str,int idx,char element) { 7 | if(idx==str.length()) 8 | { 9 | System.out.println(first); 10 | System.out.println(last); 11 | return; 12 | } 13 | char currChar=str.charAt(idx); 14 | if(currChar==element) 15 | { 16 | if(first==-1) 17 | { 18 | first=idx; 19 | } 20 | else 21 | { 22 | last=idx; 23 | } 24 | } 25 | findOccurance(str, idx+1, element); 26 | } 27 | public static void main(String args[]) { 28 | String str="abaacdaefaah"; 29 | findOccurance(str, 0, 'a'); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /src/main/java/org/example/leetcode/TwoSum.java: -------------------------------------------------------------------------------- 1 | // Name: Raj Kasaudhan 2 | //Github Username : rajksd01 3 | 4 | // Leetcode Problem 5 | //Problem URl : https://leetcode.com/problems/two-sum/ 6 | 7 | 8 | //Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target. 9 | 10 | //You may assume that each input would have exactly one solution, and you may not use the same element twice. 11 | 12 | 13 | 14 | package org.example.leetcode; 15 | 16 | class Solution { 17 | public int[] twoSum(int[] nums, int target) { 18 | for(int i = 0 ; ijavac PrintAlphabets.java 31 | 32 | C:\Java\bin>java PrintAlphabets 33 | A B C D E F G H I J K L M N O P Q R S T U V W X Y Z 34 | a b c d e f g h i j k l m n o p q r s t u v w x y z 35 | 36 | ======================================= */ -------------------------------------------------------------------------------- /src/main/java/org/example/algorithms/sorting/InsertionSort.java: -------------------------------------------------------------------------------- 1 | package org.example.algorithms.sorting; 2 | 3 | import java.util.*; 4 | class InsertionSort 5 | { 6 | public static void main(String args[]) 7 | { 8 | Scanner sc=new Scanner(System.in); 9 | System.out.println("Enter the size of array"); 10 | int n=sc.nextInt(); 11 | int a[]=new int[n]; 12 | for(int i=0;i=0&¤t numbers[i+1]) { // This is the condition for descending order 18 | isAscending = false; 19 | } 20 | } 21 | 22 | if(isAscending) { 23 | System.out.println("The array is sorted in ascending order"); 24 | } else { 25 | System.out.println("The array is not sorted in ascending order"); 26 | } 27 | 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/main/java/org/example/basics/DublicateArrayCount.java: -------------------------------------------------------------------------------- 1 | package org.example.basics; 2 | import java.util.HashMap; 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | public class DublicateArrayCount { 7 | 8 | public static void main(String[] args) { 9 | int[] arr = {1,2,2,3,3,4}; 10 | HashMap ans = new HashMap<>(); 11 | for(int e:arr){ 12 | int count = 1; 13 | if(!ans.containsKey(e)){ 14 | ans.put(e,count); 15 | } 16 | else{ 17 | ans.put(e,ans.get(e)+1); 18 | } 19 | } 20 | List> rslt = new ArrayList<>(); 21 | 22 | for(int e:ans.keySet()){ 23 | ArrayList list = new ArrayList<>(); 24 | list.add(e); 25 | list.add(ans.get(e)); 26 | rslt.add(list); 27 | } 28 | 29 | System.out.println(rslt); 30 | } 31 | 32 | } 33 | -------------------------------------------------------------------------------- /src/main/java/org/example/leetcode/GenerateParenthesis.java: -------------------------------------------------------------------------------- 1 | package org.example.leetcode; 2 | //Question number 22 3 | 4 | class GenerateParenthesis { 5 | public List generateParenthesis(int n) { 6 | List combs = new ArrayList<>(); 7 | generate(n, n, new StringBuilder(), combs); 8 | return combs; 9 | } 10 | 11 | public void generate(int open, int close, StringBuilder sb, List combs){ 12 | if(open == 0 && close == 0){ 13 | combs.add(sb.toString()); 14 | return; 15 | } 16 | if(open > 0){ 17 | sb.append("("); 18 | generate(open - 1, close, sb, combs); 19 | sb.delete(sb.length() - 1, sb.length()); 20 | } 21 | 22 | if(close > 0 && open < close){ 23 | sb.append(")"); 24 | generate(open, close - 1, sb, combs); 25 | sb.delete(sb.length() - 1, sb.length()); 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/main/java/org/example/designpatterns/prototype/Employees.java: -------------------------------------------------------------------------------- 1 | package org.example.designpatterns.prototype; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | public class Employees implements Cloneable { 7 | 8 | private List empList; 9 | 10 | public Employees(){ 11 | empList = new ArrayList(); 12 | } 13 | 14 | public Employees(List list){ 15 | this.empList=list; 16 | } 17 | public void loadData(){ 18 | //read all employees from database and put into the list 19 | empList.add("Pankaj"); 20 | empList.add("Raj"); 21 | empList.add("David"); 22 | empList.add("Lisa"); 23 | } 24 | 25 | public List getEmpList() { 26 | return empList; 27 | } 28 | 29 | @Override 30 | public Object clone() throws CloneNotSupportedException{ 31 | List temp = new ArrayList(); 32 | for(String s : this.getEmpList()){ 33 | temp.add(s); 34 | } 35 | return new Employees(temp); 36 | } 37 | 38 | } -------------------------------------------------------------------------------- /src/main/java/org/example/algorithms/sorting/BubbleSort.java: -------------------------------------------------------------------------------- 1 | package org.example.algorithms.sorting; 2 | 3 | import java.util.*; 4 | class BubbleSort 5 | { 6 | public static void main(String args[]) 7 | { 8 | Scanner sc=new Scanner(System.in); 9 | System.out.println("Enter the size of array"); 10 | int n=sc.nextInt(); 11 | int a[]=new int[n]; 12 | for(int i=0;ia[j+1]) 22 | { 23 | int temp=a[j]; 24 | a[j]=a[j+1]; 25 | a[j+1]=temp; 26 | } 27 | } 28 | } 29 | for(int i=0;ijavac PrimeNumbers.java 37 | 38 | C:\Java\bin>java PrimeNumbers 39 | 13 40 | Number is prime 41 | 42 | C:\Java\bin>java PrimeNumbers 43 | 15 44 | Number is not prime 45 | 46 | ====================================== */ 47 | -------------------------------------------------------------------------------- /src/main/java/org/example/switchexpression/SwitchExample.java: -------------------------------------------------------------------------------- 1 | package org.example.switchexpression; 2 | 3 | /** 4 | * @author rajeshp 5 | * @Date 10/17/22 6 | */ 7 | public class SwitchExample { 8 | 9 | private static int getSwitchValue(String mode) { 10 | 11 | int result = switch (mode) { 12 | case "a", "b" -> 100; 13 | case "c" -> 200; 14 | case "d", "e", "f" -> { 15 | System.out.println("Can add more logic before this"); 16 | yield 300; 17 | } 18 | default -> 0; 19 | }; 20 | return result; 21 | } 22 | 23 | 24 | public static void main(String[] args) { 25 | 26 | System.out.println(SwitchExample.getSwitchValue("a")); 27 | 28 | System.out.println(SwitchExample.getSwitchValue("c")); 29 | 30 | System.out.println(SwitchExample.getSwitchValue("d")); 31 | 32 | System.out.println(SwitchExample.getSwitchValue("z")); 33 | 34 | 35 | 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ```diff 2 | - This repo is longer accepting pull requests as Hacktoberfest 2022 has come to an end. 3 | + Thanks for your contribution. 4 | ``` 5 | # Welcome to `Hacktoberfest-Java` 6 | 7 | ## Description 8 | A repository on GitHub to encourage and include novice contributors to open source projects. 9 | 10 | ![alt hacktoberfest-banner](./hacktoberfest-banner.jpg) 11 | 12 | ## Pre-requisite 13 | Java 14 | 15 | ## Features 16 | Beginner-friendly 17 | Offers mentorship to other contributors 18 | 19 | 20 | Refer [CONTRIBUTING.md](https://github.com/BimalRajGyawali/hacktoberfest-java/blob/main/CONTRIBUTING.md) before sending a PR. 21 | 22 | 23 | Create your profile for Hacktoberfest [here](https://hacktoberfest.com/profile/). 24 | 25 | ## Contributors 26 | 27 | Lista de contribuidores 28 | 29 | -------------------------------------------------------------------------------- /src/main/java/org/example/algorithms/sorting/SelectionSort.java: -------------------------------------------------------------------------------- 1 | package org.example.algorithms.sorting; 2 | 3 | import java.util.*; 4 | class SelectionSort 5 | { 6 | public static void main(String args[]) 7 | { 8 | Scanner sc=new Scanner(System.in); 9 | System.out.println("Enter the size of array"); 10 | int n=sc.nextInt(); 11 | int a[]=new int[n]; 12 | for(int i=0;i 0) { 14 | positive++; 15 | } else if(number < 0) { 16 | negative++; 17 | } else { 18 | zeros++; 19 | } 20 | 21 | System.out.println("Press 1 to continue & 0 to stop"); 22 | input = sc.nextInt(); 23 | } 24 | 25 | System.out.println("Positives : "+ positive); 26 | System.out.println("Negatives : "+ negative); 27 | System.out.println("Zeros : "+ zeros); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/main/java/org/example/oop/classandobject/StudentInfo.java: -------------------------------------------------------------------------------- 1 | package org.example.oop.classandobject; 2 | 3 | 4 | //The StudentInfo is a class which acts as a blueprint so that many more object or to be precise more number of student's information can be stored using it. 5 | public class StudentInfo { 6 | int rollno; 7 | String name; 8 | float percentage; 9 | String city; 10 | 11 | StudentInfo(int rollno, String name, float percentage, String city) { 12 | this.rollno = rollno; 13 | this.name = name; 14 | this.percentage = percentage; 15 | this.city = city; 16 | } 17 | 18 | public static void main(String[] args) { 19 | //object has been created of the java class of StudentInfo 20 | StudentInfo XYZ = new StudentInfo(32, "XYZ", 96f, "Chandigarh"); 21 | System.out.println(XYZ.city); 22 | System.out.println(XYZ.name); 23 | System.out.println(XYZ.percentage); 24 | System.out.println(XYZ.rollno); 25 | } 26 | 27 | } 28 | -------------------------------------------------------------------------------- /src/main/java/org/example/designpatterns/singleton/Singleton.java: -------------------------------------------------------------------------------- 1 | /* 2 | Singleton Pattern says that just 3 | "define a class that has only one instance and provides a global point of access to it". 4 | */ 5 | package org.example.designpatterns.singleton; 6 | 7 | /** 8 | * @author Narendra 9 | * @version 1.0 10 | * @since 2022-10-11 11 | */ 12 | public class Singleton { 13 | 14 | //static instance which is returned everytime from the getSingleInstance() 15 | private static Singleton instance = new Singleton(); 16 | 17 | //instance variable 18 | private Integer num; 19 | 20 | //private constructor 21 | private Singleton() { 22 | this.num = 1; 23 | } 24 | 25 | //returns same instance everytime 26 | public static Singleton getSingleInstance() { 27 | return instance; 28 | } 29 | 30 | public void incrementNum() { 31 | this.num++; 32 | } 33 | 34 | //getter for num 35 | public Integer getNum() { 36 | return this.num; 37 | } 38 | 39 | } 40 | -------------------------------------------------------------------------------- /src/main/java/org/example/leetcode/ProductArrayExceptSelf.java: -------------------------------------------------------------------------------- 1 | package org.example.leetcode 2 | import java.util.*; 3 | class ProductArayExceptSelf 4 | { 5 | public int[] productExceptSelf(int[] nums) 6 | { 7 | int[] prefix = new int[nums.length]; 8 | int[] postfix = new int[nums.length]; 9 | int[] result = new int[nums.length]; 10 | 11 | int prod = 1; 12 | // prefix 13 | // 1 2 6 24 14 | for (int i = 1; i < nums.length; i++) 15 | { 16 | prefix[0] = 1; 17 | prod = prod * nums[i - 1]; 18 | prefix[i] = prod; 19 | } 20 | prod = 1; 21 | for (int i = nums.length - 2; i >= 0; i--) 22 | { 23 | postfix[nums.length - 1] = 1; 24 | prod = prod * nums[i + 1]; 25 | postfix[i] = prod; 26 | } 27 | for (int i = 0; i < nums.length; i++) 28 | { 29 | 30 | result[i] = prefix[i] * postfix[i]; 31 | } 32 | 33 | return result; 34 | } 35 | } -------------------------------------------------------------------------------- /src/main/java/org/example/leetcode/RomanToInteger.java: -------------------------------------------------------------------------------- 1 | package org.example.leetcode; 2 | //No13 in leetcode website 3 | 4 | import java.util.HashMap; 5 | import java.util.Map; 6 | 7 | public class RomanToInteger { 8 | public static void main(String[] args) { 9 | System.out.println( rti2("XCC")); 10 | } 11 | static int rti2(String s){ 12 | int total=0; 13 | Map hashmap=new HashMap<>(); 14 | hashmap.put('I',1); 15 | hashmap.put('V',5); 16 | hashmap.put('X',10); 17 | hashmap.put('L',50); 18 | hashmap.put('C',100); 19 | hashmap.put('D',500); 20 | hashmap.put('M',1000); 21 | 22 | for (int i = 0; i > combinationSum(int[] candidates, int target) { 4 | List> ans = new ArrayList<>(); 5 | if (candidates == null || target == 0) { 6 | return ans; 7 | } 8 | 9 | backtrack(ans, new ArrayList<>(), candidates, target); 10 | return ans; 11 | } 12 | 13 | public static void backtrack(List> res, List list, int[] candidates, int remain) { 14 | if (remain < 0) { 15 | return; 16 | } 17 | 18 | if (remain == 0) { 19 | res.add(new ArrayList<>(list)); 20 | return; 21 | } 22 | 23 | int len = candidates.length; 24 | for (int i = 0; i < len; i++) { 25 | int num = candidates[i]; 26 | list.add(num); 27 | backtrack(res, list, candidates, remain - num); 28 | list.remove(list.size() - 1); 29 | } 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /src/main/java/org/example/datastructures/linkedlist/algo/RemoveDuplicateNodesFromSortedList.java: -------------------------------------------------------------------------------- 1 | package org.example.datastructures.linkedlist.algo; 2 | class RemoveDuplicateNodesFromSortedList{ 3 | public class ListNode{ 4 | int val; 5 | ListNode next; 6 | ListNode(){ 7 | 8 | } 9 | ListNode(int val){ 10 | this.val=val; 11 | } 12 | ListNode(ListNode next,int val){ 13 | this.val=val; 14 | this.next=next; 15 | } 16 | } 17 | public ListNode deleteDuplicates(ListNode head) { 18 | ListNode list = head; 19 | 20 | while(list != null) { 21 | if (list.next == null) { 22 | break; 23 | } 24 | if (list.val == list.next.val) { 25 | list.next = list.next.next; 26 | } else { 27 | list = list.next; 28 | } 29 | } 30 | 31 | return head; 32 | } 33 | public static void main(String[] args) { 34 | 35 | } 36 | } 37 | 38 | 39 | -------------------------------------------------------------------------------- /src/main/java/org/example/basics/TwoDArraySearch.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | class TwoDArraySearch 3 | { 4 | public static void main(String args[]) 5 | { 6 | Scanner sc=new Scanner(System.in); 7 | System.out.println("Enter the rows"); 8 | int r=sc.nextInt(); 9 | System.out.println("Enter the columns"); 10 | int c=sc.nextInt(); 11 | int numbers[][]=new int[r][c]; 12 | System.out.println("Enter the elements"); 13 | for(int i=0;i Prototype Design Pattern 2 | ---------------------------------- 3 |
4 | Prototype design pattern is used when the Object creation is a costly affair and requires a lot of time and resources and you have a similar object already existing. Prototype pattern provides a mechanism to copy the original object to a new object and then modify it according to our needs. 5 | 6 |

7 | 8 | Suppose we have an Object that loads data from database. Now we need to modify this data in our program multiple times, so it’s not a good idea to create the Object using new keyword and load all the data again from database. The better approach would be to clone the existing object into a new object and then do the data manipulation. Prototype design pattern mandates that the Object which you are copying should provide the copying feature. 9 | 10 |

11 | 12 | It should not be done by any other class. However whether to use shallow or deep copy of the Object properties depends on the requirements and its a design decision. 13 | 14 | -------------------------------------------------------------------------------- /src/main/java/org/example/leetcode/RansomNote.java: -------------------------------------------------------------------------------- 1 | package org.example.leetcode; 2 | public class RansomNote { 3 | 4 | public static boolean canConstruct(String r, String m) { 5 | 6 | int a[]=new int[26]; 7 | 8 | int b[]=new int[26]; 9 | for(int i =0; ib && a>c) 16 | { 17 | System.out.println(a+" is greater than "+b+" and "+c); 18 | } 19 | else if(b>a && b>c) 20 | { 21 | System.out.println(b+" is greater than "+a+" and "+c); 22 | } 23 | else if(c>a && c>b) 24 | { 25 | System.out.println(c+" is greater than "+a+" and "+b); 26 | } 27 | else 28 | { 29 | System.out.println("Don't enter identical numbers"); 30 | } 31 | } 32 | } 33 | 34 | 35 | /* =========== OUTPUT ============== 36 | 37 | C:\Java\bin>javac MaxAmongThree.java 38 | 39 | C:\Java\bin>java MaxAmongThree 10 59 62 40 | 62 is greater than 10 and 59 41 | 42 | ================================= */ -------------------------------------------------------------------------------- /src/main/java/org/example/designpatterns/singleton/Demo.java: -------------------------------------------------------------------------------- 1 | package org.example.designpatterns.singleton; 2 | 3 | /** 4 | * @author Narendra 5 | * @version 1.0 6 | * @since 2022-10-11 7 | */ 8 | public class Demo { 9 | 10 | public static void main(String[] args) { 11 | 12 | //initializing the instance of Singleton class 13 | Singleton instance1 = Singleton.getSingleInstance(); 14 | 15 | // no args constructor is of type private, so it can't be called 16 | // Singleton singleton = new Singleton(); 17 | 18 | 19 | //gives output as 1 20 | System.out.println(instance1.getNum()); 21 | 22 | //num variable was incremented 23 | instance1.incrementNum(); 24 | 25 | //we tried to create a new instance but the getSingleInstance() always returns same instance 26 | Singleton instance2 = Singleton.getSingleInstance(); 27 | 28 | //the updated value of num as 2 is printed 29 | //meaning previous instance was returned by getSingleInstance() 30 | System.out.println(instance2.getNum()); 31 | } 32 | 33 | } 34 | -------------------------------------------------------------------------------- /src/main/java/org/example/collections/PriorityQueueExample.java: -------------------------------------------------------------------------------- 1 | package org.example.collections; 2 | 3 | import java.util.*; 4 | public class PriorityQueueExample{ 5 | 6 | public static void main(String args[]){ 7 | 8 | PriorityQueue queue=new PriorityQueue(); 9 | 10 | queue.add("Amit Sharma"); 11 | 12 | queue.add("Vijay Raj"); 13 | 14 | queue.add("JaiShankar"); 15 | 16 | queue.add("Raj"); 17 | 18 | System.out.println("head:"+queue.element()); 19 | 20 | System.out.println("head:"+queue.peek()); 21 | 22 | System.out.println("iterating the queue elements:"); 23 | 24 | Iterator itr=queue.iterator(); 25 | 26 | while(itr.hasNext()){ 27 | 28 | System.out.println(itr.next()); 29 | 30 | } 31 | 32 | queue.remove(); 33 | 34 | queue.poll(); 35 | 36 | System.out.println("after removing two elements:"); 37 | 38 | Iterator itr2=queue.iterator(); 39 | 40 | while(itr2.hasNext()){ 41 | 42 | System.out.println(itr2.next()); 43 | 44 | } 45 | 46 | } 47 | 48 | } 49 | -------------------------------------------------------------------------------- /src/main/java/org/example/oop/inheritance/InheritanceExample.java: -------------------------------------------------------------------------------- 1 | package org.example.oop.inheritance; 2 | class Base{ 3 | public int x; 4 | 5 | public int getX() { 6 | return x; 7 | } 8 | 9 | public void setX(int a) { 10 | System.out.println("I am in base and setting x now"); 11 | x = a; 12 | } 13 | } 14 | 15 | class Derived extends Base 16 | { 17 | public int y; 18 | 19 | public int getY() { 20 | return y; 21 | } 22 | 23 | public void setY(int b) { 24 | System.out.println("I am in Derived and setting y now"); 25 | y = b; 26 | } 27 | } 28 | 29 | 30 | public class InheritanceExample{ 31 | public static void main(String[] args) { 32 | // Creating an Object of base class 33 | Base b = new Base(); 34 | b.setX(4); 35 | System.out.println(b.getX()); 36 | 37 | // Creating an object of derived class 38 | Derived d = new Derived(); 39 | d.setY(43); 40 | System.out.println(d.getY()); 41 | 42 | 43 | 44 | 45 | } 46 | 47 | } -------------------------------------------------------------------------------- /src/main/java/org/example/basics/ArmstrongNumber.java: -------------------------------------------------------------------------------- 1 | package org.example.basics; 2 | 3 | /* 4 | Write a program to check whether entered number is armstrong or not. 5 | */ 6 | 7 | 8 | import java.lang.Math; 9 | 10 | class ArmstrongNumber 11 | { 12 | public static void main(String args[ ]) 13 | { 14 | int n=Integer.parseInt(args[0]); 15 | int temp=n,sum=0,d,p=args[0].length(); 16 | 17 | while(n>0) 18 | { 19 | d=n%10; 20 | sum+=Math.pow(d,p); 21 | n=n/10; 22 | } 23 | 24 | if(sum==temp) 25 | { 26 | System.out.println("Entered number is armstrong"); 27 | } 28 | else 29 | { 30 | System.out.println("Entered number is not armstrong"); 31 | } 32 | } 33 | } 34 | 35 | 36 | /* =============== OUTPUT ============== 37 | 38 | C:\Java\bin>javac ArmstrongNumber.java 39 | 40 | C:\Java\bin>java ArmstrongNumber 153 41 | Entered number is armstrong 42 | 43 | C:\Java\bin>java ArmstrongNumber 155 44 | Entered number is not armstrong 45 | 46 | ===================================== */ 47 | 48 | -------------------------------------------------------------------------------- /src/main/java/org/example/basics/Raisedto.java: -------------------------------------------------------------------------------- 1 | /* 2 | Two numbers are entered through the keyboard. Write a program to find the value of one number 3 | raised to the power of another. (Do not use Java built-in method) 4 | */ 5 | package org.example.basics; 6 | 7 | import java.util.*; 8 | class Raisedto 9 | { 10 | 11 | public static void main(String[] args) 12 | { 13 | int a,b,res=1; 14 | Scanner sc = new Scanner(System.in); 15 | System.out.println("Enter base value"); 16 | a=sc.nextInt(); 17 | System.out.println("Enter index value"); 18 | b=sc.nextInt(); 19 | for(int i=0;ijavac Raisedto.java 32 | 33 | C:\Java\bin>java Raisedto 34 | Enter base value 35 | 2 36 | Enter index value 37 | 5 38 | Value of 2 raised to 5 is 32 39 | 40 | ==================================== */ 41 | -------------------------------------------------------------------------------- /src/main/java/org/example/datastructures/arrays/README.md: -------------------------------------------------------------------------------- 1 | Creation and declaration of array in java. 2 | 3 | Unlike primitive data types, any array has to be created using the new operator. A reference to an array can be declared without any elements and can be assigned to null. A reference can be further modified as well. 4 | To create an array, you first must create an array variable of the desired type. The general form of a one-dimensional array declaration is 5 | type var-name[] = new type[desired-length]; 6 | 7 | - type tells the element type e.g., int, float 8 | - var-name gives the variable name e.g., student_marks, months 9 | - new allocates memory required for the array with the size mentioned at desired-length e.g., 5, 12 10 | 11 | The following code creates an array of student marks for a class with 25 students. And all elements in the array are initialized to zero automatically. 12 | int student_marks[] = new int[25]; 13 | - Arrays are allocated at run-time, hence we can use a variable to set their dimension. 14 | 15 | Length of the array can be obtained using "length" 16 | - to get array length: arr.length; 17 | 18 | -------------------------------------------------------------------------------- /src/main/java/org/example/collections/StackExample.java: -------------------------------------------------------------------------------- 1 | package org.example.collections; 2 | 3 | import java.util.Stack; 4 | 5 | public class StackExample { 6 | 7 | public static void main(String[] args) { 8 | 9 | // Create a stack object 10 | Stack stk = new Stack<>(); 11 | 12 | print(stk); 13 | 14 | // Push elements into the stack 15 | push(stk, 20); 16 | push(stk, 13); 17 | push(stk, 89); 18 | 19 | // Pop elements from the stack 20 | pop(stk); 21 | pop(stk); 22 | pop(stk); 23 | } 24 | 25 | // Perform the push operations and show the contents of the stack 26 | static void push(Stack stk, int x) { 27 | stk.push(x); 28 | 29 | System.out.println("push -> " + x); 30 | 31 | print(stk); 32 | } 33 | 34 | // Perform the pop operations and show the contents of the stack 35 | static void pop(Stack stk) { 36 | System.out.println("pop -> " + stk.pop()); 37 | 38 | print(stk); 39 | } 40 | 41 | // Print the contents of the stack 42 | static void print(Stack stk) { 43 | System.out.println("Stack: " + stk); 44 | } 45 | } -------------------------------------------------------------------------------- /src/main/java/org/example/files/CopyFileWithoutBuffer.java: -------------------------------------------------------------------------------- 1 | // Copy a file from one directory to another directory without Buffer 2 | 3 | import java.io.*; 4 | import java.util.Scanner; 5 | 6 | class CopyFileWithoutBuffer { 7 | public static void main(String args[]) throws IOException { 8 | FileInputStream fis; 9 | FileOutputStream fos; 10 | try { 11 | Scanner s = new Scanner(System.in); 12 | System.out.print("Enter the name of the file(with absolute or relative path) to be copied: "); 13 | String filename = s.nextLine(); 14 | System.out.print("Enter the name of the file(with absolute or relative path) to be copied to: "); 15 | String newFilename = s.nextLine(); 16 | s.close(); 17 | 18 | fis = new FileInputStream(filename); 19 | fos = new FileOutputStream(newFilename); 20 | 21 | System.out.println("Copying...."); 22 | int i; 23 | while ((i = fis.read()) != -1) { 24 | fos.write(i); 25 | } 26 | System.out.println("Copy completed"); 27 | } catch (FileNotFoundException e) { 28 | System.out.println("File not found"); 29 | } 30 | } 31 | } -------------------------------------------------------------------------------- /src/main/java/org/example/oop/encapsulation/Encapsulation.java: -------------------------------------------------------------------------------- 1 | package org.example.oop.encapsulation; 2 | 3 | /* 4 | Encap is the variables of a class will be 5 | hidden from other classes, and can be accessed only through th emthods of their current class 6 | its increased security of data 7 | 8 | */ 9 | public class Encapsulation { 10 | private String name; 11 | private int age; 12 | 13 | public String getName(){ 14 | return name; 15 | } 16 | 17 | public int getAge(){ 18 | return age; 19 | //get returns the variable value 20 | } 21 | 22 | public void setName(String newname){ 23 | this.name = newname; 24 | //this keyword use to refer to the current object 25 | } 26 | 27 | public void setAge(int newage){ 28 | this.age = newage; 29 | } 30 | public static void main(String args[]){ 31 | Encapsulation encap = new Encapsulation(); 32 | encap.setName("Oliver"); // set the name 33 | encap.setAge(24); //set the age 34 | 35 | System.out.println ("Name:"+ encap.getName()+ "Age" + encap.getAge()); // get the value 36 | } 37 | } -------------------------------------------------------------------------------- /src/main/java/org/example/basics/Formatting.java: -------------------------------------------------------------------------------- 1 | package org.example.formatting; 2 | 3 | import java.util.Scanner; 4 | 5 | public class Formatting { 6 | 7 | public static void main(String[] args) { 8 | Scanner sc=new Scanner(System.in); 9 | System.out.println("================================"); 10 | for(int i=0;i<3;i++){ 11 | String s1=sc.next(); 12 | int x=sc.nextInt(); 13 | System.out.print(s1); 14 | for(int z=s1.length(); z<15; z++) 15 | { 16 | System.out.print(" "); 17 | } 18 | if(x>9 && x<99) 19 | { 20 | System.out.print("0"+x); 21 | } 22 | else if(x>=0 && x<10) 23 | { 24 | System.out.print("00"+x); 25 | } 26 | else{ 27 | System.out.print(x); 28 | } 29 | System.out.println(); 30 | } 31 | System.out.println("================================"); 32 | 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /src/main/java/org/example/basics/Palindrome.java: -------------------------------------------------------------------------------- 1 | package org.example.basics; 2 | 3 | public class Palindrome { 4 | 5 | /*Method to 6 | Returning true if string is palindrome*/ 7 | static boolean isPalindrome(String str) 8 | { 9 | 10 | //Here pointers point to the begining and end of the String 11 | int i = 0, j = str.length() - 1; 12 | 13 | //do comapre while 14 | while (i < j) { 15 | 16 | //if find a mismatch letter 17 | if (str.charAt(i) != str.charAt(j)) 18 | return false; 19 | 20 | // Increment first pointer and decrement other pointer 21 | 22 | i++; 23 | j--; 24 | } 25 | 26 | // Given string is a palindrome 27 | return true; 28 | } 29 | 30 | // Method 2 31 | // main driver method 32 | public static void main(String[] args) 33 | { 34 | // Input string 35 | String str = "lol"; 36 | 37 | //Convert the string to lowercase 38 | str = str.toLowerCase(); 39 | // passing bool function till holding true 40 | if (isPalindrome(str)) 41 | 42 | // It is a palindrome 43 | System.out.print("Yes"); 44 | else 45 | 46 | // Not a palindrome 47 | System.out.print("No"); 48 | } 49 | } -------------------------------------------------------------------------------- /src/main/java/org/example/leetcode/MinimumSumGreedy.java: -------------------------------------------------------------------------------- 1 | package org.example.leetcode 2 | import java.util.*; 3 | class MinimumSumGreedy{ 4 | public static void main(String[] args) { 5 | int n = 6; 6 | int arr[] = {6, 8, 4, 5, 2, 3}; 7 | Solution s = new Solution(); 8 | long a = s.minSum(arr,n); 9 | System.out.print(a); 10 | } 11 | } 12 | 13 | class Solution { 14 | 15 | public static long minSum(int arr[], int n) 16 | { 17 | // Your code goes here 18 | PriorityQueuepq = new PriorityQueue<>(); 19 | for(int i = 0; i 0){ 25 | num1 += pq.poll().toString(); 26 | if(pq.size() > 0) num2 += pq.poll().toString(); 27 | } 28 | if(num1.length() == 0) num1 += '0'; 29 | if(num2.length() == 0) num2 += '0'; 30 | long i= Long.parseLong(num1); 31 | long j= Long.parseLong(num2); 32 | long sum = i + j; 33 | return sum; 34 | 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /src/main/java/org/example/basics/SumOfArray.java: -------------------------------------------------------------------------------- 1 | /* 2 | Write a program to take 10 numbers as input from user and display addition of them using array. 3 | */ 4 | package org.example.basics; 5 | 6 | import java.util.*; 7 | 8 | public class SumOfArray 9 | { 10 | 11 | public static void main(String[] args) 12 | { 13 | int[] a= new int[10]; 14 | Scanner sc = new Scanner(System.in); 15 | int sum=0; 16 | 17 | System.out.println("Enter 10 numbers"); 18 | for(int i=0;i<10;i++) 19 | { 20 | a[i]=sc.nextInt(); 21 | } 22 | 23 | for(int i=0;i<10;i++) 24 | { 25 | sum+=a[i]; 26 | } 27 | 28 | System.out.println("Addition of elements in array is : "+sum); 29 | } 30 | } 31 | 32 | /* ============= OUTPUT ================ 33 | 34 | C:\Java\bin>javac SumOfArray.java 35 | 36 | C:\Java\bin>java SumOfArray 37 | Enter 10 numbers 38 | 1 39 | 2 40 | 3 41 | 4 42 | 5 43 | 6 44 | 7 45 | 8 46 | 9 47 | 10 48 | Addition of elements in array is : 55 49 | 50 | ===================================== */ 51 | 52 | -------------------------------------------------------------------------------- /src/main/java/org/example/basics/DudeneyNumber.java: -------------------------------------------------------------------------------- 1 | package org.example.basics; 2 | 3 | import java.util.Scanner; 4 | 5 | public class DudeneyNumber 6 | { 7 | public static void main(String[] args) { 8 | Scanner in = new Scanner(System.in); 9 | System.out.print("Enter the number: "); 10 | int n = in.nextInt(); 11 | 12 | //Check if n is a perfect cube 13 | int cubeRoot = (int)Math.round(Math.cbrt(n)); 14 | if (cubeRoot * cubeRoot * cubeRoot == n) { 15 | //If n is perfect cube then find 16 | //sum of its digits 17 | int s = 0; 18 | int t = n; 19 | while (t != 0) { 20 | int d = t % 10; 21 | s += d; 22 | t /= 10; 23 | } 24 | 25 | if (s == cubeRoot) { 26 | System.out.println(n + " is a Dudeney number"); 27 | } 28 | else { 29 | System.out.println(n + " is not a Dudeney number"); 30 | } 31 | } 32 | else { 33 | System.out.println(n + " is not a Dudeney number"); 34 | } 35 | } 36 | } -------------------------------------------------------------------------------- /src/main/java/org/example/basics/Election.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | 4 | /* Name of the class has to be "Main" only if the class is public. */ 5 | class Election 6 | { 7 | public static void main (String[] args) 8 | { 9 | Scanner sc=new Scanner(System.in); 10 | int Xa,Xb,Xc; 11 | int T; 12 | T=sc.nextInt(); 13 | if(1<=T&&T<=500) 14 | { 15 | for(int i=1;i<=T;i++) 16 | { Xa=sc.nextInt(); 17 | Xb=sc.nextInt(); 18 | Xc=sc.nextInt(); 19 | if(1<=Xa&&Xa<=101&&1<=Xb&&Xa<=101&&1<=Xc&&Xc<=101&&(Xa+Xb+Xc)==101) 20 | {if(Xa>50) 21 | {System.out.println("A");} 22 | else if(Xb>50) 23 | {System.out.println("B");} 24 | else if(Xc>50) 25 | {System.out.println("C");} 26 | else{ 27 | if(Xa>Xb&&Xa>Xc) 28 | {System.out.println("A");} 29 | else if(Xb>Xa&&Xb>Xc) 30 | {System.out.println("B");} 31 | else if(Xc>Xa&&Xc>Xb) 32 | {System.out.println("C");} 33 | else{System.out.println("NOTA");} 34 | } 35 | } 36 | } 37 | } 38 | } 39 | } -------------------------------------------------------------------------------- /src/main/java/org/example/datastructures/stack/StackArrayList.java: -------------------------------------------------------------------------------- 1 | import java.util.ArrayList; 2 | 3 | public class StackArrayList { 4 | static class Stack{ 5 | static ArrayList list=new ArrayList<>(); 6 | public static boolean isEmpty() { 7 | return list.size()==0; 8 | } 9 | public static void push(int data) { 10 | list.add(data); 11 | } 12 | public static int pop() { 13 | if(isEmpty()){ 14 | return -1; 15 | } 16 | int top=list.get(list.size()-1); 17 | list.remove(list.size()-1); 18 | return top; 19 | } 20 | public static int peek(){ 21 | if(isEmpty()){ 22 | return -1; 23 | } 24 | return list.get(list.size()-1); 25 | } 26 | } 27 | public static void main(String[] args) { 28 | Stack s=new Stack(); 29 | s.push(1); 30 | s.push(2); 31 | s.push(3); 32 | s.push(4); 33 | while(!s.isEmpty()) 34 | { 35 | System.out.println(s.peek()); 36 | s.pop(); 37 | } 38 | } 39 | } 40 | 41 | 42 | -------------------------------------------------------------------------------- /src/main/java/org/example/strings/PalindromeString.java: -------------------------------------------------------------------------------- 1 | package org.example.strings; 2 | public class PalindromeString { 3 | 4 | public static boolean isPalindrome(String s) { 5 | String p = s.toLowerCase(); 6 | StringBuilder str = new StringBuilder(""); 7 | for (int i = 0; i 47 && p.charAt(i)<=57) || ( p.charAt(i)>96 &&p.charAt(i)<=122) ) { 9 | str.append(p.charAt(i)); 10 | } 11 | } 12 | int start =0; 13 | int end =str.length()-1; 14 | while(start<=end) { 15 | if (str.charAt(start)!= str.charAt(end)) { 16 | return false; 17 | 18 | } 19 | start++; 20 | end--; 21 | } 22 | 23 | return true; 24 | 25 | } 26 | 27 | public static void main(String[] args) { 28 | String s = "Amisha"; 29 | System.out.println(isPalindrome(s)); 30 | String str = "101"; 31 | System.out.println(isPalindrome(str)); 32 | } 33 | 34 | 35 | } 36 | -------------------------------------------------------------------------------- /src/main/java/org/example/datastructures/linkedlist/algo/RemoveNthNodeFromEnd.java: -------------------------------------------------------------------------------- 1 | package org.example.datastructures.linkedlist.algo; 2 | 3 | class RemoveNthNodeFromEnd{ 4 | public class ListNode{ 5 | int val; 6 | ListNode next; 7 | ListNode(){ 8 | 9 | } 10 | ListNode(int val){ 11 | this.val=val; 12 | } 13 | ListNode(ListNode next,int val){ 14 | this.val=val; 15 | this.next=next; 16 | } 17 | } 18 | public ListNode removeNthFromEnd(ListNode head,int n){ 19 | if(head.next==null){ 20 | return null; 21 | } 22 | int size=0; 23 | ListNode curr=head; 24 | while(curr!=null){ 25 | curr=curr.next; 26 | size++; 27 | } 28 | if(n==size) 29 | { 30 | return head.next; 31 | } 32 | int indexToSearch=size-n; 33 | ListNode prev=head; 34 | int i=0; 35 | while(i neighbors; 10 | } */ 11 | 12 | //Solution 13 | package org.example.leetcode; 14 | 15 | public class CloneGraph { 16 | public void dfs(Node node , Node copy , Node[] visited){ 17 | visited[copy.val] = copy; 18 | for(Node n : node.neighbors){ 19 | if(visited[n.val] == null){ 20 | Node newNode = new Node(n.val); 21 | copy.neighbors.add(newNode); 22 | dfs(n , newNode , visited); 23 | }else{ 24 | copy.neighbors.add(visited[n.val]); 25 | } 26 | } 27 | } 28 | 29 | public Node cloneGraph(Node node) { 30 | if(node == null) return null; 31 | Node copy = new Node(node.val); 32 | Node[] visited = new Node[101]; 33 | Arrays.fill(visited , null); 34 | dfs(node , copy , visited); 35 | return copy; 36 | } 37 | } 38 | 39 | -------------------------------------------------------------------------------- /src/main/java/org/example/algorithms/sorting/ShellSort.java: -------------------------------------------------------------------------------- 1 | public class ShellSort { 2 | 3 | static void printArray(int arr[]) 4 | { 5 | int n = arr.length; 6 | for (int i=0; i 0; gap /= 2) 16 | { 17 | for (int i = gap; i < n; i += 1) 18 | { 19 | int temp = arr[i]; 20 | int j; 21 | for (j = i; j >= gap && arr[j - gap] > temp; j -= gap) 22 | arr[j] = arr[j - gap]; 23 | 24 | arr[j] = temp; 25 | } 26 | } 27 | return 0; 28 | } 29 | 30 | public static void main(String args[]) 31 | { 32 | int arr[] = {12, 34, 54, 2, 3}; 33 | System.out.println("Array before sorting"); 34 | printArray(arr); 35 | 36 | ShellSort ob = new ShellSort(); 37 | ob.sort(arr); 38 | 39 | System.out.println("Array after sorting"); 40 | printArray(arr); 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /src/main/java/org/example/reflection/FieldModificationDemo.java: -------------------------------------------------------------------------------- 1 | package org.example.reflection; 2 | 3 | import java.lang.reflect.Field; 4 | 5 | import static java.lang.System.out; 6 | 7 | public class FieldModificationDemo { 8 | public static void main(String[] args) throws NoSuchFieldException, IllegalAccessException { 9 | // create an instance of BestFruit 10 | // we will be changing the name of the 11 | // fruit, which is stored in private variable 12 | 13 | BestFruit fruit = new BestFruit("Apple"); 14 | out.println("Before modification " + fruit.getName()); 15 | 16 | // target the field named 'name' 17 | // use getDeclaredField() instead of getField() 18 | // because getDeclaredField() 19 | // lists all the fields present in the class including the private one's. 20 | 21 | Field field = BestFruit.class.getDeclaredField("name"); 22 | 23 | // make the field accessible to modify 24 | field.setAccessible(true); 25 | 26 | // make changes ont the instance 27 | // and set name ot Mango 28 | field.set(fruit, "Mango"); 29 | 30 | out.println("After modification: " + fruit.getName()); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/main/java/org/example/leetcode/MergeKSortedList.java: -------------------------------------------------------------------------------- 1 | package org.example.leetcode; 2 | /** 3 | * https://leetcode.com/problems/merge-k-sorted-lists/submissions/ 4 | * Definition for singly-linked list. 5 | * public class ListNode { 6 | * int val; 7 | * ListNode next; 8 | * ListNode() {} 9 | * ListNode(int val) { this.val = val; } 10 | * ListNode(int val, ListNode next) { this.val = val; this.next = next; } 11 | * } 12 | */ 13 | public class MergeKSortedList { 14 | public ListNode mergeKLists(ListNode[] lists) { 15 | if (lists==null||lists.length==0) return null; 16 | PriorityQueue queue= new PriorityQueue(lists.length ,(a,b) -> a.val-b.val); 17 | ListNode dummy = new ListNode(0); 18 | ListNode tail=dummy; 19 | 20 | for (ListNode node:lists) 21 | if (node!=null) 22 | queue.add(node); 23 | 24 | while (!queue.isEmpty()){ 25 | tail.next=queue.poll(); 26 | tail=tail.next; 27 | 28 | if (tail.next!=null) 29 | queue.add(tail.next); 30 | } 31 | return dummy.next; 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /src/main/java/org/example/functionalprogramming/PredicateExample.java: -------------------------------------------------------------------------------- 1 | package org.example.functionalprogramming; 2 | 3 | import java.util.function.BiPredicate; 4 | import java.util.function.Predicate; 5 | 6 | public class PredicateExample { 7 | public static void main(String[] args) { 8 | System.out.println(isPhoneNumberValid.test("9198916644")); 9 | 10 | // Chaining the predicates 11 | boolean res = isPhoneNumberValid.and(containsNumber3).test("9198976544"); 12 | System.out.println(res); 13 | 14 | boolean orRes = isPhoneNumberValid.or(containsNumber3).test("9146865656"); 15 | System.out.println(orRes); 16 | 17 | boolean biRes = isValidName.test("Aryan", "Upadhyay"); 18 | System.out.println(biRes); 19 | } 20 | 21 | // Predicate is a boolean returning function that takes one input. 22 | static Predicate isPhoneNumberValid = phoneNumber -> 23 | phoneNumber.startsWith("91") && (phoneNumber.length() == 10); 24 | 25 | static Predicate containsNumber3 = phoneNumber -> phoneNumber.contains("3"); 26 | 27 | static BiPredicate isValidName = (fName, lName) -> 28 | (fName.length()!=0 && !lName.equals(fName)); 29 | } 30 | -------------------------------------------------------------------------------- /src/main/java/org/example/javatexteditor/README.md: -------------------------------------------------------------------------------- 1 | # 19SW42-DSA-Project 2 | A java text Editor 3 | ------------------- 4 | 5 | I wanted to make a text editor using java using which we can open, modify or create new text files. So to record the information about which file is opened or what modifications have been done with a file, I needed a data structure in which I can store this kind of information. 6 | As the text editor I am going to make has operation like undo/redo or open from history(the file(s) opened before opening of current file) options so this is clear that no data structure other than Stack could achieve this goal in an efficient manner. 7 | As from stack we can undo very easily by popping recent activity and same goes for redo and open from history options. For that I have provided my own implementation of stacks. 8 | Though it has some basic functionalities to interact with text files but we can say that some how it works same as the notepad which opens/edits/creates text files. 9 | All functionalities that were thought to be part of this project i.e text editor. are fulfilled very well. Each operation is performing actions as it should do which includes cut, copy, paste, clear, new file ,save file, undo redo and closing operations. 10 | -------------------------------------------------------------------------------- /src/main/java/org/example/basics/ArrayDemo.java: -------------------------------------------------------------------------------- 1 | /* 2 | Write a program to illustrate array in java. 3 | */ 4 | package org.example.basics; 5 | 6 | 7 | import java.util.*; 8 | class ArrayDemo 9 | { 10 | public static void main(String[ ] args) 11 | { 12 | 13 | int a[]=new int[10]; 14 | Scanner sc=new Scanner(System.in); 15 | System.out.println("Enter 10 numbers"); 16 | for(int i=0;i<10;i++) 17 | { 18 | a[i]=sc.nextInt(); 19 | } 20 | 21 | System.out.println("\nEntered numbers are"); 22 | 23 | for(int i=0;i<10;i++) 24 | { 25 | System.out.print(a[i]+" "); 26 | } 27 | 28 | System.out.println("\nEntered numbers in reverse order are"); 29 | for(int i=9;i>=0;i--) 30 | { 31 | System.out.print(a[i]+" "); 32 | } 33 | 34 | } 35 | } 36 | 37 | /* ================= OUTPUT ================= 38 | 39 | C:\Java\bin>javac ArrayDemo.java 40 | 41 | C:\Java\bin>java ArrayDemo 42 | Enter 10 numbers 43 | 1 44 | 2 45 | 3 46 | 4 47 | 5 48 | 6 49 | 7 50 | 8 51 | 9 52 | 10 53 | 54 | Entered numbers are 55 | 1 2 3 4 5 6 7 8 9 10 56 | Entered numbers in reverse order are 57 | 10 9 8 7 6 5 4 3 2 1 58 | 59 | ========================================== */ 60 | 61 | -------------------------------------------------------------------------------- /src/main/java/org/example/oop/inheritance/InheritanceInInterfaces.java: -------------------------------------------------------------------------------- 1 | package org.example.oop.inheritance; 2 | 3 | //Java program to implement inheritance in interfaces 4 | interface SampleInterface{ 5 | void method1(); 6 | void method2(); 7 | } 8 | interface ChildSampleInterface extends SampleInterface{ //This interface inherits methods of Sampleinterface 9 | void method3(); 10 | void method4(); 11 | } 12 | 13 | class MySampleClass implements ChildSampleInterface{ //class cannot extend interface only implements. 14 | public void method1(){ 15 | System.out.println("Method 1 from Sample Interface"); 16 | } 17 | public void method2(){ 18 | System.out.println("Method 2 from Sample Interface"); 19 | } 20 | public void method3(){ 21 | System.out.println("Method 3 from Child Sample Interface"); 22 | } 23 | public void method4(){ 24 | System.out.println("Method 4 from Child Sample Interface"); 25 | } 26 | } 27 | 28 | public class InheritanceInInterfaces{ 29 | public static void main(String[] args) { 30 | MySampleClass SC = new MySampleClass(); 31 | SC.method1(); 32 | SC.method2(); 33 | SC.method3(); 34 | SC.method4(); 35 | } 36 | 37 | } -------------------------------------------------------------------------------- /src/main/java/org/example/algorithms/backtracking/RatInMaze.java: -------------------------------------------------------------------------------- 1 | package org.example.algorithms.backtracking; 2 | 3 | import java.util.ArrayList; 4 | 5 | // m is the given matrix and n is the order of matrix 6 | class Solution { 7 | public static ArrayList findPath(int[][] m, int n) { 8 | // Your code here 9 | ArrayList ans = new ArrayList<>(); 10 | boolean[][] visited = new boolean[n][n]; 11 | solve(m,n,ans,"",visited,0,0); 12 | if(ans.size()==0){ 13 | ans.add("-1"); 14 | } 15 | return ans; 16 | } 17 | 18 | public static void solve(int[][] m, int n, ArrayList ans, String output,boolean[][] visited,int i,int j){ 19 | if(i<0 || i>=n || j<0 || j>=n || visited[i][j]==true || m[i][j]==0){ 20 | return; 21 | } 22 | if(i==n-1 && j==n-1){ 23 | ans.add(output); 24 | return; 25 | } 26 | visited[i][j] = true; 27 | 28 | solve(m,n,ans,output + 'D',visited,i+1,j); 29 | solve(m,n,ans,output + 'U',visited,i-1,j); 30 | solve(m,n,ans,output + 'R',visited,i,j+1); 31 | solve(m,n,ans,output + 'L',visited,i,j-1); 32 | 33 | visited[i][j]=false; 34 | } 35 | } -------------------------------------------------------------------------------- /src/main/java/org/example/basics/SumOfEvenInArray.java: -------------------------------------------------------------------------------- 1 | /* 2 | Write a program to take 10 numbers as input from user and display addition of even numbers among them using array. 3 | */ 4 | package org.example.basics; 5 | 6 | import java.util.*; 7 | 8 | public class SumOfEvenInArray 9 | { 10 | 11 | public static void main(String[] args) 12 | { 13 | int[] a= new int[10]; 14 | Scanner sc = new Scanner(System.in); 15 | int sum=0; 16 | 17 | System.out.println("Enter 10 numbers"); 18 | for(int i=0;i<10;i++) 19 | { 20 | a[i]=sc.nextInt(); 21 | } 22 | 23 | for(int i=0;i<10;i++) 24 | { 25 | if(a[i]%2==0) 26 | { 27 | sum+=a[i]; 28 | } 29 | } 30 | 31 | System.out.println("Addition of even elements in array is : "+sum); 32 | } 33 | } 34 | 35 | /* ============= OUTPUT ================ 36 | 37 | C:\Java\bin>javac SumOfArray.java 38 | 39 | C:\Java\bin>java SumOfArray 40 | Enter 10 numbers 41 | 1 42 | 2 43 | 3 44 | 4 45 | 5 46 | 6 47 | 7 48 | 8 49 | 9 50 | 10 51 | Addition of even elements in array is : 30 52 | 53 | ===================================== */ 54 | 55 | -------------------------------------------------------------------------------- /src/main/java/org/example/leetcode/MedianSortedArrays.java: -------------------------------------------------------------------------------- 1 | package org.example.leetcode; 2 | /** 3 | * MedianSortedArrays 4 | * https://leetcode.com/problems/median-of-two-sorted-arrays/ 5 | */ 6 | public class MedianSortedArrays { 7 | public double findMedianSortedArrays(int[] nums1, int[] nums2) { 8 | int ind1 = 0, ind2 = 0, mid1 = 0, mid2 = 0; 9 | 10 | for (int i=0; i<=(nums1.length + nums2.length)/2; i++) { 11 | mid1 = mid2; 12 | if (ind1 == nums1.length) { 13 | mid2 = nums2[ind2]; 14 | ind2++; 15 | } 16 | else if (ind2 == nums2.length) { 17 | mid2 = nums1[ind1]; 18 | ind1++; 19 | } 20 | else if (nums1[ind1] < nums2[ind2] ) { 21 | mid2 = nums1[ind1]; 22 | ind1++; 23 | } 24 | else { 25 | mid2 = nums2[ind2]; 26 | ind2++; 27 | } 28 | } 29 | 30 | // the median is the average of two numbers 31 | if ((nums1.length+nums2.length) % 2 == 0) { 32 | return (float)(mid1 + mid2)/2; 33 | } 34 | 35 | return mid2; 36 | } 37 | } -------------------------------------------------------------------------------- /src/main/java/org/example/basics/AddBinary.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | import java.lang.*; 3 | import java.io.*; 4 | 5 | /* Name of the class has to be "Main" only if the class is public. */ 6 | class AddBinary 7 | { 8 | public static void binary(int n) 9 | { 10 | int c=0; 11 | 12 | int a[]=new int[32]; 13 | for(int i=n;i!=0;i/=2) 14 | { 15 | a[c++]=i%2; 16 | } 17 | for(int i=c-1;i>=0;i--) 18 | { 19 | System.out.print(a[i]);} 20 | } 21 | public static int decimal(int n) 22 | { 23 | int c=0;int sum=0; 24 | for(int i=n;i!=0;i/=10) 25 | { 26 | int rem=i%10; 27 | sum+=rem*Math.pow(2,c++); 28 | } 29 | return sum; 30 | } 31 | public static void main (String[] args) throws java.lang.Exception 32 | { 33 | // your code goes here 34 | Scanner sc=new Scanner(System.in); 35 | int t=sc.nextInt(); 36 | for(int i=1;i<=t;i++) 37 | { 38 | String s1=sc.next(); 39 | String s2=sc.next(); 40 | int s11=Integer.parseInt(s1); 41 | int s22=Integer.parseInt(s2); 42 | int n1=decimal(s11); 43 | int n2=decimal(s22); 44 | int answer=n1+n2; 45 | binary(answer); 46 | } 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /src/main/java/org/example/basics/PrintName.java: -------------------------------------------------------------------------------- 1 | /* 2 | Write a program to store and display your name using charecter array. 3 | */ 4 | package org.example.basics; 5 | 6 | import java.util.*; 7 | 8 | class PrintName 9 | { 10 | public static void main(String[] args) 11 | { 12 | 13 | char[] name= new char[20]; 14 | int length; 15 | Scanner sc = new Scanner(System.in); 16 | System.out.println("Enter length of the name"); 17 | length=sc.nextInt(); 18 | System.out.println("Enter name charecter by charecter"); 19 | for(int i=0;ijavac PrintName.java 35 | 36 | C:\Java\bin>java PrintName 37 | Enter length of the name 38 | 5 39 | Enter name charecter by charecter 40 | j 41 | a 42 | m 43 | e 44 | s 45 | Your entered name is : james 46 | 47 | ========================================= */ 48 | 49 | 50 | -------------------------------------------------------------------------------- /src/main/java/org/example/basics/Calculator.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | class Calculator 3 | {public static void main(String args[]) 4 | { 5 | Scanner sc=new Scanner(System.in); 6 | System.out.println("Enter the first number"); 7 | int a=sc.nextInt(); 8 | System.out.println("Enter the second number"); 9 | int b=sc.nextInt(); 10 | System.out.println("1:Addition"); 11 | System.out.println("2:Subtraction"); 12 | System.out.println("3:Multiplication"); 13 | System.out.println("4:Division"); 14 | System.out.println("5:Remainder"); 15 | System.out.println("Enter your choice"); 16 | int ch=sc.nextInt(); 17 | switch(ch) 18 | { case 1: System.out.println(a+b); 19 | break; 20 | case 2: System.out.println(a-b); 21 | break; 22 | case 3: System.out.println(a*b); 23 | break; 24 | case 4: double di=a/b; 25 | System.out.println(di); 26 | break; 27 | case 5: if(a>b) 28 | { 29 | int rem=a%b; 30 | System.out.println(rem); 31 | } 32 | else 33 | { 34 | int rem1=b%a; 35 | System.out.println(rem1); 36 | } 37 | break; 38 | default: System.out.println("OOPS! Wrong Input"); 39 | } 40 | 41 | }} -------------------------------------------------------------------------------- /src/main/java/org/example/algorithms/sorting/QuickSort.java: -------------------------------------------------------------------------------- 1 | package org.example.algorithms.sorting; 2 | 3 | public class QuickSort { 4 | 5 | public static int partition(int arr[],int low,int high) { 6 | int pivot=arr[high]; 7 | int i=low-1; 8 | for(int j=low;j intObj = new GenericsClassExample<>(8); 9 | System.out.println("Generic Class returns: " + intObj.getData()); 10 | 11 | // initialize generic class with String data 12 | GenericsClassExample stringObj = new GenericsClassExample<>("generic class example"); 13 | System.out.println("Generic Class returns: " + stringObj.getData()); 14 | 15 | // Generic Method Example 16 | // initialize the class with Integer data 17 | GenericsMethodExample demo = new GenericsMethodExample(); 18 | 19 | // generics method working with String 20 | demo.genericsMethod("Generic Method working with string"); 21 | 22 | // generics method working with integer 23 | demo.genericsMethod(69); 24 | 25 | // create an object of GenericsClass here only subclass of Number(Integer, Double, and so on). can be used 26 | BoundedTypesExample obj = new BoundedTypesExample <>(); 27 | 28 | } 29 | 30 | } 31 | -------------------------------------------------------------------------------- /src/main/java/org/example/algorithms/sorting/PancakeSort.java: -------------------------------------------------------------------------------- 1 | package org.example.algorithms.sorting; 2 | 3 | import java.util.Arrays; 4 | 5 | public class PancakeSort { 6 | 7 | private static void flip (int[] arr, int size) { 8 | int prev, start = 0; 9 | 10 | while (start < size) { 11 | prev = arr[start]; 12 | arr[start] = arr[size]; 13 | arr[size] = prev; 14 | start++; 15 | size--; 16 | } 17 | } 18 | 19 | private static int getMax (int[] arr, int t) { 20 | int max = arr[0]; 21 | int maxIndex = 0; 22 | 23 | for (int i = 1; i < t; i++) { 24 | if (arr[i] > max) { 25 | max = arr[i]; 26 | maxIndex = i; 27 | } 28 | } 29 | 30 | return maxIndex; 31 | } 32 | 33 | public static void pancakeSort (int[] arr) { 34 | for (int i = arr.length; i > 1; i--) { 35 | int maxIndex = getMax(arr, i); 36 | 37 | flip(arr, maxIndex); 38 | flip(arr, i-1); 39 | } 40 | } 41 | 42 | public static void main(String args []) { 43 | int[] arr = { 3, 4, 1, 2 }; 44 | pancakeSort(arr); 45 | 46 | System.out.println(Arrays.toString(arr)); 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /src/main/java/org/example/socket/Client.java: -------------------------------------------------------------------------------- 1 | package org.example.socket; 2 | 3 | import java.net.Socket; 4 | import java.util.Scanner; 5 | import java.io.DataOutputStream; 6 | import java.io.DataInputStream; 7 | class Client{ 8 | public static void main(String[] args){ 9 | try{ 10 | System.out.println("Connecting to server."); 11 | 12 | // make connection to server listening in localhost at post 9999 13 | Socket socket = new Socket("localhost",9999); 14 | System.out.println("Request Accepted.\n Connection Established."); 15 | 16 | // creating output stream to send data from client to server 17 | DataOutputStream dos = new DataOutputStream(socket.getOutputStream()); 18 | 19 | //creating input stream to recieve data from server to client 20 | DataInputStream dis = new DataInputStream(socket.getInputStream()); 21 | String input = ""; 22 | while(true){ 23 | System.out.print("-: "); 24 | String message = new Scanner(System.in).nextLine(); 25 | 26 | // sending message from client to server 27 | dos.writeUTF(message); 28 | dos.flush(); 29 | 30 | //recieving message from server to client 31 | input = dis.readUTF(); 32 | System.out.println(input); 33 | 34 | } 35 | }catch(Exception e){ 36 | System.out.println(e.getMessage()); 37 | } 38 | } 39 | } -------------------------------------------------------------------------------- /src/main/java/org/example/datastructures/linkedlist/algo/LinkedListCycleDetection.java: -------------------------------------------------------------------------------- 1 | package org.example.datastructures.linkedlist.algo; 2 | 3 | import java.util.HashSet; 4 | import java.util.Set; 5 | 6 | public class LinkedListCycleDetection { 7 | 8 | static class Node { 9 | int data; 10 | Node next; 11 | Node(int tmp) {data = tmp;} 12 | } 13 | 14 | public static void main(String[] a){ 15 | 16 | Node n1 = new Node(56); 17 | Node n2 = new Node(89); 18 | Node n3 = new Node(3); 19 | Node n4 = new Node(59); 20 | Node n5 = new Node(77); 21 | n1.next = n2; 22 | n2.next = n3; 23 | n3.next = n4; 24 | n4.next = n5; 25 | n5.next = n3; 26 | 27 | System.out.println("Loop detected or not: " + identifyLoop(n1)); 28 | } 29 | 30 | 31 | static boolean identifyLoop(Node head) { 32 | 33 | 34 | Node slowPtr = head; 35 | Node fastPtr = head; 36 | while (slowPtr != null && fastPtr != null && fastPtr.next != null) { 37 | slowPtr = slowPtr.next; 38 | fastPtr = fastPtr.next.next; 39 | if(slowPtr == fastPtr) { 40 | return Boolean.TRUE; 41 | } 42 | } 43 | return Boolean.FALSE; 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /src/main/java/org/example/oop/abstraction/ReptileInterface.java: -------------------------------------------------------------------------------- 1 | package org.example.oop.abstraction; 2 | 3 | interface Reptiles { 4 | void eat(); 5 | void sleep(); 6 | } 7 | 8 | class Frog implements Reptiles{ 9 | // eat 10 | @Override 11 | public void eat(){ 12 | System.out.println("Eat"); 13 | } 14 | // sleep 15 | @Override 16 | public void sleep(){ 17 | System.out.println("Sleep"); 18 | } 19 | // hopping 20 | public void hop(){ 21 | System.out.println("Hop"); 22 | } 23 | } 24 | 25 | class Snake implements Reptiles{ 26 | // eat 27 | @Override 28 | public void eat(){ 29 | System.out.println("Eat"); 30 | } 31 | // walk 32 | @Override 33 | public void sleep(){ 34 | System.out.println("Sleep"); 35 | } 36 | // crawling 37 | public void crawl(){ 38 | System.out.println("Crawl"); 39 | } 40 | } 41 | 42 | class ReptileInterface { 43 | public static void main (String[] args) { 44 | System.out.println("Frog can:"); 45 | Frog frog = new Frog(); 46 | frog.eat(); 47 | frog.hop(); 48 | 49 | System.out.println("Snake can:"); 50 | Snake snake = new Snake(); 51 | snake.sleep(); 52 | snake.crawl(); 53 | } 54 | } 55 | 56 | 57 | 58 | -------------------------------------------------------------------------------- /src/main/java/org/example/compressiontechniques/runlengthencoder/RunLengthEncoder.java: -------------------------------------------------------------------------------- 1 | package org.example.compressiontechniques.runlengthencoder; 2 | 3 | public class RunLengthEncoder { 4 | public String compress(String str) { 5 | String compressedString = null; 6 | int n = str.length(); 7 | for (int i = 0; i < n; i++) { 8 | int count = 1; 9 | while (i < n - 1 && str.charAt(i) == str.charAt(i + 1)) { 10 | count++; 11 | i++; 12 | } 13 | if (count >= 4 && count <= 259) { 14 | if (compressedString == null) { 15 | compressedString = str.charAt(i) + "!" + count; 16 | } else { 17 | compressedString = compressedString + str.charAt(i) + "!" + count; 18 | } 19 | } else { 20 | for (int j = 1; j <= count; j++) { 21 | if (compressedString == null) { 22 | compressedString = String.valueOf(str.charAt(i)); 23 | } else { 24 | compressedString = compressedString + String.valueOf(str.charAt(i)); 25 | } 26 | 27 | } 28 | } 29 | } 30 | return compressedString; 31 | } 32 | } 33 | 34 | -------------------------------------------------------------------------------- /src/main/java/org/example/leetcode/MaxStackSum.java: -------------------------------------------------------------------------------- 1 | package org.example.leetcode; 2 | public class MaxStackSum { 3 | public static void main(String[] args) { 4 | int [] s1 = {4,2,3}; 5 | int [] s2 = {1,1,2,3}; 6 | int [] s3 = {1,4}; 7 | int n1 = 3 , n2 = 4, n3 = 2; 8 | int sum1 = 0 , sum2= 0, sum3 = 0; 9 | for(int i = 0 ; i