├── settings.gradle ├── images ├── file_example_1.jpg ├── file_example_2.jpg ├── basic_binary_tree.png ├── binary_search_tree.png └── adjacency_matrix_graph.jpeg ├── .mvn └── wrapper │ ├── maven-wrapper.jar │ └── maven-wrapper.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── src ├── main │ └── java │ │ └── com │ │ └── examplehub │ │ ├── basics │ │ ├── RecordExample.java │ │ ├── time │ │ │ ├── GetTime.java │ │ │ ├── DateExample.java │ │ │ ├── CalendarExample.java │ │ │ ├── InstantExample.java │ │ │ ├── LocalDateTimeExample.java │ │ │ └── DateTimeFormatExample.java │ │ ├── io │ │ │ ├── FileExample.java │ │ │ ├── FilesExample.java │ │ │ ├── PathsExample.java │ │ │ ├── FileWriterExample.java │ │ │ ├── BufferReaderExample.java │ │ │ ├── PrintStreamExample.java │ │ │ ├── StringReaderExample.java │ │ │ ├── CharArrayReaderExample.java │ │ │ ├── DataInputStreamExample.java │ │ │ ├── BufferInputStreamExample.java │ │ │ ├── DataOutputStreamExample.java │ │ │ ├── FileOutputStreamExample.java │ │ │ ├── RandomAccessFileExample.java │ │ │ ├── ObjectOutputInputStreamExample.java │ │ │ ├── DeleteEmptyFolder.java │ │ │ ├── DeleteFile.java │ │ │ ├── CreateFolder.java │ │ │ ├── ReadClasspathFile.java │ │ │ └── RecursionPrintDirectory.java │ │ ├── array │ │ │ └── ArrayExample.java │ │ ├── chars │ │ │ ├── CharExample.java │ │ │ ├── StringExample.java │ │ │ └── StringBuilderExample.java │ │ ├── encrypt │ │ │ ├── MD5Example.java │ │ │ ├── HashAlgoExample.java │ │ │ └── URLEncoderExample.java │ │ ├── ints │ │ │ └── IntOperation.java │ │ ├── map │ │ │ ├── EnumMapExample.java │ │ │ ├── HashMapExample.java │ │ │ ├── TreeMapExample.java │ │ │ └── PropertiesExample.java │ │ ├── oop │ │ │ ├── ObjectExample.java │ │ │ ├── InstanceofExample.java │ │ │ ├── ExampleClass.java │ │ │ ├── StaticInnerClass.java │ │ │ ├── StaticFieldFunctionExample.java │ │ │ ├── InnerClassExample.java │ │ │ ├── AbstractClassExample.java │ │ │ ├── FunctionOverloadExample.java │ │ │ ├── VarargsExample.java │ │ │ ├── FunctionOverriding.java │ │ │ ├── InterfaceExample.java │ │ │ ├── DeclareMethod.java │ │ │ ├── SetterGetter.java │ │ │ ├── AnonymousClass.java │ │ │ └── ReadJavaBeanProperties.java │ │ ├── queue │ │ │ ├── DequeExample.java │ │ │ ├── QueueExample.java │ │ │ ├── StackExample.java │ │ │ └── PriorityQueueExample.java │ │ ├── set │ │ │ ├── TreeSetExample.java │ │ │ └── ArrayListExample.java │ │ ├── thread │ │ │ ├── JoinThread.java │ │ │ ├── StopThread.java │ │ │ ├── DaemonThread.java │ │ │ ├── ExampleThread.java │ │ │ ├── SleepExample.java │ │ │ ├── CallableExample.java │ │ │ ├── ExampleRunnable.java │ │ │ ├── PriorityOfThread.java │ │ │ ├── VolatileExample.java │ │ │ ├── InterruptedThread.java │ │ │ ├── WaitNotifyExample.java │ │ │ ├── ReadWriteLockExample.java │ │ │ ├── ReentrantLockExample.java │ │ │ ├── TakeMoneyFromAccountExample.java │ │ │ ├── ReentrantLockConditionExample.java │ │ │ ├── YieldThread.java │ │ │ ├── StateOfThread.java │ │ │ ├── SellTicketThread.java │ │ │ ├── SynchronizedThread.java │ │ │ └── CopyOnWriteArrayListExample.java │ │ ├── generic │ │ │ ├── GenericClass.java │ │ │ └── GenericExample.java │ │ ├── number │ │ │ ├── DoubleExample.java │ │ │ ├── IntegerExample.java │ │ │ ├── BigDecimalExample.java │ │ │ └── BigIntegerExample.java │ │ ├── output │ │ │ └── OutputExample.java │ │ ├── regex │ │ │ ├── MatchesExample.java │ │ │ ├── PatternExample.java │ │ │ └── SplitReplaceExample.java │ │ ├── system │ │ │ ├── SystemExample.java │ │ │ └── FileSeparatorTest.java │ │ ├── collection │ │ │ ├── ListExample.java │ │ │ ├── StackExample.java │ │ │ ├── CollectionExample.java │ │ │ └── CollectionsExample.java │ │ ├── condition │ │ │ ├── BreakExample.java │ │ │ ├── SwitchExample.java │ │ │ └── ContinueExample.java │ │ ├── constant │ │ │ └── FinalExample.java │ │ ├── floats │ │ │ └── FloatOperation.java │ │ ├── utils │ │ │ └── MathUtilsExample.java │ │ ├── exception │ │ │ ├── FinallyExample.java │ │ │ ├── AssertionExample.java │ │ │ ├── ThrowsExceptionExample.java │ │ │ ├── ClassCastExceptionExample.java │ │ │ ├── ArithmeticExceptionExample.java │ │ │ ├── NullPointerExceptionExample.java │ │ │ ├── NumberFormatExceptionExample.java │ │ │ ├── IndexOutOfBoundsExceptionExample.java │ │ │ ├── CatchException.java │ │ │ └── CustomExceptionExample.java │ │ ├── reflection │ │ │ ├── GetSupperClass.java │ │ │ ├── GetClassExample.java │ │ │ ├── GetFieldExample.java │ │ │ ├── CheckClassIfPresent.java │ │ │ ├── CreateObjFromClass.java │ │ │ └── InvokeMethodExample.java │ │ ├── conversion │ │ │ ├── TypeConversionExample.java │ │ │ └── TypeCast.java │ │ ├── lambda │ │ │ └── SumWithLambda.java │ │ ├── HelloWorld.java │ │ ├── EnumExample.java │ │ ├── DateExample.java │ │ ├── loop │ │ │ └── WhileExample.java │ │ ├── function │ │ │ ├── MainFunctionArgs.java │ │ │ └── ModifyFunctionArgs.java │ │ ├── CalendarExample.java │ │ ├── ConvertStringToInteger.java │ │ ├── MultiClassInAFile.java │ │ ├── Variable.java │ │ ├── BreakWithLabel.java │ │ ├── InheritanceExample.java │ │ ├── ShutdownHook.java │ │ ├── ReturnExample.java │ │ ├── VarArgsExample.java │ │ ├── bool │ │ │ └── BooleanExample.java │ │ ├── RegexExample.java │ │ ├── MethodOverloading.java │ │ ├── GarbageCollectionExample.java │ │ ├── math │ │ │ └── MathExample.java │ │ ├── Comments.java │ │ └── StaticExample.java │ │ ├── designpatterns │ │ ├── decorator │ │ │ ├── Readable.java │ │ │ ├── Machine.java │ │ │ └── Decorator.java │ │ ├── factory │ │ │ ├── Sender.java │ │ │ ├── SenderFactory.java │ │ │ ├── SenderType.java │ │ │ ├── SmsSender.java │ │ │ ├── EmailSender.java │ │ │ ├── SmsSenderFactory.java │ │ │ └── EmailSenderFactory.java │ │ ├── singleton │ │ │ ├── SingletonExample3.java │ │ │ ├── SingletonExample1.java │ │ │ ├── SingletonExample4.java │ │ │ ├── SingletonExample6.java │ │ │ ├── SingletonExample2.java │ │ │ └── SingletonExample5.java │ │ ├── template │ │ │ ├── Plus.java │ │ │ ├── Minus.java │ │ │ └── Calculator.java │ │ └── proxy │ │ │ └── Proxy.java │ │ ├── sorts │ │ └── Sort.java │ │ ├── searches │ │ └── Search.java │ │ ├── maths │ │ ├── Ceil.java │ │ ├── Floor.java │ │ ├── FrogJump.java │ │ ├── GetTheFirstDigitRecursion.java │ │ ├── SumToNFormula.java │ │ ├── SumToNRecursion.java │ │ ├── SumToN.java │ │ ├── FactorialRecursion.java │ │ ├── LucasNumbers.java │ │ ├── AbsoluteValue.java │ │ ├── LeapYear.java │ │ ├── Factorial.java │ │ ├── ProductOfArray.java │ │ ├── MiddleIndexCalculate.java │ │ ├── SignFunction.java │ │ ├── CountDigits.java │ │ ├── CountDigitsRecursion.java │ │ ├── PowerRecursion.java │ │ ├── Median.java │ │ ├── AreAllTrue.java │ │ ├── Power.java │ │ ├── Average.java │ │ ├── SquareNumber.java │ │ ├── AbsoluteMax.java │ │ ├── AbsoluteMin.java │ │ ├── PowerOfTwoRecursion.java │ │ ├── PalindromeNumberByString.java │ │ ├── PowerOfTwo.java │ │ ├── CubeNumber.java │ │ ├── FibonacciRecursion.java │ │ ├── Hanoi.java │ │ ├── LucasNumbersIteration.java │ │ ├── FindMax.java │ │ ├── FindMin.java │ │ ├── PalindromeNumber.java │ │ ├── Allocation.java │ │ ├── Fibonacci.java │ │ └── Subtract.java │ │ ├── leetcode │ │ ├── easy │ │ │ ├── ClimbStairs.java │ │ │ ├── RunningSumOf1dArray.java │ │ │ ├── DeleteNodeInALinkedList.java │ │ │ ├── ShuffleString.java │ │ │ ├── ContainsDuplicateII.java │ │ │ ├── NumberOfGoodPairs.java │ │ │ ├── CountNegativeNumbersInASortedMatrix.java │ │ │ ├── ShuffleTheArray.java │ │ │ ├── RemoveDuplicatesFromSortedArray.java │ │ │ ├── PathSum.java │ │ │ ├── ReverseLinkedList.java │ │ │ ├── SubtractTheProductAndSumOfDigitsOfAnInteger.java │ │ │ ├── RichestCustomerWealth.java │ │ │ ├── SameTree.java │ │ │ ├── SumOfAllOddLengthSubarrays.java │ │ │ ├── RemoveDuplicatesFromSortedList.java │ │ │ ├── ThirdMaximumNumber.java │ │ │ ├── ConvertBinaryNumberInALinkedListToInteger.java │ │ │ ├── BalancedBinaryTree.java │ │ │ ├── SumOfUniqueElements.java │ │ │ └── FindLuckyIntegerInAnArray.java │ │ ├── middle │ │ │ └── ThreeSum.java │ │ ├── TreeNode.java │ │ └── utils │ │ │ └── BinaryTreeUtils.java │ │ ├── strings │ │ ├── ReverseString.java │ │ ├── RemoveWhiteSpace.java │ │ ├── PalindromeStringByReverse.java │ │ ├── JustAppearOneTime.java │ │ ├── PalindromeString.java │ │ └── PalindromeStringRecursion.java │ │ ├── projecteuler │ │ ├── Problem20.java │ │ ├── Problem01.java │ │ ├── Problem10.java │ │ ├── Problem03.java │ │ ├── Problem05.java │ │ ├── Problem07.java │ │ ├── Problem09.java │ │ ├── Problem06.java │ │ ├── Problem21.java │ │ ├── Problem30.java │ │ ├── Problem34.java │ │ ├── Problem02.java │ │ └── Problem12.java │ │ ├── datastructures │ │ ├── linkedlist │ │ │ └── Node.java │ │ ├── binarytree │ │ │ └── Node.java │ │ ├── array │ │ │ └── ReverseArray.java │ │ └── stack │ │ │ └── ExpressionEvaluation.java │ │ ├── conversions │ │ ├── DecimalToOctal.java │ │ └── DecimalToBinary.java │ │ └── dynamicprogramming │ │ └── FactorialDP.java └── test │ └── java │ └── com │ └── examplehub │ ├── basics │ ├── network │ │ └── SocketServerTest.java │ ├── map │ │ ├── PropertiesExampleTest.java │ │ └── TreeMapExampleTest.java │ ├── time │ │ ├── GetTimeTest.java │ │ ├── InstantExampleTest.java │ │ └── DateTimeFormatExampleTest.java │ ├── io │ │ ├── DeleteEmptyFolderTest.java │ │ ├── RecursionPrintDirectoryTest.java │ │ ├── CreateFolderTest.java │ │ ├── PathsExampleTest.java │ │ ├── DeleteFileTest.java │ │ └── StringReaderExampleTest.java │ ├── thread │ │ ├── YieldThreadTest.java │ │ ├── PriorityOfThreadTest.java │ │ └── SellTicketThreadTest.java │ ├── oop │ │ ├── InstanceofExampleTest.java │ │ ├── StaticInnerClassTest.java │ │ ├── AbstractClassExampleTest.java │ │ ├── DeclareMethodTest.java │ │ ├── UpcastExampleTest.java │ │ ├── ExampleClassTest.java │ │ ├── ExtendExampleTest.java │ │ ├── InterfaceExampleTest.java │ │ ├── InnerClassExampleTest.java │ │ ├── AnonymousClassTest.java │ │ ├── VarargsExampleTest.java │ │ ├── FunctionOverloadExampleTest.java │ │ └── StaticFieldFunctionExampleTest.java │ ├── exception │ │ ├── ArithmeticExceptionExampleTest.java │ │ ├── NumberFormatExceptionExampleTest.java │ │ ├── CustomExceptionExampleTest.java │ │ ├── NullPointerExceptionExampleTest.java │ │ ├── AssertionExampleTest.java │ │ ├── IndexOutOfBoundsExceptionExampleTest.java │ │ └── ClassCastExceptionExampleTest.java │ ├── condition │ │ └── ContinueExampleTest.java │ ├── reflection │ │ └── GetSupperClassTest.java │ ├── regex │ │ └── SplitReplaceExampleTest.java │ ├── system │ │ ├── SystemExampleTest.java │ │ └── FileSeparatorTestTest.java │ ├── lambda │ │ └── SumWithLambdaTest.java │ ├── set │ │ └── LinkedListExampleTest.java │ ├── encrypt │ │ ├── URLEncoderExampleTest.java │ │ └── HashAlgoExampleTest.java │ └── generic │ │ └── GenericClassTest.java │ ├── maths │ ├── HanoiTest.java │ ├── SumToNFormulaTest.java │ ├── ModeTest.java │ ├── AbsoluteMinTest.java │ ├── CubeNumberTest.java │ ├── ProductOfArrayTest.java │ ├── LucasNumbersTest.java │ ├── SumToNTest.java │ ├── FibonacciBigNumberTest.java │ ├── NextGreaterElementTest.java │ ├── CharToIntTest.java │ ├── LucasNumbersIterationTest.java │ ├── SignFunctionTest.java │ ├── FrogJumpTest.java │ ├── LeapYearTest.java │ ├── PowerOfTwoTest.java │ ├── AbsoluteMaxTest.java │ ├── CeilTest.java │ ├── GetTheFirstDigitRecursionTest.java │ ├── SumToNRecursionTest.java │ ├── AverageTest.java │ ├── RangeTest.java │ ├── AbsoluteValueTest.java │ ├── MedianTest.java │ ├── CountDigitsTest.java │ ├── AreAllTrueTest.java │ ├── PowerOfTwoRecursionTest.java │ ├── PowerTest.java │ ├── PrimeFactorsTest.java │ ├── GetTheFirstDigitTest.java │ ├── GreatestCommonDivisorTest.java │ ├── CountDigitsRecursionTest.java │ ├── DutchNationalFlagTest.java │ ├── SubtractTest.java │ ├── SquareNumberTest.java │ ├── PerfectNumberTest.java │ ├── PowerRecursionTest.java │ ├── AllocationTest.java │ ├── MiddleIndexCalculateTest.java │ └── PalindromeNumberTest.java │ ├── projecteuler │ ├── Problem04Test.java │ ├── Problem20Test.java │ ├── Problem34Test.java │ ├── Problem30Test.java │ ├── Problem41Test.java │ ├── Problem12Test.java │ ├── Problem21Test.java │ ├── Problem02Test.java │ ├── Problem06Test.java │ ├── Problem09Test.java │ ├── Problem10Test.java │ ├── Problem25Test.java │ ├── Problem05Test.java │ ├── Problem01Test.java │ └── Problem07Test.java │ ├── designpatterns │ ├── proxy │ │ └── ProxyTest.java │ ├── singleton │ │ ├── SingletonExample3Test.java │ │ ├── SingletonExample1Test.java │ │ ├── SingletonExample2Test.java │ │ ├── SingletonExample4Test.java │ │ ├── SingletonExample5Test.java │ │ └── SingletonExample6Test.java │ ├── decorator │ │ └── DecoratorTest.java │ └── template │ │ └── CalculatorTest.java │ ├── leetcode │ ├── middle │ │ ├── ThreeSumTest.java │ │ ├── LongestPalindromicSubstringTest.java │ │ └── MultiplyStringsTest.java │ ├── easy │ │ ├── PathSumIITest.java │ │ ├── Maximum69NumberTest.java │ │ ├── SelfDividingNumbersTest.java │ │ ├── ThirdMaximumNumberTest.java │ │ ├── ShuffleTheArrayTest.java │ │ ├── SortArrayByParityTest.java │ │ ├── SubtractTheProductAndSumOfDigitsOfAnIntegerTest.java │ │ ├── NumberOfGoodPairsTest.java │ │ ├── HappyNumberTest.java │ │ ├── ClimbStairsTest.java │ │ ├── ContainsDuplicateIITest.java │ │ ├── SumOfUniqueElementsTest.java │ │ ├── SumOfAllOddLengthSubarraysTest.java │ │ └── ImplementQueueUsingStacksTest.java │ └── utils │ │ └── BinaryTreeUtilsTest.java │ ├── strings │ ├── ReverseStringTest.java │ ├── JustAppearOneTimeTest.java │ ├── ReplaceWhiteSpaceTest.java │ ├── StringJoinerExampleTest.java │ ├── PalindromeTest.java │ ├── PalindromeStringByReverseTest.java │ ├── RemoveWhiteSpaceTest.java │ └── PalindromeStringRecursionTest.java │ ├── conversions │ ├── BinaryToOctalTest.java │ ├── BinaryToDecimalTest.java │ ├── BinaryToHexadecimalTest.java │ ├── DecimalToHexlTest.java │ ├── DecimalToBinaryTest.java │ └── DecimalToOctalTest.java │ ├── dynamicprogramming │ ├── LucasDPTest.java │ └── FibonacciDPTest.java │ ├── datastructures │ └── array │ │ ├── ReverseArrayTest.java │ │ └── MergeSortedArrayTest.java │ ├── matrix │ ├── PrecedencesTest.java │ ├── SortMatrixTest.java │ └── LowerTriangleMatrixTest.java │ └── utils │ ├── NodeUtilsTest.java │ └── SortUtilsTest.java ├── .gitignore └── .github └── workflows ├── maven.yml └── gradle.yml /settings.gradle: -------------------------------------------------------------------------------- 1 | rootProject.name = 'Java' 2 | 3 | -------------------------------------------------------------------------------- /images/file_example_1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/examplehub/Java/HEAD/images/file_example_1.jpg -------------------------------------------------------------------------------- /images/file_example_2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/examplehub/Java/HEAD/images/file_example_2.jpg -------------------------------------------------------------------------------- /images/basic_binary_tree.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/examplehub/Java/HEAD/images/basic_binary_tree.png -------------------------------------------------------------------------------- /.mvn/wrapper/maven-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/examplehub/Java/HEAD/.mvn/wrapper/maven-wrapper.jar -------------------------------------------------------------------------------- /images/binary_search_tree.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/examplehub/Java/HEAD/images/binary_search_tree.png -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/examplehub/Java/HEAD/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /images/adjacency_matrix_graph.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/examplehub/Java/HEAD/images/adjacency_matrix_graph.jpeg -------------------------------------------------------------------------------- /src/main/java/com/examplehub/basics/RecordExample.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.basics; 2 | 3 | public class RecordExample {} 4 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/basics/time/GetTime.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.basics.time; 2 | 3 | public class GetTime {} 4 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/basics/io/FileExample.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.basics.io; 2 | 3 | public class FileExample {} 4 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/basics/io/FilesExample.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.basics.io; 2 | 3 | public class FilesExample {} 4 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/basics/io/PathsExample.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.basics.io; 2 | 3 | public class PathsExample {} 4 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/basics/array/ArrayExample.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.basics.array; 2 | 3 | public class ArrayExample {} 4 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/basics/chars/CharExample.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.basics.chars; 2 | 3 | public class CharExample {} 4 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/basics/encrypt/MD5Example.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.basics.encrypt; 2 | 3 | public class MD5Example {} 4 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/basics/ints/IntOperation.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.basics.ints; 2 | 3 | public class IntOperation {} 4 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/basics/map/EnumMapExample.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.basics.map; 2 | 3 | public class EnumMapExample {} 4 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/basics/map/HashMapExample.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.basics.map; 2 | 3 | public class HashMapExample {} 4 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/basics/map/TreeMapExample.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.basics.map; 2 | 3 | public class TreeMapExample {} 4 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/basics/oop/ObjectExample.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.basics.oop; 2 | 3 | public class ObjectExample {} 4 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/basics/queue/DequeExample.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.basics.queue; 2 | 3 | public class DequeExample {} 4 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/basics/queue/QueueExample.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.basics.queue; 2 | 3 | public class QueueExample {} 4 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/basics/queue/StackExample.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.basics.queue; 2 | 3 | public class StackExample {} 4 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/basics/set/TreeSetExample.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.basics.set; 2 | 3 | public class TreeSetExample {} 4 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/basics/thread/JoinThread.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.basics.thread; 2 | 3 | public class JoinThread {} 4 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/basics/thread/StopThread.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.basics.thread; 2 | 3 | public class StopThread {} 4 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/basics/time/DateExample.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.basics.time; 2 | 3 | public class DateExample {} 4 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/basics/chars/StringExample.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.basics.chars; 2 | 3 | public class StringExample {} 4 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/basics/generic/GenericClass.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.basics.generic; 2 | 3 | public class GenericClass {} 4 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/basics/io/FileWriterExample.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.basics.io; 2 | 3 | public class FileWriterExample {} 4 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/basics/number/DoubleExample.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.basics.number; 2 | 3 | public class DoubleExample {} 4 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/basics/output/OutputExample.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.basics.output; 2 | 3 | public class OutputExample {} 4 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/basics/regex/MatchesExample.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.basics.regex; 2 | 3 | public class MatchesExample {} 4 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/basics/regex/PatternExample.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.basics.regex; 2 | 3 | public class PatternExample {} 4 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/basics/set/ArrayListExample.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.basics.set; 2 | 3 | public class ArrayListExample {} 4 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/basics/system/SystemExample.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.basics.system; 2 | 3 | public class SystemExample {} 4 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/basics/thread/DaemonThread.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.basics.thread; 2 | 3 | public class DaemonThread {} 4 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/basics/thread/ExampleThread.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.basics.thread; 2 | 3 | public class ExampleThread {} 4 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/basics/thread/SleepExample.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.basics.thread; 2 | 3 | public class SleepExample {} 4 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/basics/time/CalendarExample.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.basics.time; 2 | 3 | public class CalendarExample {} 4 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/basics/time/InstantExample.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.basics.time; 2 | 3 | public class InstantExample {} 4 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/basics/collection/ListExample.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.basics.collection; 2 | 3 | public class ListExample {} 4 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/basics/collection/StackExample.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.basics.collection; 2 | 3 | public class StackExample {} 4 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/basics/condition/BreakExample.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.basics.condition; 2 | 3 | public class BreakExample {} 4 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/basics/condition/SwitchExample.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.basics.condition; 2 | 3 | public class SwitchExample {} 4 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/basics/constant/FinalExample.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.basics.constant; 2 | 3 | public class FinalExample {} 4 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/basics/encrypt/HashAlgoExample.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.basics.encrypt; 2 | 3 | public class HashAlgoExample {} 4 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/basics/floats/FloatOperation.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.basics.floats; 2 | 3 | public class FloatOperation {} 4 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/basics/generic/GenericExample.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.basics.generic; 2 | 3 | public class GenericExample {} 4 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/basics/io/BufferReaderExample.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.basics.io; 2 | 3 | public class BufferReaderExample {} 4 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/basics/io/PrintStreamExample.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.basics.io; 2 | 3 | public class PrintStreamExample {} 4 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/basics/io/StringReaderExample.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.basics.io; 2 | 3 | public class StringReaderExample {} 4 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/basics/map/PropertiesExample.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.basics.map; 2 | 3 | public class PropertiesExample {} 4 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/basics/number/IntegerExample.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.basics.number; 2 | 3 | public class IntegerExample {} 4 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/basics/oop/InstanceofExample.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.basics.oop; 2 | 3 | public class InstanceofExample {} 4 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/basics/thread/CallableExample.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.basics.thread; 2 | 3 | public class CallableExample {} 4 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/basics/thread/ExampleRunnable.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.basics.thread; 2 | 3 | public class ExampleRunnable {} 4 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/basics/thread/PriorityOfThread.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.basics.thread; 2 | 3 | public class PriorityOfThread {} 4 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/basics/thread/VolatileExample.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.basics.thread; 2 | 3 | public class VolatileExample {} 4 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/basics/utils/MathUtilsExample.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.basics.utils; 2 | 3 | public class MathUtilsExample {} 4 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/basics/condition/ContinueExample.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.basics.condition; 2 | 3 | public class ContinueExample {} 4 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/basics/encrypt/URLEncoderExample.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.basics.encrypt; 2 | 3 | public class URLEncoderExample {} 4 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/basics/exception/FinallyExample.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.basics.exception; 2 | 3 | public class FinallyExample {} 4 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/basics/io/CharArrayReaderExample.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.basics.io; 2 | 3 | public class CharArrayReaderExample {} 4 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/basics/io/DataInputStreamExample.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.basics.io; 2 | 3 | public class DataInputStreamExample {} 4 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/basics/number/BigDecimalExample.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.basics.number; 2 | 3 | public class BigDecimalExample {} 4 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/basics/number/BigIntegerExample.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.basics.number; 2 | 3 | public class BigIntegerExample {} 4 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/basics/reflection/GetSupperClass.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.basics.reflection; 2 | 3 | public class GetSupperClass {} 4 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/basics/regex/SplitReplaceExample.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.basics.regex; 2 | 3 | public class SplitReplaceExample {} 4 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/basics/system/FileSeparatorTest.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.basics.system; 2 | 3 | public class FileSeparatorTest {} 4 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/basics/thread/InterruptedThread.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.basics.thread; 2 | 3 | public class InterruptedThread {} 4 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/basics/thread/WaitNotifyExample.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.basics.thread; 2 | 3 | public class WaitNotifyExample {} 4 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/basics/time/LocalDateTimeExample.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.basics.time; 2 | 3 | public class LocalDateTimeExample {} 4 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/basics/chars/StringBuilderExample.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.basics.chars; 2 | 3 | public class StringBuilderExample {} 4 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/basics/collection/CollectionExample.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.basics.collection; 2 | 3 | public class CollectionExample {} 4 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/basics/exception/AssertionExample.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.basics.exception; 2 | 3 | public class AssertionExample {} 4 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/basics/io/BufferInputStreamExample.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.basics.io; 2 | 3 | public class BufferInputStreamExample {} 4 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/basics/io/DataOutputStreamExample.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.basics.io; 2 | 3 | public class DataOutputStreamExample {} 4 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/basics/io/FileOutputStreamExample.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.basics.io; 2 | 3 | public class FileOutputStreamExample {} 4 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/basics/io/RandomAccessFileExample.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.basics.io; 2 | 3 | public class RandomAccessFileExample {} 4 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/basics/queue/PriorityQueueExample.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.basics.queue; 2 | 3 | public class PriorityQueueExample {} 4 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/basics/reflection/GetClassExample.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.basics.reflection; 2 | 3 | public class GetClassExample {} 4 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/basics/reflection/GetFieldExample.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.basics.reflection; 2 | 3 | public class GetFieldExample {} 4 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/basics/thread/ReadWriteLockExample.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.basics.thread; 2 | 3 | public class ReadWriteLockExample {} 4 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/basics/thread/ReentrantLockExample.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.basics.thread; 2 | 3 | public class ReentrantLockExample {} 4 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/basics/time/DateTimeFormatExample.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.basics.time; 2 | 3 | public class DateTimeFormatExample {} 4 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/basics/collection/CollectionsExample.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.basics.collection; 2 | 3 | public class CollectionsExample {} 4 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/basics/reflection/CheckClassIfPresent.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.basics.reflection; 2 | 3 | public class CheckClassIfPresent {} 4 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/basics/reflection/CreateObjFromClass.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.basics.reflection; 2 | 3 | public class CreateObjFromClass {} 4 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/basics/reflection/InvokeMethodExample.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.basics.reflection; 2 | 3 | public class InvokeMethodExample {} 4 | -------------------------------------------------------------------------------- /src/test/java/com/examplehub/basics/network/SocketServerTest.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.basics.network; 2 | 3 | class SocketServerTest { 4 | // TODO 5 | } 6 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/basics/conversion/TypeConversionExample.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.basics.conversion; 2 | 3 | public class TypeConversionExample {} 4 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/basics/exception/ThrowsExceptionExample.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.basics.exception; 2 | 3 | public class ThrowsExceptionExample {} 4 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/basics/io/ObjectOutputInputStreamExample.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.basics.io; 2 | 3 | public class ObjectOutputInputStreamExample {} 4 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/basics/exception/ClassCastExceptionExample.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.basics.exception; 2 | 3 | public class ClassCastExceptionExample {} 4 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/basics/thread/TakeMoneyFromAccountExample.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.basics.thread; 2 | 3 | public class TakeMoneyFromAccountExample {} 4 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/basics/exception/ArithmeticExceptionExample.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.basics.exception; 2 | 3 | public class ArithmeticExceptionExample {} 4 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/basics/exception/NullPointerExceptionExample.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.basics.exception; 2 | 3 | public class NullPointerExceptionExample {} 4 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/basics/exception/NumberFormatExceptionExample.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.basics.exception; 2 | 3 | public class NumberFormatExceptionExample {} 4 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/basics/thread/ReentrantLockConditionExample.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.basics.thread; 2 | 3 | public class ReentrantLockConditionExample {} 4 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/basics/lambda/SumWithLambda.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.basics.lambda; 2 | 3 | public interface SumWithLambda { 4 | public int sum(int n); 5 | } 6 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/designpatterns/decorator/Readable.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.designpatterns.decorator; 2 | 3 | public interface Readable { 4 | void read(); 5 | } 6 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/basics/exception/IndexOutOfBoundsExceptionExample.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.basics.exception; 2 | 3 | public class IndexOutOfBoundsExceptionExample {} 4 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/basics/oop/ExampleClass.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.basics.oop; 2 | 3 | public class ExampleClass { 4 | public String name; 5 | public int age; 6 | } 7 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/designpatterns/factory/Sender.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.designpatterns.factory; 2 | 3 | public interface Sender { 4 | String send(String message); 5 | } 6 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/designpatterns/factory/SenderFactory.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.designpatterns.factory; 2 | 3 | public interface SenderFactory { 4 | Sender build(); 5 | } 6 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/designpatterns/singleton/SingletonExample3.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.designpatterns.singleton; 2 | 3 | public enum SingletonExample3 { 4 | INSTANCE 5 | } 6 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/designpatterns/factory/SenderType.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.designpatterns.factory; 2 | 3 | public enum SenderType { 4 | EMAIL_SENDER, 5 | SMS_SENDER 6 | } 7 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/sorts/Sort.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.sorts; 2 | 3 | public interface Sort { 4 | void sort(int[] numbers); 5 | 6 | > void sort(T[] array); 7 | } 8 | -------------------------------------------------------------------------------- /src/test/java/com/examplehub/basics/map/PropertiesExampleTest.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.basics.map; 2 | 3 | import static org.junit.jupiter.api.Assertions.*; 4 | 5 | class PropertiesExampleTest {} 6 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/searches/Search.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.searches; 2 | 3 | public interface Search { 4 | int search(int[] numbers, int key); 5 | 6 | > int search(T[] array, T key); 7 | } 8 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/basics/HelloWorld.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.basics; 2 | 3 | public class HelloWorld { 4 | public static void main(String[] args) { 5 | System.out.println("Hello World from ExampleHub"); 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/basics/EnumExample.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.basics; 2 | 3 | public class EnumExample {} 4 | 5 | enum WeekDay { 6 | SUN, 7 | MON, 8 | TUE, 9 | WED, 10 | THU, 11 | FRI, 12 | SAT 13 | } 14 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/maths/Ceil.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.maths; 2 | 3 | public class Ceil { 4 | public static int ceil(double number) { 5 | return number - (int) number <= 0 ? (int) number : (int) number + 1; 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/maths/Floor.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.maths; 2 | 3 | public class Floor { 4 | public static int floor(double number) { 5 | return number - (int) number >= 0 ? (int) number : (int) number - 1; 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/designpatterns/template/Plus.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.designpatterns.template; 2 | 3 | public class Plus extends Calculator { 4 | @Override 5 | public int calculate(int a, int b) { 6 | return a + b; 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/maths/FrogJump.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.maths; 2 | 3 | public class FrogJump { 4 | public static int canCross(int leaf, int stone) { 5 | return stone == 0 ? leaf + 1 : 2 * canCross(leaf, stone - 1); 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /.mvn/wrapper/maven-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.6.1/apache-maven-3.6.1-bin.zip 2 | wrapperUrl=https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/0.5.5/maven-wrapper-0.5.5.jar 3 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionBase=GRADLE_USER_HOME 2 | distributionPath=wrapper/dists 3 | distributionUrl=https\://services.gradle.org/distributions/gradle-6.3-bin.zip 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/basics/io/DeleteEmptyFolder.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.basics.io; 2 | 3 | public class DeleteEmptyFolder { 4 | public static boolean delete(String folderPath) { 5 | return DeleteFile.deleteFile(folderPath); 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/designpatterns/template/Minus.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.designpatterns.template; 2 | 3 | public class Minus extends Calculator { 4 | @Override 5 | public int calculate(int a, int b) { 6 | return a - b; 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/basics/DateExample.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.basics; 2 | 3 | import java.util.Date; 4 | 5 | public class DateExample { 6 | public static void main(String[] args) { 7 | System.out.println(new Date()); 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/basics/loop/WhileExample.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.basics.loop; 2 | 3 | public class WhileExample { 4 | /* 5 | * Syntax 6 | * while (condition) { 7 | * // code block to be executed 8 | * } 9 | */ 10 | } 11 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/designpatterns/decorator/Machine.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.designpatterns.decorator; 2 | 3 | public class Machine implements Readable { 4 | @Override 5 | public void read() { 6 | System.out.println("I'm reading"); 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/designpatterns/factory/SmsSender.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.designpatterns.factory; 2 | 3 | public class SmsSender implements Sender { 4 | @Override 5 | public String send(String message) { 6 | return "sms:" + message; 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/designpatterns/factory/EmailSender.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.designpatterns.factory; 2 | 3 | public class EmailSender implements Sender { 4 | @Override 5 | public String send(String message) { 6 | return "email:" + message; 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/designpatterns/factory/SmsSenderFactory.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.designpatterns.factory; 2 | 3 | public class SmsSenderFactory implements SenderFactory { 4 | @Override 5 | public Sender build() { 6 | return new SmsSender(); 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /src/test/java/com/examplehub/basics/time/GetTimeTest.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.basics.time; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | class GetTimeTest { 6 | @Test 7 | void test() { 8 | System.out.println(System.currentTimeMillis()); 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/designpatterns/factory/EmailSenderFactory.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.designpatterns.factory; 2 | 3 | public class EmailSenderFactory implements SenderFactory { 4 | @Override 5 | public Sender build() { 6 | return new EmailSender(); 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/basics/function/MainFunctionArgs.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.basics.function; 2 | 3 | public class MainFunctionArgs { 4 | public static void main(String[] args) { 5 | for (String arg : args) { 6 | System.out.println(arg); 7 | } 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/basics/io/DeleteFile.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.basics.io; 2 | 3 | import java.io.File; 4 | 5 | public class DeleteFile { 6 | public static boolean deleteFile(String path) { 7 | File file = new File(path); 8 | return file.delete(); 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/basics/io/CreateFolder.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.basics.io; 2 | 3 | import java.io.File; 4 | 5 | public class CreateFolder { 6 | public static boolean createFolder(String path) { 7 | File file = new File(path); 8 | return file.mkdir(); 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/basics/oop/StaticInnerClass.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.basics.oop; 2 | 3 | public class StaticInnerClass { 4 | private static String name = "JACK"; 5 | 6 | static class InnerClass { 7 | String sayHi() { 8 | return "Hi, " + name; 9 | } 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/leetcode/easy/ClimbStairs.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.leetcode.easy; 2 | 3 | /** https://leetcode.com/problems/climbing-stairs/ */ 4 | public class ClimbStairs { 5 | public static int solution1(int n) { 6 | return n <= 2 ? n : solution1(n - 1) + solution1(n - 2); 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /src/test/java/com/examplehub/maths/HanoiTest.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.maths; 2 | 3 | import static org.junit.jupiter.api.Assertions.*; 4 | 5 | import org.junit.jupiter.api.Test; 6 | 7 | class HanoiTest { 8 | 9 | @Test 10 | void test() { 11 | Hanoi.move(5, "A", "B", "C"); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/maths/GetTheFirstDigitRecursion.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.maths; 2 | 3 | public class GetTheFirstDigitRecursion { 4 | 5 | public static int firstDigit(int number) { 6 | number = Math.abs(number); 7 | return number < 10 ? number : firstDigit(number / 10); 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/basics/CalendarExample.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.basics; 2 | 3 | import java.util.Calendar; 4 | 5 | public class CalendarExample { 6 | public static void main(String[] args) { 7 | Calendar calendar = Calendar.getInstance(); 8 | System.out.println(calendar); 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/basics/ConvertStringToInteger.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.basics; 2 | 3 | public class ConvertStringToInteger { 4 | public static void main(String[] args) { 5 | String str = "123"; 6 | int number = Integer.parseInt(str); 7 | System.out.println(number); /* 123 */ 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/basics/MultiClassInAFile.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.basics; 2 | 3 | public class MultiClassInAFile { 4 | public static void main(String[] args) { 5 | A a = new A(); 6 | B b = new B(); 7 | C c = new C(); 8 | } 9 | } 10 | 11 | class A {} 12 | 13 | class B {} 14 | 15 | class C {} 16 | -------------------------------------------------------------------------------- /src/test/java/com/examplehub/basics/io/DeleteEmptyFolderTest.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.basics.io; 2 | 3 | import static org.junit.jupiter.api.Assertions.*; 4 | 5 | import java.io.IOException; 6 | import org.junit.jupiter.api.Test; 7 | 8 | class DeleteEmptyFolderTest { 9 | @Test 10 | void test() throws IOException {} 11 | } 12 | -------------------------------------------------------------------------------- /src/test/java/com/examplehub/projecteuler/Problem04Test.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.projecteuler; 2 | 3 | import static org.junit.jupiter.api.Assertions.*; 4 | 5 | import org.junit.jupiter.api.Test; 6 | 7 | class Problem04Test { 8 | 9 | @Test 10 | void test() { 11 | assertEquals(906609, Problem04.solution1()); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /src/test/java/com/examplehub/projecteuler/Problem20Test.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.projecteuler; 2 | 3 | import static org.junit.jupiter.api.Assertions.*; 4 | 5 | import org.junit.jupiter.api.Test; 6 | 7 | class Problem20Test { 8 | 9 | @Test 10 | void test() { 11 | assertEquals(27, Problem20.solution1(10)); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /src/test/java/com/examplehub/projecteuler/Problem34Test.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.projecteuler; 2 | 3 | import static org.junit.jupiter.api.Assertions.*; 4 | 5 | import org.junit.jupiter.api.Test; 6 | 7 | class Problem34Test { 8 | 9 | @Test 10 | void test() { 11 | assertEquals(40730, Problem34.solution1()); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/maths/SumToNFormula.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.maths; 2 | 3 | public class SumToNFormula { 4 | 5 | /** 6 | * Sum of 1 2 3 4 ... n. 7 | * 8 | * @param n the last item. 9 | * @return sum of n items. 10 | */ 11 | public static int sum(int n) { 12 | return n * (n + 1) / 2; 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /src/test/java/com/examplehub/projecteuler/Problem30Test.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.projecteuler; 2 | 3 | import static org.junit.jupiter.api.Assertions.*; 4 | 5 | import org.junit.jupiter.api.Test; 6 | 7 | class Problem30Test { 8 | @Test 9 | void testSolution1() { 10 | assertEquals(443839, Problem30.solution1()); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/basics/exception/CatchException.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.basics.exception; 2 | 3 | public class CatchException { 4 | public static double divide(double a, double b) throws ArithmeticException { 5 | if (b == 0) { 6 | throw new ArithmeticException("division is zero"); 7 | } 8 | return a / b; 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /src/test/java/com/examplehub/basics/thread/YieldThreadTest.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.basics.thread; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | class YieldThreadTest { 6 | @Test 7 | void test() { 8 | YieldThread yieldThread = new YieldThread(); 9 | new Thread(yieldThread).start(); 10 | new Thread(yieldThread).start(); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/maths/SumToNRecursion.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.maths; 2 | 3 | public class SumToNRecursion { 4 | 5 | /** 6 | * Sum from 1 to n using recursion 7 | * 8 | * @param n the limitation. 9 | * @return sum of 1 to n. 10 | */ 11 | public static int sum(int n) { 12 | return n == 0 ? 0 : n + sum(n - 1); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /src/test/java/com/examplehub/projecteuler/Problem41Test.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.projecteuler; 2 | 3 | import static org.junit.jupiter.api.Assertions.*; 4 | 5 | import org.junit.jupiter.api.Test; 6 | 7 | class Problem41Test { 8 | @Test 9 | void testSolution1() { 10 | // assertEquals(7652413, Problem41.solution1()); 11 | // TODO 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/basics/io/ReadClasspathFile.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.basics.io; 2 | 3 | import java.io.InputStream; 4 | 5 | public class ReadClasspathFile { 6 | public static void main(String[] args) { 7 | InputStream inputStream = ReadClasspathFile.class.getResourceAsStream("default.properties"); 8 | System.out.println(inputStream); 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/strings/ReverseString.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.strings; 2 | 3 | public class ReverseString { 4 | 5 | /** 6 | * Reverse a string. 7 | * 8 | * @param s the string. 9 | * @return the string reversed. 10 | */ 11 | public static String reverse(String s) { 12 | return new StringBuilder(s).reverse().toString(); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /src/test/java/com/examplehub/maths/SumToNFormulaTest.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.maths; 2 | 3 | import static org.junit.jupiter.api.Assertions.*; 4 | 5 | import org.junit.jupiter.api.Test; 6 | 7 | class SumToNFormulaTest { 8 | @Test 9 | void test() { 10 | assertEquals(55, SumToNFormula.sum(10)); 11 | assertEquals(5050, SumToNFormula.sum(100)); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/designpatterns/singleton/SingletonExample1.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.designpatterns.singleton; 2 | 3 | public class SingletonExample1 { 4 | private static final SingletonExample1 INSTANCE = new SingletonExample1(); 5 | 6 | private SingletonExample1() {} 7 | 8 | public static SingletonExample1 getInstance() { 9 | return INSTANCE; 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/leetcode/easy/RunningSumOf1dArray.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.leetcode.easy; 2 | 3 | /** https://leetcode.com/problems/running-sum-of-1d-array/ */ 4 | public class RunningSumOf1dArray { 5 | public static int[] solution1(int[] nums) { 6 | for (int i = 1; i < nums.length; ++i) { 7 | nums[i] += nums[i - 1]; 8 | } 9 | return nums; 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /src/test/java/com/examplehub/projecteuler/Problem12Test.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.projecteuler; 2 | 3 | import static org.junit.jupiter.api.Assertions.*; 4 | 5 | import org.junit.jupiter.api.Test; 6 | 7 | class Problem12Test { 8 | @Test 9 | void test() { 10 | assertEquals(28, Problem12.solution1(5)); 11 | // assertEquals(76576500, Problem12.solution1(500)); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /src/test/java/com/examplehub/projecteuler/Problem21Test.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.projecteuler; 2 | 3 | import static org.junit.jupiter.api.Assertions.*; 4 | 5 | import org.junit.jupiter.api.Test; 6 | 7 | class Problem21Test { 8 | 9 | @Test 10 | void test() { 11 | assertEquals(504, Problem21.solution1(1000)); 12 | assertEquals(31626, Problem21.solution1(10000)); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/basics/thread/YieldThread.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.basics.thread; 2 | 3 | public class YieldThread implements Runnable { 4 | 5 | @Override 6 | public void run() { 7 | // System.out.println(Thread.currentThread().getName() + " is running"); 8 | Thread.yield(); 9 | System.out.println(Thread.currentThread().getName() + " is stopped"); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/maths/SumToN.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.maths; 2 | 3 | public class SumToN { 4 | 5 | /** 6 | * Sum from 1 to n. 7 | * 8 | * @param n the limitation. 9 | * @return sum of 1 to n. 10 | */ 11 | public static int sum(int n) { 12 | int sum = 0; 13 | for (int i = 0; i <= n; ++i) { 14 | sum += i; 15 | } 16 | return sum; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/projecteuler/Problem20.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.projecteuler; 2 | 3 | import com.examplehub.maths.Factorial; 4 | import com.examplehub.maths.SumOfDigits; 5 | 6 | /** https://projecteuler.net/problem=20 */ 7 | public class Problem20 { 8 | public static int solution1(int number) { 9 | return SumOfDigits.sumOfDigits(Factorial.factorial(number)); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /src/test/java/com/examplehub/projecteuler/Problem02Test.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.projecteuler; 2 | 3 | import static org.junit.jupiter.api.Assertions.*; 4 | 5 | import org.junit.jupiter.api.Test; 6 | 7 | class Problem02Test { 8 | @Test 9 | void testSolution1() { 10 | assertEquals(10, Problem02.solution1(10)); 11 | assertEquals(4613732, Problem02.solution1(4000000)); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/projecteuler/Problem01.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.projecteuler; 2 | 3 | /** https://projecteuler.net/problem=1 */ 4 | public class Problem01 { 5 | 6 | public static int solution1(int n) { 7 | int sum = 0; 8 | for (int i = 1; i < n; ++i) { 9 | if (i % 3 == 0 || i % 5 == 0) { 10 | sum += i; 11 | } 12 | } 13 | return sum; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /src/test/java/com/examplehub/basics/io/RecursionPrintDirectoryTest.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.basics.io; 2 | 3 | import java.io.File; 4 | import org.junit.jupiter.api.Disabled; 5 | import org.junit.jupiter.api.Test; 6 | 7 | class RecursionPrintDirectoryTest { 8 | 9 | @Test 10 | @Disabled 11 | void test1() { 12 | RecursionPrintDirectory.printFiles(new File("src/main/java")); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /src/test/java/com/examplehub/projecteuler/Problem06Test.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.projecteuler; 2 | 3 | import static org.junit.jupiter.api.Assertions.*; 4 | 5 | import org.junit.jupiter.api.Test; 6 | 7 | class Problem06Test { 8 | 9 | @Test 10 | void testSolution1() { 11 | assertEquals(2640, Problem06.solution1(10)); 12 | assertEquals(25164150, Problem06.solution1(100)); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /src/test/java/com/examplehub/projecteuler/Problem09Test.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.projecteuler; 2 | 3 | import static org.junit.jupiter.api.Assertions.*; 4 | 5 | import org.junit.jupiter.api.Test; 6 | 7 | class Problem09Test { 8 | 9 | @Test 10 | void testSolution1() { 11 | assertEquals(60, Problem09.solution1(12)); 12 | assertEquals(31875000, Problem09.solution1(1000)); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .gradle 2 | /build/ 3 | .idea/ 4 | target 5 | *.iml 6 | 7 | # Ignore Gradle GUI config 8 | gradle-app.setting 9 | 10 | # Avoid ignoring Gradle wrapper jar file (.jar files are usually ignored) 11 | !gradle-wrapper.jar 12 | 13 | # Cache of project 14 | .gradletasknamecache 15 | 16 | # # Work around https://youtrack.jetbrains.com/issue/IDEA-116898 17 | # gradle/wrapper/gradle-wrapper.properties 18 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/basics/thread/StateOfThread.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.basics.thread; 2 | 3 | public class StateOfThread implements Runnable { 4 | 5 | @Override 6 | public void run() { 7 | for (int i = 0; i < 5; i++) { 8 | try { 9 | Thread.sleep(1000); 10 | } catch (InterruptedException e) { 11 | e.printStackTrace(); 12 | } 13 | } 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /src/test/java/com/examplehub/maths/ModeTest.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.maths; 2 | 3 | import static org.junit.jupiter.api.Assertions.*; 4 | 5 | import org.junit.jupiter.api.Test; 6 | 7 | class ModeTest { 8 | @Test 9 | void testMode() { 10 | assertEquals(2, Mode.mode(new int[] {1, 2, 2, 3, 4, 7, 9, 2})); 11 | assertEquals(3, Mode.mode(new int[] {1, 2, 3, 4, 5, 6, 3, 3, 2, 3, 6})); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /src/test/java/com/examplehub/projecteuler/Problem10Test.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.projecteuler; 2 | 3 | import static org.junit.jupiter.api.Assertions.*; 4 | 5 | import org.junit.jupiter.api.Test; 6 | 7 | class Problem10Test { 8 | 9 | @Test 10 | void testSolution1() { 11 | assertEquals(17, Problem10.solution1(10)); 12 | assertEquals(142913828922L, Problem10.solution1(2000000)); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/leetcode/middle/ThreeSum.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.leetcode.middle; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | /** https://leetcode.com/problems/3sum/ */ 7 | public class ThreeSum { 8 | public static List> solution1(int[] nums) { 9 | List> result = new ArrayList<>(); 10 | // TODO 11 | return result; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/strings/RemoveWhiteSpace.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.strings; 2 | 3 | public class RemoveWhiteSpace { 4 | 5 | /** 6 | * Remove all whitespace of a string. 7 | * 8 | * @param s the string to be removed whitespace. 9 | * @return string no whitespace. 10 | */ 11 | public static String removeWhitespace(String s) { 12 | return s.replaceAll("\\s+", ""); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /src/test/java/com/examplehub/basics/oop/InstanceofExampleTest.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.basics.oop; 2 | 3 | import static org.junit.jupiter.api.Assertions.*; 4 | 5 | import org.junit.jupiter.api.Test; 6 | 7 | class InstanceofExampleTest { 8 | @Test 9 | void test() { 10 | String str = "hello world"; 11 | assertTrue(str instanceof String); 12 | assertTrue(str instanceof Object); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /src/test/java/com/examplehub/designpatterns/proxy/ProxyTest.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.designpatterns.proxy; 2 | 3 | import com.examplehub.designpatterns.decorator.Readable; 4 | import org.junit.jupiter.api.Disabled; 5 | import org.junit.jupiter.api.Test; 6 | 7 | class ProxyTest { 8 | @Test 9 | @Disabled 10 | void test() { 11 | Readable readable = new Proxy(); 12 | readable.read(); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/leetcode/easy/DeleteNodeInALinkedList.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.leetcode.easy; 2 | 3 | import com.examplehub.leetcode.ListNode; 4 | 5 | /** https://leetcode.com/problems/delete-node-in-a-linked-list/solution/ */ 6 | public class DeleteNodeInALinkedList { 7 | public static void solution1(ListNode node) { 8 | node.val = node.next.val; 9 | node.next = node.next.next; 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/maths/FactorialRecursion.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.maths; 2 | 3 | /** https://en.wikipedia.org/wiki/Factorial */ 4 | public class FactorialRecursion { 5 | public static long factorial(int number) { 6 | if (number < 0) { 7 | throw new ArithmeticException(number + " is negative"); 8 | } 9 | return number == 0 || number == 1 ? 1 : number * factorial(number - 1); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/maths/LucasNumbers.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.maths; 2 | 3 | /** https://en.wikipedia.org/wiki/Lucas_number */ 4 | public class LucasNumbers { 5 | 6 | /** 7 | * Get nth lucas number. 8 | * 9 | * @param n the nth. 10 | * @return nth lucas number. 11 | */ 12 | public static int lucas(int n) { 13 | return n == 0 ? 2 : n == 1 ? 1 : lucas(n - 1) + lucas(n - 2); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /src/test/java/com/examplehub/basics/oop/StaticInnerClassTest.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.basics.oop; 2 | 3 | import static org.junit.jupiter.api.Assertions.*; 4 | 5 | import org.junit.jupiter.api.Test; 6 | 7 | class StaticInnerClassTest { 8 | @Test 9 | void test() { 10 | StaticInnerClass.InnerClass innerClass = new StaticInnerClass.InnerClass(); 11 | assertEquals("Hi, JACK", innerClass.sayHi()); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /src/test/java/com/examplehub/leetcode/middle/ThreeSumTest.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.leetcode.middle; 2 | 3 | import static org.junit.jupiter.api.Assertions.*; 4 | 5 | import org.junit.jupiter.api.Test; 6 | 7 | class ThreeSumTest { 8 | @Test 9 | void testSolution1() { 10 | int[] nums = {-1, 0, 1, 2, -1, -4}; 11 | // FIXME assertEquals("[[-1,-1,2],[-1,0,1]]", ThreeSum.solution1(nums).toString()); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/designpatterns/decorator/Decorator.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.designpatterns.decorator; 2 | 3 | public class Decorator implements Readable { 4 | Readable readable; 5 | 6 | public Decorator(Readable readable) { 7 | this.readable = readable; 8 | } 9 | 10 | @Override 11 | public void read() { 12 | readable.read(); 13 | System.out.println("I not have enough power"); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/maths/AbsoluteValue.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.maths; 2 | 3 | public class AbsoluteValue { 4 | 5 | /** 6 | * Returns the absolute value of a {@code int} value. 7 | * 8 | * @param a the argument whose absolute value is to be determined. 9 | * @return the absolute value of the argument. 10 | */ 11 | public static int absoluteValue(int a) { 12 | return a < 0 ? -a : a; 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/designpatterns/template/Calculator.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.designpatterns.template; 2 | 3 | public abstract class Calculator { 4 | public int calculate(String expression, String operator) { 5 | String[] operators = expression.split(operator); 6 | return calculate(Integer.parseInt(operators[0]), Integer.parseInt(operators[1])); 7 | } 8 | 9 | public abstract int calculate(int a, int b); 10 | } 11 | -------------------------------------------------------------------------------- /src/test/java/com/examplehub/basics/io/CreateFolderTest.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.basics.io; 2 | 3 | import static org.junit.jupiter.api.Assertions.*; 4 | 5 | import org.junit.jupiter.api.Disabled; 6 | import org.junit.jupiter.api.Test; 7 | 8 | class CreateFolderTest { 9 | 10 | @Disabled 11 | @Test 12 | void test() { 13 | String path = "example_folder"; 14 | assertTrue(CreateFolder.createFolder(path)); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/basics/thread/SellTicketThread.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.basics.thread; 2 | 3 | public class SellTicketThread implements Runnable { 4 | 5 | private int totalTickets = 10; 6 | 7 | @Override 8 | public void run() { 9 | while (totalTickets > 0) { 10 | System.out.println(Thread.currentThread().getName() + " buy " + totalTickets + " nth ticket"); 11 | totalTickets--; 12 | } 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/datastructures/linkedlist/Node.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.datastructures.linkedlist; 2 | 3 | public class Node { 4 | public Node next; 5 | public E data; 6 | 7 | public Node() { 8 | this(null, null); 9 | } 10 | 11 | public Node(E data) { 12 | this(null, data); 13 | } 14 | 15 | public Node(Node next, E data) { 16 | this.next = next; 17 | this.data = data; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/designpatterns/singleton/SingletonExample4.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.designpatterns.singleton; 2 | 3 | public class SingletonExample4 { 4 | private static SingletonExample4 INSTANCE; 5 | 6 | private SingletonExample4() {} 7 | 8 | public static SingletonExample4 getInstance() { 9 | if (INSTANCE == null) { 10 | INSTANCE = new SingletonExample4(); 11 | } 12 | return INSTANCE; 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/maths/LeapYear.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.maths; 2 | 3 | public class LeapYear { 4 | 5 | /** 6 | * Test if a year is leap year. 7 | * 8 | * @param year the year to be checked. 9 | * @return {@code true} if the year is leap year, otherwise {@code false}. 10 | */ 11 | public static boolean isLeapYear(int year) { 12 | return year % 4 == 0 && year % 100 != 0 || year % 400 == 0; 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/basics/oop/StaticFieldFunctionExample.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.basics.oop; 2 | 3 | public class StaticFieldFunctionExample { 4 | private static int count; 5 | private int num; 6 | 7 | public StaticFieldFunctionExample() { 8 | num++; 9 | count++; 10 | } 11 | 12 | public static int getCount() { 13 | return count; 14 | } 15 | 16 | public int getNum() { 17 | return num; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/maths/Factorial.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.maths; 2 | 3 | /** https://en.wikipedia.org/wiki/Factorial */ 4 | public class Factorial { 5 | public static long factorial(int number) { 6 | if (number < 0) { 7 | throw new ArithmeticException(number + " is negative"); 8 | } 9 | long fact = 1; 10 | for (int i = 1; i <= number; ++i) { 11 | fact *= i; 12 | } 13 | return fact; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /src/test/java/com/examplehub/projecteuler/Problem25Test.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.projecteuler; 2 | 3 | import static org.junit.jupiter.api.Assertions.*; 4 | 5 | import org.junit.jupiter.api.Test; 6 | 7 | class Problem25Test { 8 | @Test 9 | void testSolution1() { 10 | assertEquals(12, Problem25.solution1(3)); 11 | } 12 | 13 | @Test 14 | void testSolution2() { 15 | assertEquals(4782, Problem25.solution2(1000)); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/basics/exception/CustomExceptionExample.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.basics.exception; 2 | 3 | public class CustomExceptionExample extends Exception { 4 | 5 | // @java.io.Serial 6 | private static final long serialVersionUID = 234122876006267687L; 7 | 8 | public CustomExceptionExample() { 9 | super(); 10 | } 11 | 12 | public CustomExceptionExample(String message) { 13 | super(message); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/maths/ProductOfArray.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.maths; 2 | 3 | public class ProductOfArray { 4 | 5 | /** 6 | * Return the product of numbers. 7 | * 8 | * @param nums the numbers. 9 | * @return the product of numbers. 10 | */ 11 | public static int product(int... nums) { 12 | int product = 1; 13 | for (int num : nums) { 14 | product *= num; 15 | } 16 | return product; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/projecteuler/Problem10.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.projecteuler; 2 | 3 | import com.examplehub.maths.PrimeCheck; 4 | 5 | /** link: https://projecteuler.net/problem=10 */ 6 | public class Problem10 { 7 | public static long solution1(int n) { 8 | long sum = 0; 9 | for (int i = 2; i <= n; i++) { 10 | if (PrimeCheck.isPrime(i)) { 11 | sum += i; 12 | } 13 | } 14 | return sum; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /src/test/java/com/examplehub/projecteuler/Problem05Test.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.projecteuler; 2 | 3 | import static org.junit.jupiter.api.Assertions.*; 4 | 5 | import org.junit.jupiter.api.Test; 6 | 7 | class Problem05Test { 8 | @Test 9 | void testSolution1() { 10 | assertEquals(2520, Problem05.solution1(10)); 11 | assertEquals(360360, Problem05.solution1(15)); 12 | assertEquals(232792560, Problem05.solution1(20)); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/leetcode/easy/ShuffleString.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.leetcode.easy; 2 | 3 | /** https://leetcode.com/problems/shuffle-string/ */ 4 | public class ShuffleString { 5 | 6 | public static String solution1(String s, int[] indices) { 7 | char[] chars = new char[s.length()]; 8 | for (int i = 0; i < indices.length; ++i) { 9 | chars[indices[i]] = s.charAt(i); 10 | } 11 | return new String(chars); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /src/test/java/com/examplehub/basics/io/PathsExampleTest.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.basics.io; 2 | 3 | import static org.junit.jupiter.api.Assertions.assertEquals; 4 | 5 | import java.nio.file.Path; 6 | import java.nio.file.Paths; 7 | import org.junit.jupiter.api.Test; 8 | 9 | class PathsExampleTest { 10 | @Test 11 | void get() { 12 | Path path = Paths.get("pom.xml"); 13 | assertEquals("pom.xml", path.getFileName().toString()); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /src/test/java/com/examplehub/maths/AbsoluteMinTest.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.maths; 2 | 3 | import static org.junit.jupiter.api.Assertions.assertEquals; 4 | 5 | import org.junit.jupiter.api.Test; 6 | 7 | class AbsoluteMinTest { 8 | 9 | @Test 10 | void testAbsoluteMin() { 11 | assertEquals(1, AbsoluteMin.absoluteMin(new int[] {1, -2, 5, -8, 7})); 12 | 13 | assertEquals(1, AbsoluteMin.absoluteMin(new int[] {1, -2, 3, -4, 5})); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /src/test/java/com/examplehub/strings/ReverseStringTest.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.strings; 2 | 3 | import static org.junit.jupiter.api.Assertions.*; 4 | 5 | import org.junit.jupiter.api.Test; 6 | 7 | class ReverseStringTest { 8 | 9 | @Test 10 | void reverse() { 11 | assertEquals("", ReverseString.reverse("")); 12 | assertEquals("123", ReverseString.reverse("321")); 13 | assertEquals("abc", ReverseString.reverse("cba")); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/maths/MiddleIndexCalculate.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.maths; 2 | 3 | public class MiddleIndexCalculate { 4 | 5 | /** 6 | * Return middle index from left index to right index. 7 | * 8 | * @param left the left index of array. 9 | * @param right the right index of array. 10 | * @return middle index. 11 | */ 12 | public static int middle(int left, int right) { 13 | return (left + right) >>> 1; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/projecteuler/Problem03.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.projecteuler; 2 | 3 | import com.examplehub.maths.PrimeCheck; 4 | 5 | /** link: https://projecteuler.net/problem=3 */ 6 | public class Problem03 { 7 | public static long solution1(long n) { 8 | for (long i = n; i >= 2; i--) { 9 | if (n % i == 0 && PrimeCheck.isPrime(i)) { 10 | return i; 11 | } 12 | } 13 | return -1; /* not found */ 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /src/test/java/com/examplehub/maths/CubeNumberTest.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.maths; 2 | 3 | import static org.junit.jupiter.api.Assertions.*; 4 | 5 | import org.junit.jupiter.api.Test; 6 | 7 | class CubeNumberTest { 8 | 9 | @Test 10 | void testCubeNumber() { 11 | int[] cubeNumbers = {-8, -1, 0, 1, 8, 27, 64, 8000, 216_000}; 12 | for (int numbers : cubeNumbers) { 13 | assertTrue(CubeNumber.isCubeNumber(numbers)); 14 | } 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /src/test/java/com/examplehub/maths/ProductOfArrayTest.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.maths; 2 | 3 | import static org.junit.jupiter.api.Assertions.*; 4 | 5 | import org.junit.jupiter.api.Test; 6 | 7 | class ProductOfArrayTest { 8 | @Test 9 | void testProduct() { 10 | assertEquals(0, ProductOfArray.product(0, 1, 2, 3)); 11 | assertEquals(1, ProductOfArray.product(1, 1)); 12 | assertEquals(120, ProductOfArray.product(1, 2, 3, 5, 4)); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/datastructures/binarytree/Node.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.datastructures.binarytree; 2 | 3 | public class Node { 4 | 5 | /** Node value of BinaryTree. */ 6 | public E value; 7 | 8 | /** The pointer of left child. */ 9 | public Node left; 10 | 11 | /** The Pointer of right child. */ 12 | public Node right; 13 | 14 | public Node(E value) { 15 | this.value = value; 16 | left = right = null; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/designpatterns/singleton/SingletonExample6.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.designpatterns.singleton; 2 | 3 | public class SingletonExample6 { 4 | 5 | private SingletonExample6() {} 6 | 7 | private static final class InstanceHolder { 8 | private static final SingletonExample6 INSTANCE = new SingletonExample6(); 9 | } 10 | 11 | public static SingletonExample6 getInstance() { 12 | return InstanceHolder.INSTANCE; 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /src/test/java/com/examplehub/basics/oop/AbstractClassExampleTest.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.basics.oop; 2 | 3 | import static org.junit.jupiter.api.Assertions.*; 4 | 5 | import org.junit.jupiter.api.Test; 6 | 7 | class AbstractClassExampleTest { 8 | @Test 9 | void test() { 10 | People people = new Student(); 11 | assertEquals("Student.say", people.say()); 12 | people = new Teacher(); 13 | assertEquals("Teacher.eat", people.say()); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /src/test/java/com/examplehub/leetcode/easy/PathSumIITest.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.leetcode.easy; 2 | 3 | import com.examplehub.leetcode.TreeNode; 4 | import com.examplehub.leetcode.utils.BinaryTreeUtils; 5 | import org.junit.jupiter.api.Test; 6 | 7 | class PathSumIITest { 8 | @Test 9 | void testSolution1() { 10 | TreeNode root = 11 | BinaryTreeUtils.createTree(new int[] {5, 4, 8, 11, 0, 13, 4, 7, 2, 0, 0, 5, 1}, 0); 12 | // TODO 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/basics/function/ModifyFunctionArgs.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.basics.function; 2 | 3 | public class ModifyFunctionArgs { 4 | static class MyObj { 5 | public int age = -1; 6 | } 7 | 8 | public static void modify(int num, String str, Integer integer, int[] array, MyObj obj) { 9 | num = num + 1; 10 | str = "<" + str + ">"; 11 | integer = integer + 1; 12 | array[0] = -array[0]; 13 | obj.age = -obj.age; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/designpatterns/proxy/Proxy.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.designpatterns.proxy; 2 | 3 | import com.examplehub.designpatterns.decorator.Machine; 4 | import com.examplehub.designpatterns.decorator.Readable; 5 | 6 | public class Proxy implements Readable { 7 | 8 | Readable readable; 9 | 10 | public Proxy() { 11 | readable = new Machine(); 12 | } 13 | 14 | @Override 15 | public void read() { 16 | readable.read(); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/leetcode/TreeNode.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.leetcode; 2 | 3 | public class TreeNode { 4 | public int val; 5 | public TreeNode left; 6 | public TreeNode right; 7 | 8 | public TreeNode() {} 9 | 10 | public TreeNode(int val) { 11 | this(val, null, null); 12 | } 13 | 14 | public TreeNode(int val, TreeNode left, TreeNode right) { 15 | this.val = val; 16 | this.left = left; 17 | this.right = right; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/leetcode/easy/ContainsDuplicateII.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.leetcode.easy; 2 | 3 | public class ContainsDuplicateII { 4 | public static boolean solution1(int[] nums, int k) { 5 | for (int i = 0; i < nums.length; ++i) { 6 | for (int j = i + 1; j < nums.length; ++j) { 7 | if (nums[i] == nums[j] && Math.abs(i - j) <= k) { 8 | return true; 9 | } 10 | } 11 | } 12 | return false; 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/projecteuler/Problem05.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.projecteuler; 2 | 3 | /** link: https://projecteuler.net/problem=5 */ 4 | public class Problem05 { 5 | 6 | public static long solution1(int n) { 7 | for (int i = n; ; i++) { 8 | for (int j = 1; j <= n; j++) { 9 | if (i % j != 0) { 10 | break; 11 | } 12 | if (j == n) { 13 | return i; 14 | } 15 | } 16 | } 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /src/test/java/com/examplehub/maths/LucasNumbersTest.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.maths; 2 | 3 | import static org.junit.jupiter.api.Assertions.*; 4 | 5 | import org.junit.jupiter.api.Test; 6 | 7 | class LucasNumbersTest { 8 | @Test 9 | void test() { 10 | int[] lucasNumbers = {2, 1, 3, 4, 7, 11, 18, 29, 47, 76, 123}; 11 | for (int i = 0; i < lucasNumbers.length; i++) { 12 | assertEquals(lucasNumbers[i], LucasNumbers.lucas(i)); 13 | } 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/basics/thread/SynchronizedThread.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.basics.thread; 2 | 3 | public class SynchronizedThread implements Runnable { 4 | private int count = 10; 5 | 6 | @Override 7 | public void run() { 8 | while (count > 0) { 9 | reduce(); 10 | } 11 | } 12 | 13 | private synchronized void reduce() { 14 | System.out.println(Thread.currentThread().getName() + " count = " + count); 15 | count--; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/maths/SignFunction.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.maths; 2 | 3 | public class SignFunction { 4 | 5 | /** 6 | * Sign function algorithm. 7 | * 8 | * @param number the number to be checked. 9 | * @return {@code 1} if number is positive, {@code 0} returned if number is zero, otherwise {@code 10 | * -1} returned. 11 | */ 12 | public static int signFunction(int number) { 13 | return Integer.compare(number, 0); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/projecteuler/Problem07.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.projecteuler; 2 | 3 | import com.examplehub.maths.PrimeCheck; 4 | 5 | /** link: https://projecteuler.net/problem=7 */ 6 | public class Problem07 { 7 | 8 | public static long solution(int n) { 9 | int count = 0; 10 | int i; 11 | for (i = 2; count != n; i++) { 12 | if (PrimeCheck.isPrime(i)) { 13 | count++; 14 | } 15 | } 16 | return i - 1; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /src/test/java/com/examplehub/basics/exception/ArithmeticExceptionExampleTest.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.basics.exception; 2 | 3 | import static org.junit.jupiter.api.Assertions.*; 4 | 5 | import org.junit.jupiter.api.Test; 6 | 7 | class ArithmeticExceptionExampleTest { 8 | @Test 9 | void testDivideByZero() { 10 | try { 11 | int division = 10 / 0; 12 | fail(); 13 | } catch (ArithmeticException e) { 14 | assertTrue(true); 15 | } 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /src/test/java/com/examplehub/leetcode/middle/LongestPalindromicSubstringTest.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.leetcode.middle; 2 | 3 | import static org.junit.jupiter.api.Assertions.*; 4 | 5 | import org.junit.jupiter.api.Test; 6 | 7 | class LongestPalindromicSubstringTest { 8 | @Test 9 | void testSolution1() { 10 | assertEquals("bab", LongestPalindromicSubstring.solution1("babad")); 11 | assertEquals("bb", LongestPalindromicSubstring.solution1("cbbd")); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /src/test/java/com/examplehub/maths/SumToNTest.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.maths; 2 | 3 | import static org.junit.jupiter.api.Assertions.*; 4 | 5 | import org.junit.jupiter.api.Test; 6 | 7 | class SumToNTest { 8 | 9 | @Test 10 | void testSum() { 11 | assertEquals(0, SumToN.sum(0)); 12 | assertEquals(1, SumToN.sum(1)); 13 | assertEquals(3, SumToN.sum(2)); 14 | assertEquals(55, SumToN.sum(10)); 15 | assertEquals(5050, SumToN.sum(100)); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /src/test/java/com/examplehub/leetcode/easy/Maximum69NumberTest.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.leetcode.easy; 2 | 3 | import static org.junit.jupiter.api.Assertions.*; 4 | 5 | import org.junit.jupiter.api.Test; 6 | 7 | class Maximum69NumberTest { 8 | @Test 9 | void testSolution1() { 10 | assertEquals(Maximum69Number.solution1(9669), 9969); 11 | assertEquals(Maximum69Number.solution1(9996), 9999); 12 | assertEquals(Maximum69Number.solution1(9999), 9999); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /src/test/java/com/examplehub/maths/FibonacciBigNumberTest.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.maths; 2 | 3 | import static org.junit.jupiter.api.Assertions.*; 4 | 5 | import org.junit.jupiter.api.Test; 6 | 7 | class FibonacciBigNumberTest { 8 | @Test 9 | void testFibonacciBigNumber() { 10 | assertEquals("1", FibonacciBigNumber.fibonacci(1)); 11 | assertEquals("1", FibonacciBigNumber.fibonacci(2)); 12 | assertEquals("144", FibonacciBigNumber.fibonacci(12)); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /src/test/java/com/examplehub/maths/NextGreaterElementTest.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.maths; 2 | 3 | import static org.junit.jupiter.api.Assertions.*; 4 | 5 | import java.util.Arrays; 6 | import org.junit.jupiter.api.Test; 7 | 8 | class NextGreaterElementTest { 9 | @Test 10 | void test() { 11 | assertEquals( 12 | Arrays.toString(new int[] {5, -1, 4, 5, -1}), 13 | Arrays.toString(NextGreaterElement.nextGreater(new int[] {2, 5, 3, 4, 5}))); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /src/test/java/com/examplehub/strings/JustAppearOneTimeTest.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.strings; 2 | 3 | import static org.junit.jupiter.api.Assertions.*; 4 | 5 | import org.junit.jupiter.api.Test; 6 | 7 | class JustAppearOneTimeTest { 8 | @Test 9 | void testSolution1() { 10 | assertTrue(JustAppearOneTime.solution1("abc", "ab")); 11 | assertFalse(JustAppearOneTime.solution1("abcbc", "bc")); 12 | assertFalse(JustAppearOneTime.solution1("abc", "ac")); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/basics/oop/InnerClassExample.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.basics.oop; 2 | 3 | public class InnerClassExample {} 4 | 5 | class OuterClass { 6 | private final String name; 7 | 8 | public OuterClass(String name) { 9 | this.name = name; 10 | } 11 | 12 | class InnerClass { 13 | public String sayHi() { 14 | return "Hi, " + OuterClass.this.name; 15 | } 16 | } 17 | 18 | public String getName() { 19 | return name; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/datastructures/array/ReverseArray.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.datastructures.array; 2 | 3 | public class ReverseArray { 4 | 5 | /** 6 | * Reverse a array. 7 | * 8 | * @param array the array to be reversed. 9 | */ 10 | public static void reverse(int[] array) { 11 | for (int i = 0, j = array.length - 1; i < j; i++, j--) { 12 | int temp = array[i]; 13 | array[i] = array[j]; 14 | array[j] = temp; 15 | } 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/maths/CountDigits.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.maths; 2 | 3 | public class CountDigits { 4 | 5 | /** 6 | * Count the number of digits of a number. 7 | * 8 | * @param number the number to counted. 9 | * @return the number of digits. 10 | */ 11 | public static int countDigits(int number) { 12 | int count = 0; 13 | do { 14 | number /= 10; 15 | count++; 16 | } while (number != 0); 17 | return count; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/maths/CountDigitsRecursion.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.maths; 2 | 3 | public class CountDigitsRecursion { 4 | 5 | /** 6 | * Count the number of digits of a number using recursion. 7 | * 8 | * @param number the number to be counted. 9 | * @return the number of digits. 10 | */ 11 | public static int countDigits(int number) { 12 | number = Math.abs(number); 13 | return number < 10 ? 1 : 1 + countDigits(number / 10); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /src/test/java/com/examplehub/basics/oop/DeclareMethodTest.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.basics.oop; 2 | 3 | import static org.junit.jupiter.api.Assertions.*; 4 | 5 | import org.junit.jupiter.api.Test; 6 | 7 | class DeclareMethodTest { 8 | @Test 9 | void test() { 10 | DeclareMethod declareMethod = new DeclareMethod(); 11 | declareMethod.setName("Jack"); 12 | declareMethod.setAge(25); 13 | 14 | assertEquals("I'm Jack. I'm 25", declareMethod.intro()); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /src/test/java/com/examplehub/basics/time/InstantExampleTest.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.basics.time; 2 | 3 | import static org.junit.jupiter.api.Assertions.*; 4 | 5 | import java.time.Instant; 6 | import org.junit.jupiter.api.Test; 7 | 8 | class InstantExampleTest { 9 | @Test 10 | void test() { 11 | Instant now = Instant.now(); 12 | System.out.println(now); 13 | System.out.println(now.toEpochMilli()); 14 | System.out.println(now.getEpochSecond()); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /src/test/java/com/examplehub/conversions/BinaryToOctalTest.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.conversions; 2 | 3 | import static org.junit.jupiter.api.Assertions.*; 4 | 5 | import org.junit.jupiter.api.Test; 6 | 7 | class BinaryToOctalTest { 8 | 9 | @Test 10 | void testBinaryToOctal() { 11 | assertEquals("17", BinaryToOctal.toOctal("1111")); 12 | assertEquals("7", BinaryToOctal.toOctal("111")); 13 | assertEquals("1274", BinaryToOctal.toOctal("1010111100")); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /src/test/java/com/examplehub/leetcode/middle/MultiplyStringsTest.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.leetcode.middle; 2 | 3 | import static org.junit.jupiter.api.Assertions.*; 4 | 5 | import org.junit.jupiter.api.Test; 6 | 7 | class MultiplyStringsTest { 8 | @Test 9 | void testSolution1() { 10 | assertEquals("6", MultiplyStrings.solution1("2", "3")); 11 | } 12 | 13 | @Test 14 | void testSolution2() { 15 | assertEquals("6", MultiplyStrings.solution2("2", "3")); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /src/test/java/com/examplehub/maths/CharToIntTest.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.maths; 2 | 3 | import static org.junit.jupiter.api.Assertions.*; 4 | 5 | import org.junit.jupiter.api.Test; 6 | 7 | class CharToIntTest { 8 | @Test 9 | void testCharToInt() { 10 | for (char i = 0, ch = '0'; ch <= '9'; i++, ch++) { 11 | assertEquals(i, CharToInt.toInt(ch)); 12 | assertEquals(i, CharToInt.toInt2(ch)); 13 | assertEquals(i, CharToInt.toInt3(ch)); 14 | } 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/designpatterns/singleton/SingletonExample2.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.designpatterns.singleton; 2 | 3 | public class SingletonExample2 { 4 | private static final SingletonExample2 INSTANCE; 5 | 6 | static { 7 | /* 8 | * do more work here 9 | */ 10 | INSTANCE = new SingletonExample2(); 11 | } 12 | 13 | private SingletonExample2() {} 14 | 15 | public static SingletonExample2 getInstance() { 16 | return INSTANCE; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/projecteuler/Problem09.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.projecteuler; 2 | 3 | /** https://projecteuler.net/problem=9 */ 4 | public class Problem09 { 5 | 6 | public static int solution1(int n) { 7 | for (int a = 1; a <= n; a++) { 8 | for (int b = a + 1; b <= n; b++) { 9 | int c = n - a - b; 10 | if (b < c && (a * a + b * b == c * c)) { 11 | return a * b * c; 12 | } 13 | } 14 | } 15 | return -1; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /src/test/java/com/examplehub/dynamicprogramming/LucasDPTest.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.dynamicprogramming; 2 | 3 | import static org.junit.jupiter.api.Assertions.assertEquals; 4 | 5 | import org.junit.jupiter.api.Test; 6 | 7 | class LucasDPTest { 8 | @Test 9 | void test() { 10 | int[] lucasNumbers = {2, 1, 3, 4, 7, 11, 18, 29, 47, 76, 123}; 11 | for (int i = 0; i < lucasNumbers.length; i++) { 12 | assertEquals(lucasNumbers[i], LucasDP.lucas(i)); 13 | } 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /src/test/java/com/examplehub/leetcode/easy/SelfDividingNumbersTest.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.leetcode.easy; 2 | 3 | import static org.junit.jupiter.api.Assertions.assertEquals; 4 | 5 | import java.util.Arrays; 6 | import org.junit.jupiter.api.Test; 7 | 8 | class SelfDividingNumbersTest { 9 | @Test 10 | void testSolution1() { 11 | assertEquals( 12 | Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 12, 15, 22), 13 | SelfDividingNumbers.solution1(1, 22)); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/basics/Variable.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.basics; 2 | 3 | public class Variable { 4 | public static void main(String[] args) { 5 | int age = 18; 6 | System.out.println(age); 7 | 8 | age = age + 2; 9 | System.out.println(age); 10 | 11 | int a = 1; 12 | int b = 2; 13 | int c = 3; 14 | int sum = a + b + c; 15 | System.out.println(sum); 16 | 17 | String name = "ExampleHub"; 18 | System.out.println(name); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/basics/oop/AbstractClassExample.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.basics.oop; 2 | 3 | public class AbstractClassExample {} 4 | 5 | abstract class People { 6 | public abstract String say(); 7 | } 8 | 9 | class Student extends People { 10 | 11 | @Override 12 | public String say() { 13 | return "Student.say"; 14 | } 15 | } 16 | 17 | class Teacher extends People { 18 | 19 | @Override 20 | public String say() { 21 | return "Teacher.eat"; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/basics/oop/FunctionOverloadExample.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.basics.oop; 2 | 3 | public class FunctionOverloadExample { 4 | public int sum(int a, int b) { 5 | return a + b; 6 | } 7 | 8 | public int sum(int a, int b, int c) { 9 | return a + b + c; 10 | } 11 | 12 | public int sum(int a, int b, int c, int d) { 13 | return sum(sum(a, b), sum(c, d)); 14 | } 15 | 16 | public double sum(double a, double b) { 17 | return a + b; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /src/test/java/com/examplehub/datastructures/array/ReverseArrayTest.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.datastructures.array; 2 | 3 | import static org.junit.jupiter.api.Assertions.*; 4 | 5 | import java.util.stream.IntStream; 6 | import org.junit.jupiter.api.Test; 7 | 8 | class ReverseArrayTest { 9 | @Test 10 | void testReverse() { 11 | int[] array = IntStream.range(1, 6).toArray(); 12 | ReverseArray.reverse(array); 13 | assertArrayEquals(new int[] {5, 4, 3, 2, 1}, array); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /src/test/java/com/examplehub/leetcode/easy/ThirdMaximumNumberTest.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.leetcode.easy; 2 | 3 | import static org.junit.jupiter.api.Assertions.*; 4 | 5 | import org.junit.jupiter.api.Test; 6 | 7 | class ThirdMaximumNumberTest { 8 | @Test 9 | void testSolution1() { 10 | assertEquals(1, ThirdMaximumNumber.solution1(3, 2, 1)); 11 | assertEquals(2, ThirdMaximumNumber.solution1(1, 2)); 12 | assertEquals(1, ThirdMaximumNumber.solution1(2, 2, 3, 1)); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/maths/PowerRecursion.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.maths; 2 | 3 | public class PowerRecursion { 4 | 5 | /** 6 | * Returns the value of the first argument raised to the power of the second argument using 7 | * recursion. 8 | * 9 | * @param a the base. 10 | * @param b the exponent. 11 | * @return the value {@code a}{@code b}. 12 | */ 13 | public static long pow(int a, int b) { 14 | return b == 0 ? 1 : a * pow(a, b - 1); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /src/test/java/com/examplehub/basics/condition/ContinueExampleTest.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.basics.condition; 2 | 3 | import static org.junit.jupiter.api.Assertions.*; 4 | 5 | import org.junit.jupiter.api.Test; 6 | 7 | class ContinueExampleTest { 8 | @Test 9 | void test1() { 10 | int sum = 0; 11 | for (int i = 1; i <= 10; ++i) { 12 | if (i % 2 == 1) { 13 | continue; 14 | } 15 | sum += i; 16 | } 17 | assertEquals(2 + 4 + 6 + 8 + 10, sum); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /src/test/java/com/examplehub/basics/exception/NumberFormatExceptionExampleTest.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.basics.exception; 2 | 3 | import static org.junit.jupiter.api.Assertions.*; 4 | 5 | import org.junit.jupiter.api.Test; 6 | 7 | class NumberFormatExceptionExampleTest { 8 | @Test 9 | void testFormatNumber() { 10 | try { 11 | int number = Integer.parseInt("123a"); 12 | fail(); 13 | } catch (NumberFormatException e) { 14 | assertTrue(true); 15 | } 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /src/test/java/com/examplehub/basics/oop/UpcastExampleTest.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.basics.oop; 2 | 3 | import static org.junit.jupiter.api.Assertions.*; 4 | 5 | import org.junit.jupiter.api.Test; 6 | 7 | class UpcastExampleTest { 8 | @Test 9 | void test() { 10 | UpClass upClass = new UpcastExample("Jack", 26, 180); 11 | assertEquals("Jack", upClass.getName()); 12 | assertEquals(26, upClass.getAge()); 13 | assertEquals(180, ((UpcastExample) upClass).getHeight()); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /src/test/java/com/examplehub/matrix/PrecedencesTest.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.matrix; 2 | 3 | import static org.junit.jupiter.api.Assertions.*; 4 | 5 | import org.junit.jupiter.api.Test; 6 | 7 | class PrecedencesTest { 8 | @Test 9 | void testPrecedences() { 10 | assertTrue(Precedences.compare("*", "+") > 0); 11 | assertTrue(Precedences.compare("*", "-") > 0); 12 | assertEquals(Precedences.compare("+", "-"), 0); 13 | assertTrue(Precedences.compare("*", "/") >= 0); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/strings/PalindromeStringByReverse.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.strings; 2 | 3 | public class PalindromeStringByReverse { 4 | /** 5 | * Test if a string is palindrome string or not using reversing string. 6 | * 7 | * @param s the string to be checked. 8 | * @return {@code true} if the string is palindrome string, otherwise {@code false}. 9 | */ 10 | public static boolean isPalindrome(String s) { 11 | return s.equals(ReverseString.reverse(s)); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /src/test/java/com/examplehub/basics/oop/ExampleClassTest.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.basics.oop; 2 | 3 | import static org.junit.jupiter.api.Assertions.*; 4 | 5 | import org.junit.jupiter.api.Test; 6 | 7 | class ExampleClassTest { 8 | @Test 9 | void test() { 10 | ExampleClass exampleClass = new ExampleClass(); 11 | exampleClass.name = "duyuanchao"; 12 | exampleClass.age = 25; 13 | assertEquals("duyuanchao", exampleClass.name); 14 | assertEquals(25, exampleClass.age); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /src/test/java/com/examplehub/leetcode/easy/ShuffleTheArrayTest.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.leetcode.easy; 2 | 3 | import static org.junit.jupiter.api.Assertions.*; 4 | 5 | import java.util.Arrays; 6 | import org.junit.jupiter.api.Test; 7 | 8 | class ShuffleTheArrayTest { 9 | @Test 10 | void testSolution1() { 11 | int[] nums = {2, 5, 1, 3, 4, 7}; 12 | ShuffleTheArray.solution1(nums, 3); 13 | assertEquals(Arrays.toString(nums), Arrays.toString(new int[] {2, 3, 5, 4, 1, 7})); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /src/test/java/com/examplehub/maths/LucasNumbersIterationTest.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.maths; 2 | 3 | import static org.junit.jupiter.api.Assertions.assertEquals; 4 | 5 | import org.junit.jupiter.api.Test; 6 | 7 | class LucasNumbersIterationTest { 8 | @Test 9 | void test() { 10 | int[] lucasNumbers = {2, 1, 3, 4, 7, 11, 18, 29, 47, 76, 123}; 11 | for (int i = 0; i < lucasNumbers.length; i++) { 12 | assertEquals(lucasNumbers[i], LucasNumbersIteration.lucas(i)); 13 | } 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /src/test/java/com/examplehub/maths/SignFunctionTest.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.maths; 2 | 3 | import static org.junit.jupiter.api.Assertions.*; 4 | 5 | import org.junit.jupiter.api.Test; 6 | 7 | class SignFunctionTest { 8 | @Test 9 | void testSignFunction() { 10 | for (int i = 1; i <= 100; i++) { 11 | assertEquals(1, SignFunction.signFunction(i)); 12 | assertEquals(-1, SignFunction.signFunction(-i)); 13 | } 14 | assertEquals(0, SignFunction.signFunction(0)); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/strings/JustAppearOneTime.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.strings; 2 | 3 | /** 4 | * str = "abc", sub = "ab", return true str = "abcbca", sub = "bc", return false str = "abc", sub = 5 | * "ac", return false 6 | */ 7 | public class JustAppearOneTime { 8 | 9 | public static boolean solution1(String str, String sub) { 10 | int firstIndex = str.indexOf(sub); 11 | int lastIndex = str.lastIndexOf(sub); 12 | return firstIndex != -1 && firstIndex == lastIndex; 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /src/test/java/com/examplehub/leetcode/easy/SortArrayByParityTest.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.leetcode.easy; 2 | 3 | import static org.junit.jupiter.api.Assertions.*; 4 | 5 | import java.util.Arrays; 6 | import org.junit.jupiter.api.Test; 7 | 8 | class SortArrayByParityTest { 9 | @Test 10 | void testSolution1() { 11 | int[] nums = {3, 1, 2, 4}; 12 | assertEquals( 13 | Arrays.toString(SortArrayByParity.solution1(nums)), 14 | Arrays.toString(new int[] {2, 4, 3, 1})); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/basics/oop/VarargsExample.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.basics.oop; 2 | 3 | public class VarargsExample { 4 | private String[] languages; 5 | 6 | public void setLanguage(String... languages) { 7 | this.languages = languages; 8 | } 9 | 10 | public String[] getLanguages() { 11 | return languages; 12 | } 13 | 14 | public int sum(int... nums) { 15 | int sum = 0; 16 | for (int num : nums) { 17 | sum = sum + num; 18 | } 19 | return sum; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/maths/Median.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.maths; 2 | 3 | import java.util.Arrays; 4 | 5 | /** https://en.wikipedia.org/wiki/Median */ 6 | public class Median { 7 | public static double median(double[] numbers) { 8 | Arrays.sort(numbers); 9 | int length = numbers.length; 10 | int middle_index = length >> 1; 11 | return EvenCheck.isEvenFaster(length) 12 | ? (numbers[middle_index] + numbers[middle_index - 1]) / 2 13 | : numbers[middle_index]; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /src/test/java/com/examplehub/basics/oop/ExtendExampleTest.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.basics.oop; 2 | 3 | import static org.junit.jupiter.api.Assertions.*; 4 | 5 | import org.junit.jupiter.api.Test; 6 | 7 | class ExtendExampleTest { 8 | @Test 9 | void test() { 10 | ExtendExample extendExample = new ExtendExample("Jack", 25, 180); 11 | assertEquals("Jack", extendExample.getName()); 12 | assertEquals(25, extendExample.getAge()); 13 | assertEquals(180, extendExample.getHeight()); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /src/test/java/com/examplehub/basics/oop/InterfaceExampleTest.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.basics.oop; 2 | 3 | import static org.junit.jupiter.api.Assertions.*; 4 | 5 | import org.junit.jupiter.api.Test; 6 | 7 | class InterfaceExampleTest { 8 | @Test 9 | void test() { 10 | USB usb = new Phone(); 11 | assertEquals("phone's usb", usb.charge()); 12 | usb = new Computer(); 13 | assertEquals("computer's usb", usb.charge()); 14 | assertEquals("hello world", usb.defaultFunction()); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /src/test/java/com/examplehub/maths/FrogJumpTest.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.maths; 2 | 3 | import static org.junit.jupiter.api.Assertions.*; 4 | 5 | import org.junit.jupiter.api.Test; 6 | 7 | class FrogJumpTest { 8 | 9 | @Test 10 | void test() { 11 | assertEquals(1, FrogJump.canCross(0, 0)); 12 | assertEquals(2, FrogJump.canCross(1, 0)); 13 | assertEquals(3, FrogJump.canCross(2, 0)); 14 | assertEquals(4, FrogJump.canCross(1, 1)); 15 | assertEquals(6, FrogJump.canCross(2, 1)); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /src/test/java/com/examplehub/maths/LeapYearTest.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.maths; 2 | 3 | import static org.junit.jupiter.api.Assertions.*; 4 | 5 | import org.junit.jupiter.api.Test; 6 | 7 | class LeapYearTest { 8 | 9 | @Test 10 | void testLeapYear() { 11 | assertTrue(LeapYear.isLeapYear(1600)); 12 | assertTrue(LeapYear.isLeapYear(2000)); 13 | assertFalse(LeapYear.isLeapYear(1700)); 14 | assertFalse(LeapYear.isLeapYear(1800)); 15 | assertFalse(LeapYear.isLeapYear(1900)); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /src/test/java/com/examplehub/maths/PowerOfTwoTest.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.maths; 2 | 3 | import static org.junit.jupiter.api.Assertions.*; 4 | 5 | import org.junit.jupiter.api.Test; 6 | 7 | class PowerOfTwoTest { 8 | @Test 9 | void test() { 10 | for (int i = -5; i <= 0; ++i) { 11 | assertFalse(PowerOfTwo.isPowerOfTwo(i)); 12 | } 13 | assertFalse(PowerOfTwo.isPowerOfTwo(99)); 14 | assertTrue(PowerOfTwo.isPowerOfTwo(64)); 15 | assertTrue(PowerOfTwo.isPowerOfTwo(1024)); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/maths/AreAllTrue.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.maths; 2 | 3 | public class AreAllTrue { 4 | 5 | /** 6 | * Test if an boolean array are all true. 7 | * 8 | * @param array the array to be tested. 9 | * @return true if all elements of array are ture, otherwise false. 10 | */ 11 | public static boolean areAllTrue(boolean... array) { 12 | for (boolean bool : array) { 13 | if (!bool) { 14 | return false; 15 | } 16 | } 17 | return true; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /src/test/java/com/examplehub/basics/exception/CustomExceptionExampleTest.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.basics.exception; 2 | 3 | import static org.junit.jupiter.api.Assertions.*; 4 | 5 | import org.junit.jupiter.api.Test; 6 | 7 | class CustomExceptionExampleTest { 8 | @Test 9 | void testCustomException() { 10 | try { 11 | throw new CustomExceptionExample("custom exception"); 12 | } catch (CustomExceptionExample e) { 13 | assertEquals("custom exception", e.getMessage()); 14 | } 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /src/test/java/com/examplehub/basics/oop/InnerClassExampleTest.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.basics.oop; 2 | 3 | import static org.junit.jupiter.api.Assertions.*; 4 | 5 | import org.junit.jupiter.api.Test; 6 | 7 | class InnerClassExampleTest { 8 | @Test 9 | void test() { 10 | OuterClass outerClass = new OuterClass("jack"); 11 | assertEquals("jack", outerClass.getName()); 12 | OuterClass.InnerClass innerClass = outerClass.new InnerClass(); 13 | assertEquals("Hi, jack", innerClass.sayHi()); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /src/test/java/com/examplehub/maths/AbsoluteMaxTest.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.maths; 2 | 3 | import static org.junit.jupiter.api.Assertions.*; 4 | 5 | import org.junit.jupiter.api.Test; 6 | 7 | class AbsoluteMaxTest { 8 | 9 | @Test 10 | void testAbsoluteMax() { 11 | assertEquals(-8, AbsoluteMax.absoluteMax(new int[] {1, -2, 5, -8, 7})); 12 | 13 | assertEquals(5, AbsoluteMax.absoluteMax(new int[] {1, 2, 3, -4, 5})); 14 | 15 | assertEquals(-10, AbsoluteMax.absoluteMax(new int[] {-10})); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/basics/BreakWithLabel.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.basics; 2 | 3 | public class BreakWithLabel { 4 | public static void main(String[] args) { 5 | label: 6 | for (int i = 1; i <= 10; ++i) { 7 | for (int j = 1; j <= 10; ++j) { 8 | System.out.println("i = " + i + ", j = " + j); 9 | if (j == 2) { 10 | break label; 11 | } 12 | } 13 | } 14 | /* 15 | * output: 16 | * i = 1, j = 1 17 | * i = 1, j = 2 18 | */ 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/basics/InheritanceExample.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.basics; 2 | 3 | public class InheritanceExample { 4 | public static void main(String[] args) { 5 | Dog dog = new Dog(); 6 | dog.bark(); /* barking... */ 7 | dog.eat(); /* eating... */ 8 | } 9 | } 10 | 11 | class Animal { 12 | public void eat() { 13 | System.out.println("eating..."); 14 | } 15 | } 16 | 17 | class Dog extends Animal { 18 | public void bark() { 19 | System.out.println("barking..."); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/basics/ShutdownHook.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.basics; 2 | 3 | public class ShutdownHook { 4 | public static void main(String[] args) { 5 | Runtime.getRuntime() 6 | .addShutdownHook( 7 | new Thread( 8 | () -> { 9 | /* do something when JVM is shutdown */ 10 | System.out.print("JVM is Shutdown"); 11 | })); 12 | 13 | System.out.println("JVM will be shutdown"); 14 | System.exit(0); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/conversions/DecimalToOctal.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.conversions; 2 | 3 | public class DecimalToOctal { 4 | public static String toOctal(int number) { 5 | boolean isNegative = number < 0; 6 | number = Math.abs(number); 7 | StringBuilder result = new StringBuilder(); 8 | do { 9 | result.append(number % 8); 10 | number /= 8; 11 | } while (number != 0); 12 | return isNegative ? result.append("-").reverse().toString() : result.reverse().toString(); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/leetcode/easy/NumberOfGoodPairs.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.leetcode.easy; 2 | 3 | /** https://leetcode.com/problems/number-of-good-pairs/ */ 4 | public class NumberOfGoodPairs { 5 | public static int solution1(int[] nums) { 6 | int goodPairs = 0; 7 | for (int i = 0; i < nums.length; ++i) { 8 | for (int j = i + 1; j < nums.length; j++) { 9 | if (nums[i] == nums[j]) { 10 | goodPairs++; 11 | } 12 | } 13 | } 14 | return goodPairs; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /src/test/java/com/examplehub/leetcode/easy/SubtractTheProductAndSumOfDigitsOfAnIntegerTest.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.leetcode.easy; 2 | 3 | import static org.junit.jupiter.api.Assertions.*; 4 | 5 | import org.junit.jupiter.api.Test; 6 | 7 | class SubtractTheProductAndSumOfDigitsOfAnIntegerTest { 8 | @Test 9 | void testSolution1() { 10 | assertEquals(SubtractTheProductAndSumOfDigitsOfAnInteger.solution1(234), 15); 11 | assertEquals(SubtractTheProductAndSumOfDigitsOfAnInteger.solution1(4421), 21); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /src/test/java/com/examplehub/maths/CeilTest.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.maths; 2 | 3 | import static org.junit.jupiter.api.Assertions.*; 4 | 5 | import org.junit.jupiter.api.Test; 6 | 7 | class CeilTest { 8 | @Test 9 | void testCeil() { 10 | assertEquals(Math.ceil(3.14), Ceil.ceil(3.14)); 11 | assertEquals(Math.ceil(3), Ceil.ceil(3)); 12 | assertEquals(Math.ceil(0), Ceil.ceil(0)); 13 | assertEquals(Math.ceil(-3), Ceil.ceil(-3)); 14 | assertEquals(Math.ceil(-3.14), Ceil.ceil(-3.14)); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/conversions/DecimalToBinary.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.conversions; 2 | 3 | public class DecimalToBinary { 4 | public static String toBinary(int number) { 5 | boolean isNegative = number < 0; 6 | number = Math.abs(number); 7 | StringBuilder result = new StringBuilder(); 8 | do { 9 | result.append(number % 2); 10 | number /= 2; 11 | } while (number != 0); 12 | return isNegative ? result.append("-").reverse().toString() : result.reverse().toString(); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/maths/Power.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.maths; 2 | 3 | public class Power { 4 | 5 | /** 6 | * Returns the value of the first argument raised to the power of the second argument. 7 | * 8 | * @param a the base. 9 | * @param b the exponent. 10 | * @return the value {@code a}{@code b}. 11 | */ 12 | public static long pow(int a, int b) { 13 | int power = 1; 14 | for (int i = 1; i <= b; i++) { 15 | power *= a; 16 | } 17 | return power; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/projecteuler/Problem06.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.projecteuler; 2 | 3 | import com.examplehub.maths.SumToN; 4 | 5 | /** https://projecteuler.net/problem=6 */ 6 | public class Problem06 { 7 | public static long solution1(int n) { 8 | long sumOfSquares = 0; 9 | for (int i = 1; i <= n; ++i) { 10 | sumOfSquares = (long) (sumOfSquares + Math.pow(i, 2)); 11 | } 12 | 13 | long squareOfSum = (long) Math.pow(SumToN.sum(n), 2); 14 | return squareOfSum - sumOfSquares; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /src/test/java/com/examplehub/designpatterns/singleton/SingletonExample3Test.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.designpatterns.singleton; 2 | 3 | import static org.junit.jupiter.api.Assertions.*; 4 | 5 | import org.junit.jupiter.api.Test; 6 | 7 | class SingletonExample3Test { 8 | @Test 9 | void test() { 10 | SingletonExample3 firstInstance = SingletonExample3.INSTANCE; 11 | SingletonExample3 secondInstance = SingletonExample3.INSTANCE; 12 | assertEquals(firstInstance.hashCode(), secondInstance.hashCode()); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /src/test/java/com/examplehub/leetcode/easy/NumberOfGoodPairsTest.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.leetcode.easy; 2 | 3 | import static org.junit.jupiter.api.Assertions.*; 4 | 5 | import org.junit.jupiter.api.Test; 6 | 7 | class NumberOfGoodPairsTest { 8 | @Test 9 | void testSolution1() { 10 | int[] numbers = new int[] {1, 2, 3, 1, 1, 3}; 11 | assertEquals(4, NumberOfGoodPairs.solution1(numbers)); 12 | 13 | numbers = new int[] {1, 1, 1, 1}; 14 | assertEquals(6, NumberOfGoodPairs.solution1(numbers)); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /src/test/java/com/examplehub/maths/GetTheFirstDigitRecursionTest.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.maths; 2 | 3 | import static org.junit.jupiter.api.Assertions.*; 4 | 5 | import org.junit.jupiter.api.Test; 6 | 7 | class GetTheFirstDigitRecursionTest { 8 | @Test 9 | void testFirstDigit() { 10 | int[][] testNumbers = {{-123, 1}, {0, 0}, {123, 1}, {123456789, 1}}; 11 | for (int[] testNumber : testNumbers) { 12 | assertEquals(testNumber[1], GetTheFirstDigitRecursion.firstDigit(testNumber[0])); 13 | } 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /src/test/java/com/examplehub/maths/SumToNRecursionTest.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.maths; 2 | 3 | import static org.junit.jupiter.api.Assertions.*; 4 | 5 | import org.junit.jupiter.api.Test; 6 | 7 | class SumToNRecursionTest { 8 | @Test 9 | void testSum() { 10 | assertEquals(0, SumToNRecursion.sum(0)); 11 | assertEquals(1, SumToNRecursion.sum(1)); 12 | assertEquals(3, SumToNRecursion.sum(2)); 13 | assertEquals(55, SumToNRecursion.sum(10)); 14 | assertEquals(5050, SumToNRecursion.sum(100)); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /src/test/java/com/examplehub/leetcode/easy/HappyNumberTest.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.leetcode.easy; 2 | 3 | import static org.junit.jupiter.api.Assertions.*; 4 | 5 | import org.junit.jupiter.api.Test; 6 | 7 | class HappyNumberTest { 8 | @Test 9 | void testSolution1() { 10 | assertTrue(HappyNumber.solution1(19)); 11 | assertFalse(HappyNumber.solution1(2)); 12 | } 13 | 14 | @Test 15 | void testSolution2() { 16 | assertTrue(HappyNumber.solution1(19)); 17 | assertFalse(HappyNumber.solution1(2)); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /src/test/java/com/examplehub/maths/AverageTest.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.maths; 2 | 3 | import static org.junit.jupiter.api.Assertions.*; 4 | 5 | import org.junit.jupiter.api.Test; 6 | 7 | class AverageTest { 8 | 9 | @Test 10 | void testAverage() { 11 | assertEquals(12.0, Average.average(3, 6, 9, 12, 15, 18, 21)); 12 | assertEquals(5.5, Average.average(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)); 13 | assertEquals(3.3, Average.average(1.1, 2.2, 3.3, 4.4, 5.5)); 14 | assertEquals(1.0, Average.average(1.0)); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/basics/ReturnExample.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.basics; 2 | 3 | public class ReturnExample { 4 | public static void main(String[] args) { 5 | System.out.println(test(15, 10)); /* 1 */ 6 | System.out.println(test(10, 15)); /* -1 */ 7 | System.out.println(test(10, 10)); /* 0 */ 8 | } 9 | 10 | private static int test(int test, int target) { 11 | if (test > target) { 12 | return 1; 13 | } 14 | if (test < target) { 15 | return -1; 16 | } 17 | return 0; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /src/test/java/com/examplehub/basics/exception/NullPointerExceptionExampleTest.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.basics.exception; 2 | 3 | import static org.junit.jupiter.api.Assertions.*; 4 | 5 | import org.junit.jupiter.api.Test; 6 | 7 | class NullPointerExceptionExampleTest { 8 | @Test 9 | void testNullPointer() { 10 | try { 11 | String str = null; 12 | System.out.println(" str length = " + str.length()); 13 | fail(); 14 | } catch (NullPointerException e) { 15 | assertTrue(true); 16 | } 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /src/test/java/com/examplehub/basics/reflection/GetSupperClassTest.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.basics.reflection; 2 | 3 | import static org.junit.jupiter.api.Assertions.*; 4 | 5 | import org.junit.jupiter.api.Test; 6 | 7 | class GetSupperClassTest { 8 | @Test 9 | void test() { 10 | Class cls = Integer.class; 11 | assertEquals(Number.class, cls.getSuperclass()); 12 | assertEquals(Object.class, cls.getSuperclass().getSuperclass()); 13 | assertEquals(null, cls.getSuperclass().getSuperclass().getSuperclass()); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /src/test/java/com/examplehub/conversions/BinaryToDecimalTest.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.conversions; 2 | 3 | import static org.junit.jupiter.api.Assertions.*; 4 | 5 | import org.junit.jupiter.api.Test; 6 | 7 | class BinaryToDecimalTest { 8 | @Test 9 | void testBinaryToDecimal() { 10 | assertEquals(0, BinaryToDecimal.toDecimal("0")); 11 | assertEquals(1, BinaryToDecimal.toDecimal("1")); 12 | assertEquals(10, BinaryToDecimal.toDecimal("1010")); 13 | assertEquals(-29, BinaryToDecimal.toDecimal("-11101")); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /src/test/java/com/examplehub/conversions/BinaryToHexadecimalTest.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.conversions; 2 | 3 | import static org.junit.jupiter.api.Assertions.*; 4 | 5 | import org.junit.jupiter.api.Test; 6 | 7 | class BinaryToHexadecimalTest { 8 | 9 | @Test 10 | void testBinaryHexadecimal() { 11 | assertEquals("7", BinaryToHexadecimal.toHexadecimal("0111")); 12 | assertEquals("F", BinaryToHexadecimal.toHexadecimal("1111")); 13 | assertEquals("1569", BinaryToHexadecimal.toHexadecimal("1010101101001")); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /src/test/java/com/examplehub/designpatterns/singleton/SingletonExample1Test.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.designpatterns.singleton; 2 | 3 | import static org.junit.jupiter.api.Assertions.*; 4 | 5 | import org.junit.jupiter.api.Test; 6 | 7 | class SingletonExample1Test { 8 | @Test 9 | void test() { 10 | SingletonExample1 firstInstance = SingletonExample1.getInstance(); 11 | SingletonExample1 secondInstance = SingletonExample1.getInstance(); 12 | assertEquals(firstInstance.hashCode(), secondInstance.hashCode()); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /src/test/java/com/examplehub/designpatterns/singleton/SingletonExample2Test.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.designpatterns.singleton; 2 | 3 | import static org.junit.jupiter.api.Assertions.*; 4 | 5 | import org.junit.jupiter.api.Test; 6 | 7 | class SingletonExample2Test { 8 | @Test 9 | void test() { 10 | SingletonExample2 firstInstance = SingletonExample2.getInstance(); 11 | SingletonExample2 secondInstance = SingletonExample2.getInstance(); 12 | assertEquals(firstInstance.hashCode(), secondInstance.hashCode()); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /src/test/java/com/examplehub/leetcode/easy/ClimbStairsTest.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.leetcode.easy; 2 | 3 | import static org.junit.jupiter.api.Assertions.*; 4 | 5 | import org.junit.jupiter.api.Test; 6 | 7 | class ClimbStairsTest { 8 | @Test 9 | void test() { 10 | assertEquals(0, ClimbStairs.solution1(0)); 11 | assertEquals(1, ClimbStairs.solution1(1)); 12 | assertEquals(2, ClimbStairs.solution1(2)); 13 | assertEquals(3, ClimbStairs.solution1(3)); 14 | assertEquals(5, ClimbStairs.solution1(4)); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /src/test/java/com/examplehub/strings/ReplaceWhiteSpaceTest.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.strings; 2 | 3 | import static org.junit.jupiter.api.Assertions.*; 4 | 5 | import org.junit.jupiter.api.Test; 6 | 7 | class ReplaceWhiteSpaceTest { 8 | @Test 9 | void testSolution1() { 10 | assertEquals("We%20are%20happy.", ReplaceWhiteSpace.solution1("We are happy.", "%20")); 11 | } 12 | 13 | @Test 14 | void testSolution2() { 15 | assertEquals("We%20are%20happy.", ReplaceWhiteSpace.solution2("We are happy.", "%20")); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/basics/oop/FunctionOverriding.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.basics.oop; 2 | 3 | public class FunctionOverriding { 4 | public static void main(String[] args) { 5 | Vehicle bike = new Bike(); 6 | bike.run(); /* Bike is running. */ 7 | } 8 | } 9 | 10 | class Vehicle { 11 | public void run() { 12 | System.out.println("Vehicle is running."); 13 | } 14 | } 15 | 16 | class Bike extends Vehicle { 17 | @Override 18 | public void run() { 19 | System.out.println("Bike is running."); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/maths/Average.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.maths; 2 | 3 | /** https://en.wikipedia.org/wiki/Average */ 4 | public class Average { 5 | 6 | /** 7 | * Calculate average value of numbers. 8 | * 9 | * @param numbers the numbers to be calculated. 10 | * @return average value of numbers. 11 | */ 12 | public static double average(double... numbers) { 13 | double sum = 0; 14 | for (double number : numbers) { 15 | sum += number; 16 | } 17 | return sum / numbers.length; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /src/test/java/com/examplehub/basics/exception/AssertionExampleTest.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.basics.exception; 2 | 3 | import static org.junit.jupiter.api.Assertions.*; 4 | 5 | import org.junit.jupiter.api.Test; 6 | 7 | class AssertionExampleTest { 8 | @Test 9 | void test() { 10 | assert 3 + 2 == 5; 11 | 12 | try { 13 | assert 3 + 2 != 5 : "error msg"; 14 | fail(); 15 | } catch (AssertionError error) { 16 | assertTrue(true); 17 | assertEquals("error msg", error.getMessage()); 18 | } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /src/test/java/com/examplehub/leetcode/easy/ContainsDuplicateIITest.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.leetcode.easy; 2 | 3 | import static org.junit.jupiter.api.Assertions.*; 4 | 5 | import org.junit.jupiter.api.Test; 6 | 7 | class ContainsDuplicateIITest { 8 | @Test 9 | void testSolution1() { 10 | assertTrue(ContainsDuplicateII.solution1(new int[] {1, 2, 3, 1}, 3)); 11 | assertTrue(ContainsDuplicateII.solution1(new int[] {1, 0, 1, 1}, 1)); 12 | assertFalse(ContainsDuplicateII.solution1(new int[] {1, 2, 3, 1, 2, 3}, 2)); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /src/test/java/com/examplehub/maths/RangeTest.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.maths; 2 | 3 | import static org.junit.jupiter.api.Assertions.*; 4 | 5 | import java.util.Arrays; 6 | import org.junit.jupiter.api.Test; 7 | 8 | class RangeTest { 9 | @Test 10 | void testRange() { 11 | assertTrue(Arrays.equals(new int[] {0, 1, 2, 3, 4}, Range.range(5))); 12 | assertTrue(Arrays.equals(new int[] {10, 11, 12, 13, 14, 15}, Range.range(10, 16))); 13 | assertTrue(Arrays.equals(new int[] {1, 3, 5, 7, 9}, Range.range(1, 10, 2))); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/basics/VarArgsExample.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.basics; 2 | 3 | public class VarArgsExample { 4 | public static void main(String[] args) { 5 | System.out.println("1 + 2 = " + add(1, 2)); /* 1 + 2 = 3 */ 6 | System.out.println("1 + 2 + 3 = " + add(1, 2, 3)); 7 | System.out.println("1 + 2 + 3 + 4 = " + add(1, 2, 3, 4)); 8 | } 9 | 10 | public static int add(int... numbers) { 11 | int sum = 0; 12 | for (int number : numbers) { 13 | sum += number; 14 | } 15 | return sum; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /src/test/java/com/examplehub/basics/thread/PriorityOfThreadTest.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.basics.thread; 2 | 3 | import static org.junit.jupiter.api.Assertions.*; 4 | 5 | import org.junit.jupiter.api.Test; 6 | 7 | class PriorityOfThreadTest { 8 | @Test 9 | void test() { 10 | Thread thread = 11 | new Thread( 12 | () -> { 13 | assertEquals(Thread.MAX_PRIORITY, Thread.currentThread().getPriority()); 14 | }); 15 | thread.setPriority(Thread.MAX_PRIORITY); 16 | thread.start(); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /src/test/java/com/examplehub/maths/AbsoluteValueTest.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.maths; 2 | 3 | import static org.junit.jupiter.api.Assertions.*; 4 | 5 | import org.junit.jupiter.api.Test; 6 | 7 | class AbsoluteValueTest { 8 | 9 | @Test 10 | void test() { 11 | assertEquals(AbsoluteValue.absoluteValue(-1), Math.abs(-1)); 12 | assertEquals(AbsoluteValue.absoluteValue(0), Math.abs(0)); 13 | assertEquals(AbsoluteValue.absoluteValue(1), Math.abs(1)); 14 | assertEquals(AbsoluteValue.absoluteValue(100), Math.abs(-100)); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /src/test/java/com/examplehub/maths/MedianTest.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.maths; 2 | 3 | import static org.junit.jupiter.api.Assertions.*; 4 | 5 | import org.junit.jupiter.api.Test; 6 | 7 | class MedianTest { 8 | 9 | @Test 10 | void testMedian() { 11 | assertEquals(0, Median.median(new double[] {0})); 12 | assertEquals(2.5, Median.median(new double[] {4, 1, 3, 2})); 13 | assertEquals(6, Median.median(new double[] {1, 3, 3, 6, 7, 8, 9})); 14 | assertEquals(8, Median.median(new double[] {2, 70, 6, 50, 20, 8, 4})); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/basics/bool/BooleanExample.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.basics.bool; 2 | 3 | public class BooleanExample { 4 | public static void main(String[] args) { 5 | System.out.println(true); // true 6 | System.out.println(false); // false 7 | // System.out.println(3 == 4); // false 8 | // System.out.println(3 == 3); // true 9 | // 10 | // int age = 18; 11 | // System.out.println(age >= 10 && age <= 17); // false 12 | // System.out.println(age >= 10 && age <= 19); // true 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/leetcode/easy/CountNegativeNumbersInASortedMatrix.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.leetcode.easy; 2 | 3 | /** https://leetcode.com/problems/count-negative-numbers-in-a-sorted-matrix/ */ 4 | public class CountNegativeNumbersInASortedMatrix { 5 | public static int solution1(int[][] grid) { 6 | int countNegative = 0; 7 | for (int[] columns : grid) { 8 | for (int num : columns) { 9 | if (num < 0) { 10 | countNegative++; 11 | } 12 | } 13 | } 14 | return countNegative; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/leetcode/easy/ShuffleTheArray.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.leetcode.easy; 2 | 3 | /** https://leetcode.com/problems/shuffle-the-array/ */ 4 | public class ShuffleTheArray { 5 | public static int[] solution1(int[] nums, int n) { 6 | int[] result = new int[nums.length]; 7 | int index = 0; 8 | for (int i = 0, j = n; i < n; i++, j++) { 9 | result[index++] = nums[i]; 10 | result[index++] = nums[j]; 11 | } 12 | System.arraycopy(result, 0, nums, 0, result.length); 13 | return nums; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /src/test/java/com/examplehub/designpatterns/decorator/DecoratorTest.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.designpatterns.decorator; 2 | 3 | import org.junit.jupiter.api.Disabled; 4 | import org.junit.jupiter.api.Test; 5 | 6 | class DecoratorTest { 7 | @Test 8 | @Disabled 9 | void test() { 10 | Readable readable = new Machine(); 11 | readable.read(); // I'm reading 12 | 13 | /* 14 | * I'm reading 15 | * I not have enough power 16 | */ 17 | Decorator decorator = new Decorator(readable); 18 | decorator.read(); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /src/test/java/com/examplehub/projecteuler/Problem01Test.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.projecteuler; 2 | 3 | import static org.junit.jupiter.api.Assertions.*; 4 | 5 | import org.junit.jupiter.api.Test; 6 | 7 | class Problem01Test { 8 | 9 | @Test 10 | void testSolution1() { 11 | 12 | assertEquals(0, Problem01.solution1(-100)); 13 | assertEquals(0, Problem01.solution1(3)); 14 | assertEquals(3, Problem01.solution1(4)); 15 | assertEquals(23, Problem01.solution1(10)); 16 | assertEquals(233168, Problem01.solution1(1000)); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/basics/oop/InterfaceExample.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.basics.oop; 2 | 3 | public class InterfaceExample {} 4 | 5 | interface USB { 6 | String charge(); 7 | 8 | default String defaultFunction() { 9 | return "hello world"; 10 | } 11 | } 12 | 13 | class Computer implements USB { 14 | 15 | @Override 16 | public String charge() { 17 | return "computer's usb"; 18 | } 19 | } 20 | 21 | class Phone implements USB { 22 | 23 | @Override 24 | public String charge() { 25 | return "phone's usb"; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/leetcode/easy/RemoveDuplicatesFromSortedArray.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.leetcode.easy; 2 | 3 | /** https://leetcode.com/problems/remove-duplicates-from-sorted-array/ */ 4 | public class RemoveDuplicatesFromSortedArray { 5 | 6 | public static int solution1(int[] nums) { 7 | if (nums.length == 0) { 8 | return 0; 9 | } 10 | int i = 0; 11 | for (int j = 1; j < nums.length; ++j) { 12 | if (nums[j] != nums[i]) { 13 | nums[++i] = nums[j]; 14 | } 15 | } 16 | return i + 1; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/projecteuler/Problem21.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.projecteuler; 2 | 3 | import com.examplehub.maths.SumOfDivisor; 4 | 5 | /** https://projecteuler.net/problem=21 */ 6 | public class Problem21 { 7 | public static int solution1(int n) { 8 | int sum = 0; 9 | for (int i = 1; i < n; i++) { 10 | int sumOfDivisor = SumOfDivisor.sumOfDivisorExclude(i); 11 | if (SumOfDivisor.sumOfDivisorExclude(sumOfDivisor) == i && sumOfDivisor != i) { 12 | sum += i; 13 | } 14 | } 15 | return sum; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /src/test/java/com/examplehub/leetcode/utils/BinaryTreeUtilsTest.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.leetcode.utils; 2 | 3 | import static org.junit.jupiter.api.Assertions.*; 4 | 5 | import com.examplehub.leetcode.TreeNode; 6 | import org.junit.jupiter.api.Test; 7 | 8 | class BinaryTreeUtilsTest { 9 | @Test 10 | void test() { 11 | TreeNode root = BinaryTreeUtils.createTree(new int[] {1, 2, 3}, 0); 12 | assert root != null; 13 | assertEquals(1, root.val); 14 | assertEquals(2, root.left.val); 15 | assertEquals(3, root.right.val); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /src/test/java/com/examplehub/maths/CountDigitsTest.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.maths; 2 | 3 | import static org.junit.jupiter.api.Assertions.*; 4 | 5 | import org.junit.jupiter.api.Test; 6 | 7 | class CountDigitsTest { 8 | 9 | @Test 10 | void testCountDigits() { 11 | assertEquals(3, CountDigits.countDigits(-123)); 12 | assertEquals(1, CountDigits.countDigits(-1)); 13 | assertEquals(1, CountDigits.countDigits(0)); 14 | assertEquals(3, CountDigits.countDigits(123)); 15 | assertEquals(6, CountDigits.countDigits(123456)); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/basics/oop/DeclareMethod.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.basics.oop; 2 | 3 | public class DeclareMethod { 4 | private String name; 5 | private int age; 6 | 7 | public String getName() { 8 | return name; 9 | } 10 | 11 | public void setName(String name) { 12 | this.name = name; 13 | } 14 | 15 | public int getAge() { 16 | return age; 17 | } 18 | 19 | public void setAge(int age) { 20 | this.age = age; 21 | } 22 | 23 | public String intro() { 24 | return "I'm " + name + ". I'm " + age; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/designpatterns/singleton/SingletonExample5.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.designpatterns.singleton; 2 | 3 | public class SingletonExample5 { 4 | private static SingletonExample5 INSTANCE; 5 | 6 | private SingletonExample5() {} 7 | 8 | public static SingletonExample5 getInstance() { 9 | if (INSTANCE == null) { 10 | synchronized (SingletonExample5.class) { 11 | if (INSTANCE == null) { 12 | INSTANCE = new SingletonExample5(); 13 | } 14 | } 15 | } 16 | return INSTANCE; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/leetcode/easy/PathSum.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.leetcode.easy; 2 | 3 | import com.examplehub.leetcode.TreeNode; 4 | 5 | /** https://leetcode.com/problems/path-sum/ */ 6 | public class PathSum { 7 | public static boolean solution1(TreeNode root, int sum) { 8 | if (root == null) { 9 | return false; 10 | } 11 | if (root.val == sum && root.left == null && root.right == null) { 12 | return true; 13 | } 14 | return solution1(root.left, sum - root.val) || solution1(root.right, sum - root.val); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/leetcode/easy/ReverseLinkedList.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.leetcode.easy; 2 | 3 | import com.examplehub.leetcode.ListNode; 4 | 5 | /** https://leetcode.com/problems/reverse-linked-list/ */ 6 | public class ReverseLinkedList { 7 | public static ListNode solution1(ListNode head) { 8 | ListNode prev = null; 9 | ListNode cur = head; 10 | while (cur != null) { 11 | ListNode nextTemp = cur.next; 12 | cur.next = prev; 13 | prev = cur; 14 | cur = nextTemp; 15 | } 16 | return prev; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/leetcode/utils/BinaryTreeUtils.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.leetcode.utils; 2 | 3 | import com.examplehub.leetcode.TreeNode; 4 | 5 | public class BinaryTreeUtils { 6 | public static TreeNode createTree(int[] nodes, int i) { 7 | int numberOfNodes = nodes.length; 8 | if (i >= numberOfNodes || nodes[i] == -1) { 9 | return null; 10 | } 11 | TreeNode root = new TreeNode(nodes[i]); 12 | root.left = createTree(nodes, i * 2 + 1); 13 | root.right = createTree(nodes, i * 2 + 2); 14 | return root; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/maths/SquareNumber.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.maths; 2 | 3 | /** https://en.wikipedia.org/wiki/Square_number */ 4 | public class SquareNumber { 5 | 6 | /** 7 | * Test if a number is square number or not. 8 | * 9 | * @param number the number to checked. 10 | * @return {@code true} if given {@code number} is square number, otherwise {@code false}. 11 | */ 12 | public static boolean isSquareNumber(int number) { 13 | return number >= 0 && (int) Math.sqrt(number) * (int) Math.sqrt(number) == number; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /src/test/java/com/examplehub/basics/regex/SplitReplaceExampleTest.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.basics.regex; 2 | 3 | import static org.junit.jupiter.api.Assertions.*; 4 | 5 | import java.util.Arrays; 6 | import org.junit.jupiter.api.Test; 7 | 8 | class SplitReplaceExampleTest { 9 | @Test 10 | void testSplit() { 11 | assertEquals("[a, b, c]", Arrays.toString("a b c".split("\\s"))); 12 | assertEquals("[a, b, , c]", Arrays.toString("a b c".split("\\s"))); 13 | assertEquals("[a, b, c]", Arrays.toString("a, b ;; c".split("[,;\\s]+"))); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /src/test/java/com/examplehub/strings/StringJoinerExampleTest.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.strings; 2 | 3 | import static org.junit.jupiter.api.Assertions.assertEquals; 4 | 5 | import com.examplehub.basics.chars.StringJoinerExample; 6 | import org.junit.jupiter.api.Test; 7 | 8 | class StringJoinerExampleTest { 9 | 10 | @Test 11 | void example1() { 12 | assertEquals(StringJoinerExample.example1(), "Java->Python->C"); 13 | } 14 | 15 | @Test 16 | void example2() { 17 | assertEquals(StringJoinerExample.example2(), "[Java->Python->C]"); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/basics/io/RecursionPrintDirectory.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.basics.io; 2 | 3 | import java.io.File; 4 | import java.util.Objects; 5 | 6 | public class RecursionPrintDirectory { 7 | public static void printFiles(File dir) { 8 | System.out.print("\t"); 9 | if (dir.isFile()) { 10 | System.out.println(dir.getName()); 11 | } else { 12 | System.out.println(dir.getName() + "/"); 13 | for (File file : Objects.requireNonNull(dir.listFiles())) { 14 | printFiles(file); 15 | } 16 | } 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /src/test/java/com/examplehub/basics/system/SystemExampleTest.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.basics.system; 2 | 3 | import static org.junit.jupiter.api.Assertions.*; 4 | 5 | import org.junit.jupiter.api.Test; 6 | 7 | class SystemExampleTest { 8 | @Test 9 | void testCurrentTime() { 10 | long startTime = System.currentTimeMillis(); 11 | int sum = 0; 12 | for (int i = 1; i <= 100; ++i) { 13 | sum += i; 14 | } 15 | assertEquals(5050, sum); 16 | long endTime = System.currentTimeMillis(); 17 | assertTrue(endTime >= startTime); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /src/test/java/com/examplehub/designpatterns/template/CalculatorTest.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.designpatterns.template; 2 | 3 | import static org.junit.jupiter.api.Assertions.*; 4 | 5 | import org.junit.jupiter.api.Test; 6 | 7 | class CalculatorTest { 8 | @Test 9 | void testPlus() { 10 | Calculator calculator = new Plus(); 11 | assertEquals(5, calculator.calculate("2+3", "\\+")); 12 | } 13 | 14 | @Test 15 | void testMinus() { 16 | Calculator calculator = new Minus(); 17 | assertEquals(-1, calculator.calculate("2-3", "-")); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /src/test/java/com/examplehub/maths/AreAllTrueTest.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.maths; 2 | 3 | import static org.junit.jupiter.api.Assertions.*; 4 | 5 | import org.junit.jupiter.api.Test; 6 | 7 | class AreAllTrueTest { 8 | @Test 9 | void test() { 10 | assertTrue(AreAllTrue.areAllTrue(true, true, true, true)); 11 | assertFalse(AreAllTrue.areAllTrue(true, true, true, false)); 12 | assertFalse(AreAllTrue.areAllTrue(true, false, true, false)); 13 | assertFalse(AreAllTrue.areAllTrue(false)); 14 | assertTrue(AreAllTrue.areAllTrue(true)); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /src/test/java/com/examplehub/maths/PowerOfTwoRecursionTest.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.maths; 2 | 3 | import static org.junit.jupiter.api.Assertions.*; 4 | 5 | import org.junit.jupiter.api.Test; 6 | 7 | class PowerOfTwoRecursionTest { 8 | @Test 9 | void test() { 10 | for (int i = -5; i <= 0; ++i) { 11 | assertFalse(PowerOfTwoRecursion.isPowerOfTwo(i)); 12 | } 13 | assertFalse(PowerOfTwoRecursion.isPowerOfTwo(99)); 14 | assertTrue(PowerOfTwoRecursion.isPowerOfTwo(64)); 15 | assertTrue(PowerOfTwoRecursion.isPowerOfTwo(1024)); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /src/test/java/com/examplehub/maths/PowerTest.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.maths; 2 | 3 | import static org.junit.jupiter.api.Assertions.*; 4 | 5 | import org.junit.jupiter.api.Test; 6 | 7 | class PowerTest { 8 | 9 | @Test 10 | void testPow() { 11 | assertEquals(1, Power.pow(0, 0)); 12 | assertEquals(0, Power.pow(0, 10)); 13 | assertEquals(1, Power.pow(1, 0)); 14 | assertEquals(2, Power.pow(2, 1)); 15 | assertEquals(8, Power.pow(2, 3)); 16 | assertEquals(1024, Power.pow(2, 10)); 17 | assertEquals(625, Power.pow(25, 2)); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/leetcode/easy/SubtractTheProductAndSumOfDigitsOfAnInteger.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.leetcode.easy; 2 | 3 | /** https://leetcode.com/problems/subtract-the-product-and-sum-of-digits-of-an-integer/ */ 4 | public class SubtractTheProductAndSumOfDigitsOfAnInteger { 5 | public static int solution1(int n) { 6 | int product = 1; 7 | int sum = 0; 8 | while (n != 0) { 9 | int remainder = n % 10; 10 | product *= remainder; 11 | sum += remainder; 12 | n /= 10; 13 | } 14 | return product - sum; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /src/test/java/com/examplehub/basics/lambda/SumWithLambdaTest.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.basics.lambda; 2 | 3 | import static org.junit.jupiter.api.Assertions.*; 4 | 5 | import org.junit.jupiter.api.Test; 6 | 7 | class SumWithLambdaTest { 8 | @Test 9 | void testLambda() { 10 | SumWithLambda sumWithLambda = 11 | n -> { 12 | int total = 0; 13 | for (int i = 1; i <= n; ++i) { 14 | total = total + i; 15 | } 16 | return total; 17 | }; 18 | assertEquals(5050, sumWithLambda.sum(100)); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /src/test/java/com/examplehub/basics/oop/AnonymousClassTest.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.basics.oop; 2 | 3 | import static org.junit.jupiter.api.Assertions.*; 4 | 5 | import org.junit.jupiter.api.Test; 6 | 7 | class AnonymousClassTest { 8 | @Test 9 | void test() { 10 | AnonymousClass anonymousClass = new AnonymousClass("JACK"); 11 | anonymousClass.asyncHi(); 12 | try { 13 | Thread.sleep(1000); 14 | } catch (InterruptedException e) { 15 | e.printStackTrace(); 16 | } 17 | assertEquals("Hi, JACK", anonymousClass.getHiMsg()); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /src/test/java/com/examplehub/maths/PrimeFactorsTest.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.maths; 2 | 3 | import static org.junit.jupiter.api.Assertions.*; 4 | 5 | import java.util.Arrays; 6 | import org.junit.jupiter.api.Test; 7 | 8 | class PrimeFactorsTest { 9 | 10 | @Test 11 | void testPrimeFactors() { 12 | assertTrue(Arrays.equals(new int[] {2}, PrimeFactors.primeFactors(2))); 13 | assertTrue(Arrays.equals(new int[] {2, 2, 3}, PrimeFactors.primeFactors(12))); 14 | assertTrue(Arrays.equals(new int[] {3, 3, 5, 7}, PrimeFactors.primeFactors(315))); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/leetcode/easy/RichestCustomerWealth.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.leetcode.easy; 2 | 3 | import com.examplehub.maths.SumOfArray; 4 | 5 | /** https://leetcode.com/problems/richest-customer-wealth/ */ 6 | public class RichestCustomerWealth { 7 | public static int solution1(int[][] accounts) { 8 | int max = SumOfArray.sum(accounts[0]); 9 | for (int i = 1; i < accounts.length; ++i) { 10 | int temp = SumOfArray.sum(accounts[i]); 11 | if (temp > max) { 12 | max = temp; 13 | } 14 | } 15 | return max; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /src/test/java/com/examplehub/basics/thread/SellTicketThreadTest.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.basics.thread; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | class SellTicketThreadTest { 6 | @Test 7 | void test() { 8 | SellTicketThread sellTicketThread = new SellTicketThread(); 9 | 10 | Thread firstThread = new Thread(sellTicketThread); 11 | Thread secondThread = new Thread(sellTicketThread); 12 | Thread thirdThread = new Thread(sellTicketThread); 13 | 14 | firstThread.start(); 15 | secondThread.start(); 16 | thirdThread.start(); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/projecteuler/Problem30.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.projecteuler; 2 | 3 | /** https://projecteuler.net/problem=30 */ 4 | public class Problem30 { 5 | 6 | public static int solution1() { 7 | int sum = 0; 8 | for (int i = 2; i <= 1000000; i++) { 9 | int number = i; 10 | int tempSum = 0; 11 | while (number != 0) { 12 | tempSum = (int) (tempSum + Math.pow(number % 10, 5)); 13 | number /= 10; 14 | } 15 | if (tempSum == i) { 16 | sum += i; 17 | } 18 | } 19 | return sum; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /src/test/java/com/examplehub/basics/set/LinkedListExampleTest.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.basics.set; 2 | 3 | import static org.junit.jupiter.api.Assertions.*; 4 | 5 | import java.util.LinkedHashSet; 6 | import java.util.Set; 7 | import org.junit.jupiter.api.Test; 8 | 9 | class LinkedListExampleTest { 10 | @Test 11 | void testAdd() { 12 | Set set = new LinkedHashSet<>(); 13 | assertTrue(set.add(2)); 14 | assertFalse(set.add(2)); 15 | assertTrue(set.add(3)); 16 | assertTrue(set.add(1)); 17 | assertEquals("[2, 3, 1]", set.toString()); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /src/test/java/com/examplehub/datastructures/array/MergeSortedArrayTest.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.datastructures.array; 2 | 3 | import static org.junit.jupiter.api.Assertions.assertArrayEquals; 4 | 5 | import java.util.stream.IntStream; 6 | import org.junit.jupiter.api.Test; 7 | 8 | class MergeSortedArrayTest { 9 | 10 | @Test 11 | void testMerge() { 12 | int[] firstArray = {1, 3, 5, 7, 9}; 13 | int[] secondArray = {2, 4, 6, 8, 10}; 14 | assertArrayEquals( 15 | IntStream.range(1, 11).toArray(), MergeSortedArray.merge(firstArray, secondArray)); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /src/test/java/com/examplehub/projecteuler/Problem07Test.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.projecteuler; 2 | 3 | import static org.junit.jupiter.api.Assertions.*; 4 | 5 | import org.junit.jupiter.api.Test; 6 | 7 | class Problem07Test { 8 | @Test 9 | void testSolution1() { 10 | assertEquals(2, Problem07.solution(1)); 11 | assertEquals(3, Problem07.solution(2)); 12 | assertEquals(5, Problem07.solution(3)); 13 | assertEquals(7, Problem07.solution(4)); 14 | assertEquals(13, Problem07.solution(6)); 15 | assertEquals(104743, Problem07.solution(10001)); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/basics/RegexExample.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.basics; 2 | 3 | import java.util.regex.Matcher; 4 | import java.util.regex.Pattern; 5 | 6 | public class RegexExample { 7 | public static void main(String[] args) { 8 | 9 | /* 10 | * Searched 11 | */ 12 | Pattern pattern = Pattern.compile("hub", Pattern.CASE_INSENSITIVE); 13 | Matcher matcher = pattern.matcher("ExampleHub"); 14 | if (matcher.find()) { 15 | System.out.println("Searched"); 16 | } else { 17 | System.out.println("Not searched"); 18 | } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/basics/oop/SetterGetter.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.basics.oop; 2 | 3 | public class SetterGetter { 4 | private String name; 5 | private int age; 6 | 7 | public void setName(String name) { 8 | this.name = name; 9 | } 10 | 11 | public void setAge(int age) { 12 | if (age < 0 || age > 150) { 13 | throw new IllegalArgumentException("invalid age:" + age); 14 | } 15 | this.age = age; 16 | } 17 | 18 | public String getName() { 19 | return name; 20 | } 21 | 22 | public int getAge() { 23 | return age; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/leetcode/easy/SameTree.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.leetcode.easy; 2 | 3 | import com.examplehub.leetcode.TreeNode; 4 | 5 | /** https://leetcode.com/problems/same-tree/ */ 6 | public class SameTree { 7 | public static boolean solution1(TreeNode p, TreeNode q) { 8 | if (p == null && q == null) { 9 | return true; 10 | } 11 | if (p == null || q == null) { 12 | return false; 13 | } 14 | if (p.val != q.val) { 15 | return false; 16 | } 17 | return solution1(p.left, q.left) && solution1(p.right, q.right); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/leetcode/easy/SumOfAllOddLengthSubarrays.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.leetcode.easy; 2 | 3 | /** https://leetcode.com/problems/sum-of-all-odd-length-subarrays/ */ 4 | public class SumOfAllOddLengthSubarrays { 5 | public static int solution1(int[] arr) { 6 | int sum = 0; 7 | for (int subLen = 1, len = arr.length; subLen <= len; subLen += 2) { 8 | for (int i = 0; i + subLen <= len; ++i) { 9 | for (int j = i; j < i + subLen; ++j) { 10 | sum = sum + arr[j]; 11 | } 12 | } 13 | } 14 | return sum; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /src/test/java/com/examplehub/maths/GetTheFirstDigitTest.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.maths; 2 | 3 | import static org.junit.jupiter.api.Assertions.*; 4 | 5 | import org.junit.jupiter.api.Test; 6 | 7 | class GetTheFirstDigitTest { 8 | 9 | @Test 10 | void test() { 11 | int[][] testNumbers = {{-123, 1}, {0, 0}, {123, 1}, {123456789, 1}}; 12 | for (int[] testNumber : testNumbers) { 13 | assertEquals(testNumber[1], GetTheFirstDigit.firstDigit(testNumber[0])); 14 | assertEquals(testNumber[1], GetTheFirstDigit.firstDigitByString(testNumber[0])); 15 | } 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /.github/workflows/maven.yml: -------------------------------------------------------------------------------- 1 | # This workflow will build a Java project with Maven 2 | # For more information see: https://help.github.com/actions/language-and-framework-guides/building-and-testing-java-with-maven 3 | 4 | name: Java CI with Maven 5 | 6 | on: [push, pull_request] 7 | 8 | jobs: 9 | build: 10 | 11 | runs-on: ubuntu-latest 12 | 13 | steps: 14 | - uses: actions/checkout@v2 15 | - name: Set up JDK 14 16 | uses: actions/setup-java@v1 17 | with: 18 | java-version: 14 19 | - name: Build with Maven 20 | run: mvn clean test 21 | -------------------------------------------------------------------------------- /src/test/java/com/examplehub/designpatterns/singleton/SingletonExample4Test.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.designpatterns.singleton; 2 | 3 | import static org.junit.jupiter.api.Assertions.*; 4 | 5 | import org.junit.jupiter.api.Test; 6 | 7 | class SingletonExample4Test { 8 | @Test 9 | void test() { 10 | SingletonExample4 firstInstance = SingletonExample4.getInstance(); 11 | SingletonExample4 secondInstance = SingletonExample4.getInstance(); 12 | assertSame(firstInstance, secondInstance); 13 | } 14 | 15 | @Test 16 | void testMultiThreads() { 17 | // TODO 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /src/test/java/com/examplehub/designpatterns/singleton/SingletonExample5Test.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.designpatterns.singleton; 2 | 3 | import static org.junit.jupiter.api.Assertions.*; 4 | 5 | import org.junit.jupiter.api.Test; 6 | 7 | class SingletonExample5Test { 8 | @Test 9 | void test() { 10 | SingletonExample5 firstInstance = SingletonExample5.getInstance(); 11 | SingletonExample5 secondInstance = SingletonExample5.getInstance(); 12 | assertSame(firstInstance, secondInstance); 13 | } 14 | 15 | @Test 16 | void testMultiThreads() { 17 | // TODO 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /src/test/java/com/examplehub/designpatterns/singleton/SingletonExample6Test.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.designpatterns.singleton; 2 | 3 | import static org.junit.jupiter.api.Assertions.*; 4 | 5 | import org.junit.jupiter.api.Test; 6 | 7 | class SingletonExample6Test { 8 | @Test 9 | void test() { 10 | SingletonExample6 firstInstance = SingletonExample6.getInstance(); 11 | SingletonExample6 secondInstance = SingletonExample6.getInstance(); 12 | assertSame(firstInstance, secondInstance); 13 | } 14 | 15 | @Test 16 | void testMultiThreads() { 17 | // TODO 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /src/test/java/com/examplehub/conversions/DecimalToHexlTest.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.conversions; 2 | 3 | import static org.junit.jupiter.api.Assertions.assertEquals; 4 | 5 | import org.junit.jupiter.api.Test; 6 | 7 | class DecimalToHexlTest { 8 | @Test 9 | void testDecimalToOctal() { 10 | assertEquals(Integer.toString(0, 16), DecimalToHex.toHex(0)); 11 | assertEquals(Integer.toString(6, 16), DecimalToHex.toHex(6)); 12 | assertEquals(Integer.toString(111, 16), DecimalToHex.toHex(111)); 13 | assertEquals(Integer.toString(-1111, 16), DecimalToHex.toHex(-1111)); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/leetcode/easy/RemoveDuplicatesFromSortedList.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.leetcode.easy; 2 | 3 | import com.examplehub.leetcode.ListNode; 4 | 5 | /** https://leetcode.com/problems/remove-duplicates-from-sorted-list/ */ 6 | public class RemoveDuplicatesFromSortedList { 7 | 8 | public static ListNode solution1(ListNode head) { 9 | ListNode p = head; 10 | while (p != null && p.next != null) { 11 | if (p.val == p.next.val) { 12 | p.next = p.next.next; 13 | } else { 14 | p = p.next; 15 | } 16 | } 17 | return head; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /src/test/java/com/examplehub/utils/NodeUtilsTest.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.utils; 2 | 3 | import static org.junit.jupiter.api.Assertions.*; 4 | 5 | import com.examplehub.leetcode.ListNode; 6 | import org.junit.jupiter.api.Test; 7 | 8 | class NodeUtilsTest { 9 | @Test 10 | void testLength() { 11 | ListNode head = NodeUtils.makeList(); 12 | assertEquals(0, NodeUtils.length(head)); 13 | 14 | head = NodeUtils.makeList(1, 2, 3, 4); 15 | assertEquals(4, NodeUtils.length(head)); 16 | 17 | head = NodeUtils.makeList(6); 18 | assertEquals(1, NodeUtils.length(head)); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/basics/MethodOverloading.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.basics; 2 | 3 | public class MethodOverloading { 4 | public static int sum(int a, int b) { 5 | return a + b; 6 | } 7 | 8 | public static int sum(int a, int b, int c, int d) { 9 | return sum(a, b) + sum(c, d); /* return a + b + c + d */ 10 | } 11 | 12 | public static int sum(int... numbers) { 13 | int sum = 0; 14 | for (int number : numbers) { 15 | sum += number; 16 | } 17 | return sum; 18 | } 19 | 20 | public static float sum(float a, float b) { 21 | return a + b; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/maths/AbsoluteMax.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.maths; 2 | 3 | public class AbsoluteMax { 4 | 5 | /** 6 | * Calculate absolute max value. 7 | * 8 | * @param numbers the numbers to be checked. 9 | * @return absolute max value. 10 | */ 11 | public static int absoluteMax(int[] numbers) { 12 | int absMax = Math.abs(numbers[0]); 13 | int number = numbers[0]; 14 | for (int temp : numbers) { 15 | if (Math.abs(temp) > absMax) { 16 | absMax = Math.abs(temp); 17 | number = temp; 18 | } 19 | } 20 | return number; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/maths/AbsoluteMin.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.maths; 2 | 3 | public class AbsoluteMin { 4 | 5 | /** 6 | * Calculate absolute min value. 7 | * 8 | * @param numbers the numbers to be checked. 9 | * @return absolute min value. 10 | */ 11 | public static int absoluteMin(int[] numbers) { 12 | int absMin = Math.abs(numbers[0]); 13 | int number = numbers[0]; 14 | for (int temp : numbers) { 15 | if (Math.abs(temp) < absMin) { 16 | absMin = Math.abs(temp); 17 | number = temp; 18 | } 19 | } 20 | return number; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/projecteuler/Problem34.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.projecteuler; 2 | 3 | import com.examplehub.maths.Factorial; 4 | 5 | /** https://projecteuler.net/problem=34 */ 6 | public class Problem34 { 7 | public static int solution1() { 8 | int bigSum = 0; 9 | for (int i = 3; i <= 100000; i++) { 10 | int sum = 0; 11 | int num = i; 12 | while (num != 0) { 13 | sum = (int) (sum + Factorial.factorial(num % 10)); 14 | num /= 10; 15 | } 16 | if (i == sum) { 17 | bigSum += i; 18 | } 19 | } 20 | return bigSum; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /src/test/java/com/examplehub/basics/io/DeleteFileTest.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.basics.io; 2 | 3 | import static org.junit.jupiter.api.Assertions.*; 4 | 5 | import java.io.File; 6 | import java.io.IOException; 7 | import org.junit.jupiter.api.Test; 8 | 9 | class DeleteFileTest { 10 | @Test 11 | void test() throws IOException { 12 | String path = "example.txt"; 13 | File file = new File(path); 14 | boolean isCreated = file.createNewFile(); 15 | assertTrue(file.delete()); 16 | 17 | path = "non_exists.txt"; 18 | file = new File(path); 19 | assertFalse(file.delete()); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /src/test/java/com/examplehub/conversions/DecimalToBinaryTest.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.conversions; 2 | 3 | import static org.junit.jupiter.api.Assertions.*; 4 | 5 | import org.junit.jupiter.api.Test; 6 | 7 | class DecimalToBinaryTest { 8 | @Test 9 | void testDecimalToBinary() { 10 | assertEquals(Integer.toString(0, 2), DecimalToBinary.toBinary(0)); 11 | assertEquals(Integer.toString(6, 2), DecimalToBinary.toBinary(6)); 12 | assertEquals(Integer.toString(111, 2), DecimalToBinary.toBinary(111)); 13 | assertEquals(Integer.toString(-1111, 2), DecimalToBinary.toBinary(-1111)); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /src/test/java/com/examplehub/conversions/DecimalToOctalTest.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.conversions; 2 | 3 | import static org.junit.jupiter.api.Assertions.assertEquals; 4 | 5 | import org.junit.jupiter.api.Test; 6 | 7 | class DecimalToOctalTest { 8 | @Test 9 | void testDecimalToOctal() { 10 | assertEquals(Integer.toString(0, 8), DecimalToOctal.toOctal(0)); 11 | assertEquals(Integer.toString(6, 8), DecimalToOctal.toOctal(6)); 12 | assertEquals(Integer.toString(111, 8), DecimalToOctal.toOctal(111)); 13 | assertEquals(Integer.toString(-1111, 8), DecimalToOctal.toOctal(-1111)); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/basics/oop/AnonymousClass.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.basics.oop; 2 | 3 | public class AnonymousClass { 4 | private String name; 5 | private String hiMsg; 6 | 7 | public AnonymousClass(String name) { 8 | this.name = name; 9 | } 10 | 11 | public void asyncHi() { 12 | new Thread( 13 | new Runnable() { 14 | @Override 15 | public void run() { 16 | hiMsg = "Hi, " + name; 17 | } 18 | }) 19 | .start(); 20 | } 21 | 22 | public String getHiMsg() { 23 | return hiMsg; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/maths/PowerOfTwoRecursion.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.maths; 2 | 3 | public class PowerOfTwoRecursion { 4 | /** 5 | * Test if a number is power of two or not using recursion. 6 | * 7 | * @param number the number to be checked. 8 | * @return {@code true} if given number is power of two, otherwise {@code false}. 9 | */ 10 | public static boolean isPowerOfTwo(int number) { 11 | if (number == 1) { 12 | return true; 13 | } 14 | if (number % 2 != 0 || number <= 0) { 15 | return false; 16 | } 17 | return isPowerOfTwo(number / 2); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /src/test/java/com/examplehub/maths/GreatestCommonDivisorTest.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.maths; 2 | 3 | import static org.junit.jupiter.api.Assertions.*; 4 | 5 | import org.junit.jupiter.api.Test; 6 | 7 | class GreatestCommonDivisorTest { 8 | @Test 9 | void testGCD() { 10 | int[][] testNumbers = {{8, 12, 4}, {54, 24, 6}, {3, 4, 1}, {3, 0, 3}}; 11 | for (int[] testNumber : testNumbers) { 12 | assertEquals(testNumber[2], GreatestCommonDivisor.gcdEasy(testNumber[0], testNumber[1])); 13 | assertEquals(testNumber[2], GreatestCommonDivisor.gcd(testNumber[0], testNumber[1])); 14 | } 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /src/test/java/com/examplehub/matrix/SortMatrixTest.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.matrix; 2 | 3 | import static org.junit.jupiter.api.Assertions.*; 4 | 5 | import org.junit.jupiter.api.Test; 6 | 7 | class SortMatrixTest { 8 | @Test 9 | void test() { 10 | int[][] matrix = { 11 | {1, 3, 5}, 12 | {2, 4, 6}, 13 | {7, 8, 9, 10} 14 | }; 15 | SortMatrix.sort(matrix); 16 | 17 | int value = 1; 18 | for (int[] ints : matrix) { 19 | for (int number : ints) { 20 | System.out.println(number); 21 | assertEquals(value++, number); 22 | } 23 | } 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/leetcode/easy/ThirdMaximumNumber.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.leetcode.easy; 2 | 3 | import java.util.Arrays; 4 | import java.util.HashSet; 5 | 6 | /** https://leetcode.com/problems/third-maximum-number/ */ 7 | public class ThirdMaximumNumber { 8 | public static int solution1(int... nums) { 9 | HashSet set = new HashSet<>(); 10 | for (int num : nums) { 11 | set.add(num); 12 | } 13 | int i = 0; 14 | for (int num : set) { 15 | nums[i++] = num; 16 | } 17 | Arrays.sort(nums, 0, i); 18 | return i < 3 ? nums[i - 1] : nums[i - 3]; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/maths/PalindromeNumberByString.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.maths; 2 | 3 | import com.examplehub.strings.PalindromeString; 4 | 5 | public class PalindromeNumberByString { 6 | 7 | /** 8 | * Test if a number is palindrome number by converting the number to string. 9 | * 10 | * @param number the number to be checked. 11 | * @return {@code true} if the given number is palindrome number, otherwise {@code false}. 12 | */ 13 | public static boolean isPalindrome(int number) { 14 | number = Math.abs(number); 15 | return PalindromeString.isPalindrome(number + ""); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/maths/PowerOfTwo.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.maths; 2 | 3 | public class PowerOfTwo { 4 | /** 5 | * Test if a number is power of two or not. 6 | * 7 | * @param number the number to be checked. 8 | * @return {@code true} if given number is power of two, otherwise {@code false}. 9 | */ 10 | public static boolean isPowerOfTwo(int number) { 11 | if (number <= 0) { 12 | return false; 13 | } 14 | while (number != 1) { 15 | if (number % 2 != 0) { 16 | return false; 17 | } 18 | number >>= 1; 19 | } 20 | return true; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /src/test/java/com/examplehub/leetcode/easy/SumOfUniqueElementsTest.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.leetcode.easy; 2 | 3 | import static org.junit.jupiter.api.Assertions.*; 4 | 5 | import org.junit.jupiter.api.Test; 6 | 7 | class SumOfUniqueElementsTest { 8 | @Test 9 | void testSolution1() { 10 | int[] nums = {1, 2, 3, 2}; 11 | assertEquals(4, SumOfUniqueElements.solution1(nums)); 12 | 13 | nums = new int[] {1, 1, 1, 1, 1}; 14 | assertEquals(0, SumOfUniqueElements.solution1(nums)); 15 | 16 | nums = new int[] {1, 2, 3, 4, 5}; 17 | assertEquals(15, SumOfUniqueElements.solution1(nums)); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /src/test/java/com/examplehub/maths/CountDigitsRecursionTest.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.maths; 2 | 3 | import static org.junit.jupiter.api.Assertions.*; 4 | 5 | import org.junit.jupiter.api.Test; 6 | 7 | class CountDigitsRecursionTest { 8 | 9 | @Test 10 | void testCountDigits() { 11 | assertEquals(3, CountDigitsRecursion.countDigits(-123)); 12 | assertEquals(1, CountDigitsRecursion.countDigits(-1)); 13 | assertEquals(1, CountDigitsRecursion.countDigits(0)); 14 | assertEquals(3, CountDigitsRecursion.countDigits(123)); 15 | assertEquals(6, CountDigitsRecursion.countDigits(123456)); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/leetcode/easy/ConvertBinaryNumberInALinkedListToInteger.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.leetcode.easy; 2 | 3 | import com.examplehub.leetcode.ListNode; 4 | 5 | /* 6 | * https://leetcode.com/problems/convert-binary-number-in-a-linked-list-to-integer/ 7 | */ 8 | public class ConvertBinaryNumberInALinkedListToInteger { 9 | 10 | public static int solution1(ListNode head) { 11 | StringBuilder builder = new StringBuilder(); 12 | while (head != null) { 13 | builder.append(head.val); 14 | head = head.next; 15 | } 16 | return Integer.parseInt(builder.toString(), 2); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/strings/PalindromeString.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.strings; 2 | 3 | public class PalindromeString { 4 | 5 | /** 6 | * Test if a string is palindrome string. 7 | * 8 | * @param s the string to be checked. 9 | * @return {@code true} if the string is palindrome string, otherwise {@code false}. 10 | */ 11 | public static boolean isPalindrome(String s) { 12 | char[] chars = s.toCharArray(); 13 | for (int i = 0, j = chars.length - 1; i < j; ++i, --j) { 14 | if (chars[i] != chars[j]) { 15 | return false; 16 | } 17 | } 18 | return true; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /src/test/java/com/examplehub/basics/encrypt/URLEncoderExampleTest.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.basics.encrypt; 2 | 3 | import static org.junit.jupiter.api.Assertions.*; 4 | 5 | import java.net.URLDecoder; 6 | import java.net.URLEncoder; 7 | import java.nio.charset.StandardCharsets; 8 | import org.junit.jupiter.api.Test; 9 | 10 | class URLEncoderExampleTest { 11 | @Test 12 | void testEncodeDecode() { 13 | String encoded = URLEncoder.encode("中 文", StandardCharsets.UTF_8); 14 | assertEquals("%E4%B8%AD+%E6%96%87", encoded); 15 | assertEquals("中 文", URLDecoder.decode(encoded, StandardCharsets.UTF_8)); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /src/test/java/com/examplehub/strings/PalindromeTest.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.strings; 2 | 3 | import static org.junit.jupiter.api.Assertions.*; 4 | 5 | import org.junit.jupiter.api.Test; 6 | 7 | class PalindromeTest { 8 | @Test 9 | void testPalindromeString() { 10 | String[] palindromeString = {"", "1", "a", "aa", "aba", "abc11cba", "amanaplanacanalpanama"}; 11 | for (String item : palindromeString) { 12 | assertTrue(PalindromeString.isPalindrome(item)); 13 | } 14 | 15 | assertFalse(PalindromeString.isPalindrome("abc")); 16 | assertFalse(PalindromeString.isPalindrome("root")); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /src/test/java/com/examplehub/utils/SortUtilsTest.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.utils; 2 | 3 | import static org.junit.jupiter.api.Assertions.assertTrue; 4 | 5 | import java.util.stream.IntStream; 6 | import org.junit.jupiter.api.Test; 7 | 8 | class SortUtilsTest { 9 | 10 | @Test 11 | void testIsSorted() { 12 | int[] ints = IntStream.range(-50, 50).toArray(); 13 | assertTrue(SortUtils.isSorted(ints)); 14 | } 15 | 16 | @Test 17 | void testIsSortedGeneric() { 18 | Integer[] integers = IntStream.range(-50, 50).boxed().toArray(Integer[]::new); 19 | assertTrue(SortUtils.isSorted(integers)); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/basics/GarbageCollectionExample.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.basics; 2 | 3 | public class GarbageCollectionExample { 4 | public static void main(String[] args) { 5 | Test test = new Test(); 6 | test = null; 7 | System.gc(); /* garbage collector is called. */ 8 | 9 | test = new Test(); 10 | test = null; 11 | Runtime.getRuntime().gc(); /* garbage collector is called. */ 12 | } 13 | } 14 | 15 | class Test { 16 | public Test() {} 17 | 18 | @Override 19 | protected void finalize() throws Throwable { 20 | System.out.println("garbage collector is called."); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /src/test/java/com/examplehub/basics/map/TreeMapExampleTest.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.basics.map; 2 | 3 | import static org.junit.jupiter.api.Assertions.*; 4 | 5 | import java.util.TreeMap; 6 | import org.junit.jupiter.api.Test; 7 | 8 | class TreeMapExampleTest { 9 | @Test 10 | void test() { 11 | TreeMap map = new TreeMap<>(); 12 | map.put("tencent", "wechat"); 13 | map.put("apple", "iPhone"); 14 | map.put("google", "pixel"); 15 | assertEquals("iPhone", map.get("apple")); 16 | assertEquals("pixel", map.get("google")); 17 | assertEquals("wechat", map.get("tencent")); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /src/test/java/com/examplehub/maths/DutchNationalFlagTest.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.maths; 2 | 3 | import static org.junit.jupiter.api.Assertions.*; 4 | 5 | import org.junit.jupiter.api.Test; 6 | 7 | class DutchNationalFlagTest { 8 | @Test 9 | void test() { 10 | int[] array = {1, 0, 2, 0, 1, 1, 2, 0}; 11 | DutchNationalFlag.sort(array); 12 | for (int i = 0; i < array.length; ++i) { 13 | if (i <= 2) { 14 | assertEquals(array[i], 0); 15 | } else if (i <= 5) { 16 | assertEquals(array[i], 1); 17 | } else { 18 | assertEquals(array[i], 2); 19 | } 20 | } 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /src/test/java/com/examplehub/maths/SubtractTest.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.maths; 2 | 3 | import static org.junit.jupiter.api.Assertions.*; 4 | 5 | import org.junit.jupiter.api.Test; 6 | 7 | class SubtractTest { 8 | 9 | @Test 10 | void testSubtract() { 11 | assertEquals(0, Subtract.subtract(-2, -2)); 12 | assertEquals(6, Subtract.subtract(4, -2)); 13 | assertEquals(10, Subtract.subtract(20, 10)); 14 | 15 | assertEquals(0.0, Subtract.subtract(2.0, 2.0)); 16 | assertTrue(Math.abs(Subtract.subtract(3.14, 3.0) - 0.14) <= 0.000001); 17 | assertEquals(50.25, Subtract.subtract(100.75, 50.50)); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /src/test/java/com/examplehub/basics/encrypt/HashAlgoExampleTest.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.basics.encrypt; 2 | 3 | import static org.junit.jupiter.api.Assertions.*; 4 | 5 | import org.junit.jupiter.api.Test; 6 | 7 | class HashAlgoExampleTest { 8 | @Test 9 | void testGetHash() { 10 | assertEquals(0x5e918d2, "hello".hashCode()); 11 | assertEquals(0x7a9d88e8, "hello, java".hashCode()); 12 | assertEquals(0xa0dbae2f, "hello, bob".hashCode()); 13 | } 14 | 15 | @Test 16 | void testCollision() { 17 | assertEquals(0x7460e8c0, "AaAaAa".hashCode()); 18 | assertEquals(0x7460e8c0, "BBAaBB".hashCode()); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /src/test/java/com/examplehub/basics/oop/VarargsExampleTest.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.basics.oop; 2 | 3 | import static org.junit.jupiter.api.Assertions.*; 4 | 5 | import java.util.Arrays; 6 | import org.junit.jupiter.api.Test; 7 | 8 | class VarargsExampleTest { 9 | @Test 10 | void test() { 11 | VarargsExample varargsExample = new VarargsExample(); 12 | varargsExample.setLanguage("Java", "Go", "Python"); 13 | assertEquals("[Java, Go, Python]", Arrays.toString(varargsExample.getLanguages())); 14 | assertEquals(6, varargsExample.sum(1, 2, 3)); 15 | assertEquals(15, varargsExample.sum(1, 2, 3, 4, 5)); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /src/test/java/com/examplehub/basics/oop/FunctionOverloadExampleTest.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.basics.oop; 2 | 3 | import static org.junit.jupiter.api.Assertions.*; 4 | 5 | import org.junit.jupiter.api.Test; 6 | 7 | class FunctionOverloadExampleTest { 8 | @Test 9 | void test() { 10 | FunctionOverloadExample functionOverloadExample = new FunctionOverloadExample(); 11 | assertEquals(3, functionOverloadExample.sum(1, 2)); 12 | assertEquals(6, functionOverloadExample.sum(1, 2, 3)); 13 | assertEquals(10, functionOverloadExample.sum(1, 2, 3, 4)); 14 | assertEquals(4.0, functionOverloadExample.sum(1.5, 2.5)); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /src/test/java/com/examplehub/maths/SquareNumberTest.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.maths; 2 | 3 | import static org.junit.jupiter.api.Assertions.*; 4 | 5 | import org.junit.jupiter.api.Test; 6 | 7 | class SquareNumberTest { 8 | @Test 9 | void testSquareNumber() { 10 | int[] squareNumbers = {0, 1, 4, 9, 16, 81, 100, 841, 1600}; 11 | for (int number : squareNumbers) { 12 | assertTrue(SquareNumber.isSquareNumber(number)); 13 | } 14 | 15 | int[] notSquareNumbers = {2, 3, 5, 6, 7, 8, 10, 99}; 16 | for (int number : notSquareNumbers) { 17 | assertFalse(SquareNumber.isSquareNumber(number)); 18 | } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/basics/math/MathExample.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.basics.math; 2 | 3 | public class MathExample { 4 | public static void main(String[] args) { 5 | System.out.println(Math.max(3, 4)); /* 4 */ 6 | System.out.println(Math.min(-3, -2)); /* -3 */ 7 | System.out.println(Math.sqrt(81)); /* 9.0 */ 8 | System.out.println(Math.sqrt(2)); /* 1.4142135623730951 */ 9 | System.out.println(Math.abs(-3)); /* 3 */ 10 | System.out.println(Math.random()); /* [0.0 - 1.0) */ 11 | 12 | int randomNumber = (int) (Math.random() * 100) + 1; 13 | System.out.println(randomNumber); /* [1, 100] */ 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/leetcode/easy/BalancedBinaryTree.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.leetcode.easy; 2 | 3 | import com.examplehub.leetcode.TreeNode; 4 | 5 | /** https://leetcode.com/problems/balanced-binary-tree/ */ 6 | public class BalancedBinaryTree { 7 | public static boolean solution1(TreeNode root) { 8 | if (root == null) { 9 | return true; 10 | } 11 | return Math.abs( 12 | MaximumDepthOfBinaryTree.solution1(root.left) 13 | - MaximumDepthOfBinaryTree.solution1(root.right)) 14 | <= 1 15 | && solution1(root.left) 16 | && solution1(root.right); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /src/test/java/com/examplehub/basics/oop/StaticFieldFunctionExampleTest.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.basics.oop; 2 | 3 | import static org.junit.jupiter.api.Assertions.*; 4 | 5 | import org.junit.jupiter.api.Test; 6 | 7 | class StaticFieldFunctionExampleTest { 8 | @Test 9 | void test() { 10 | var staticFieldFunctionExample = new StaticFieldFunctionExample(); 11 | staticFieldFunctionExample = new StaticFieldFunctionExample(); 12 | staticFieldFunctionExample = new StaticFieldFunctionExample(); 13 | assertEquals(3, StaticFieldFunctionExample.getCount()); 14 | assertEquals(1, staticFieldFunctionExample.getNum()); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /src/test/java/com/examplehub/leetcode/easy/SumOfAllOddLengthSubarraysTest.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.leetcode.easy; 2 | 3 | import static org.junit.jupiter.api.Assertions.*; 4 | 5 | import org.junit.jupiter.api.Test; 6 | 7 | class SumOfAllOddLengthSubarraysTest { 8 | @Test 9 | void testSolution1() { 10 | int[] arr = {1, 4, 2, 5, 3}; 11 | assertEquals(58, SumOfAllOddLengthSubarrays.solution1(arr)); 12 | 13 | arr = new int[] {1, 2}; 14 | assertEquals(3, SumOfAllOddLengthSubarrays.solution1(arr)); 15 | 16 | arr = new int[] {10, 11, 12}; 17 | assertEquals(66, SumOfAllOddLengthSubarrays.solution1(arr)); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /src/test/java/com/examplehub/maths/PerfectNumberTest.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.maths; 2 | 3 | import static org.junit.jupiter.api.Assertions.*; 4 | 5 | import org.junit.jupiter.api.Test; 6 | 7 | class PerfectNumberTest { 8 | 9 | @Test 10 | void testPerfectNumber() { 11 | int[] perfectNumbers = {6, 28, 496, 8128}; 12 | for (int number : perfectNumbers) { 13 | assertTrue(PerfectNumber.isPerfectNumber(number)); 14 | } 15 | 16 | int[] notPerfectNumbers = {0, 1, 2, 3, 4, 5, 7, 8, 8, 27}; 17 | for (int number : notPerfectNumbers) { 18 | assertFalse(PerfectNumber.isPerfectNumber(number)); 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/maths/CubeNumber.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.maths; 2 | 3 | /** https://en.wikipedia.org/wiki/Cube_(algebra) */ 4 | public class CubeNumber { 5 | 6 | /** 7 | * Test if a number is cube number or not. 8 | * 9 | * @param number the number to checked. 10 | * @return {@code true} if given {@code number} is cube number, otherwise {@code false}. 11 | */ 12 | public static boolean isCubeNumber(int number) { 13 | number = Math.abs(number); 14 | for (int i = 0; i <= number; ++i) { 15 | if (i * i * i == number) { 16 | return true; 17 | } 18 | } 19 | return false; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/maths/FibonacciRecursion.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.maths; 2 | 3 | /** https://en.wikipedia.org/wiki/Fibonacci_number */ 4 | public class FibonacciRecursion { 5 | 6 | /** 7 | * Get nth item of fibonacci sequence using recursion. 8 | * 9 | * @param n the nth. 10 | * @return the nth item of fibonacci sequence. 11 | * @throws IllegalArgumentException if {@code n} is negative. 12 | */ 13 | public static int fibonacci(int n) { 14 | if (n < 0) { 15 | throw new IllegalArgumentException("n < 0"); 16 | } 17 | return n == 0 ? 0 : n == 1 ? 1 : fibonacci(n - 1) + fibonacci(n - 2); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/maths/Hanoi.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.maths; 2 | 3 | public class Hanoi { 4 | 5 | /** 6 | * Hanoi tower algorithm. 7 | * 8 | * @param numberOfPlates the number of plates. 9 | * @param from origin 10 | * @param to destination 11 | * @param tempTower temp tower 12 | */ 13 | public static void move(int numberOfPlates, String from, String to, String tempTower) { 14 | if (numberOfPlates != 0) { 15 | move(numberOfPlates - 1, from, tempTower, to); 16 | System.out.printf("move from %s to %s%n", from, to); 17 | move(numberOfPlates - 1, tempTower, to, from); 18 | } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/maths/LucasNumbersIteration.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.maths; 2 | 3 | /** https://en.wikipedia.org/wiki/Lucas_number */ 4 | public class LucasNumbersIteration { 5 | 6 | /** 7 | * Get nth lucas number using iteration. 8 | * 9 | * @param n the nth. 10 | * @return nth lucas number. 11 | */ 12 | public static int lucas(int n) { 13 | if (n == 0) { 14 | return 2; 15 | } 16 | int first = 1; 17 | int second = 3; 18 | for (int i = 1; i < n; i++) { 19 | int temp = first + second; 20 | first = second; 21 | second = temp; 22 | } 23 | return first; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /src/test/java/com/examplehub/basics/generic/GenericClassTest.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.basics.generic; 2 | 3 | import static org.junit.jupiter.api.Assertions.*; 4 | 5 | import org.junit.jupiter.api.Test; 6 | 7 | class GenericClassTest { 8 | @Test 9 | void testWriteGenericClass() { 10 | class MyBean { 11 | T value; 12 | 13 | public T getValue() { 14 | return value; 15 | } 16 | 17 | public void setValue(T value) { 18 | this.value = value; 19 | } 20 | } 21 | MyBean bean1 = new MyBean<>(); 22 | bean1.setValue(123); 23 | assertEquals(123, bean1.getValue()); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/basics/oop/ReadJavaBeanProperties.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.basics.oop; 2 | 3 | public class ReadJavaBeanProperties {} 4 | 5 | class MyBean { 6 | private String name; 7 | private int age; 8 | 9 | public MyBean() {} 10 | 11 | public MyBean(String name, int age) { 12 | this.name = name; 13 | this.age = age; 14 | } 15 | 16 | public String getName() { 17 | return name; 18 | } 19 | 20 | public void setName(String name) { 21 | this.name = name; 22 | } 23 | 24 | public int getAge() { 25 | return age; 26 | } 27 | 28 | public void setAge(int age) { 29 | this.age = age; 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/leetcode/easy/SumOfUniqueElements.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.leetcode.easy; 2 | 3 | /** https://leetcode.com/problems/sum-of-unique-elements/ */ 4 | public class SumOfUniqueElements { 5 | public static int solution1(int[] nums) { 6 | int sum = 0; 7 | for (int i = 0; i < nums.length; ++i) { 8 | boolean isUnique = true; 9 | for (int j = 0; j < nums.length; ++j) { 10 | if (i != j && nums[i] == nums[j]) { 11 | isUnique = false; 12 | break; 13 | } 14 | } 15 | if (isUnique) { 16 | sum += nums[i]; 17 | } 18 | } 19 | return sum; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/maths/FindMax.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.maths; 2 | 3 | import java.util.Arrays; 4 | 5 | public class FindMax { 6 | 7 | /** 8 | * Find max value in array. 9 | * 10 | * @param numbers the numbers to be find. 11 | * @return max value of numbers. 12 | */ 13 | public static int findMax(int... numbers) { 14 | int max = numbers[0]; 15 | for (int number : numbers) { 16 | if (number > max) { 17 | max = number; 18 | } 19 | } 20 | return max; 21 | } 22 | 23 | public static int findMaxSecond(int... numbers) { 24 | return Arrays.stream(numbers).max().getAsInt(); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/maths/FindMin.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.maths; 2 | 3 | import java.util.Arrays; 4 | 5 | public class FindMin { 6 | 7 | /** 8 | * Find min value in array. 9 | * 10 | * @param numbers the numbers to be find. 11 | * @return min value of numbers. 12 | */ 13 | public static int findMin(int[] numbers) { 14 | int min = numbers[0]; 15 | for (int number : numbers) { 16 | if (number < min) { 17 | min = number; 18 | } 19 | } 20 | return min; 21 | } 22 | 23 | public static int findMinSecond(int[] numbers) { 24 | return Arrays.stream(numbers).min().getAsInt(); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/test/java/com/examplehub/basics/system/FileSeparatorTestTest.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.basics.system; 2 | 3 | import static org.junit.jupiter.api.Assertions.*; 4 | 5 | import java.io.File; 6 | import org.junit.jupiter.api.Test; 7 | import org.junit.jupiter.api.condition.EnabledOnOs; 8 | import org.junit.jupiter.api.condition.OS; 9 | 10 | class FileSeparatorTestTest { 11 | @Test 12 | @EnabledOnOs({OS.LINUX, OS.MAC, OS.MAC}) 13 | void testOnUnixLike() { 14 | assertEquals("/", File.separator); 15 | } 16 | 17 | @Test 18 | @EnabledOnOs(OS.WINDOWS) 19 | void testOnWindows() { 20 | assertEquals("\\", File.separator); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /src/test/java/com/examplehub/maths/PowerRecursionTest.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.maths; 2 | 3 | import static org.junit.jupiter.api.Assertions.assertEquals; 4 | 5 | import org.junit.jupiter.api.Test; 6 | 7 | class PowerRecursionTest { 8 | 9 | @Test 10 | void testPow() { 11 | assertEquals(1, PowerRecursion.pow(0, 0)); 12 | assertEquals(0, PowerRecursion.pow(0, 10)); 13 | assertEquals(1, PowerRecursion.pow(1, 0)); 14 | assertEquals(2, PowerRecursion.pow(2, 1)); 15 | assertEquals(8, PowerRecursion.pow(2, 3)); 16 | assertEquals(1024, PowerRecursion.pow(2, 10)); 17 | assertEquals(625, PowerRecursion.pow(25, 2)); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/basics/Comments.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.basics; 2 | 3 | public class Comments { 4 | 5 | /** 6 | * Driver Code 7 | * 8 | * @param args the arguments passed to main function 9 | */ 10 | public static void main(String[] args) { 11 | System.out.println( 12 | "This is a single comment"); // Single-line comments start with two forward slashes (//). 13 | System.out.println("This is a another single comment"); /* single or multi line */ 14 | 15 | /* 16 | *This is a multi-line comments. 17 | * This will print ExampleHub. 18 | */ 19 | System.out.println("ExampleHub"); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/basics/thread/CopyOnWriteArrayListExample.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.basics.thread; 2 | 3 | import java.util.concurrent.CopyOnWriteArrayList; 4 | 5 | public class CopyOnWriteArrayListExample { 6 | public static void main(String[] args) { 7 | CopyOnWriteArrayList integers = new CopyOnWriteArrayList<>(); 8 | for (int i = 0; i < 10; i++) { 9 | int finalI = i; 10 | new Thread(() -> integers.add(finalI)).start(); 11 | } 12 | try { 13 | Thread.sleep(1000); 14 | } catch (InterruptedException e) { 15 | e.printStackTrace(); 16 | } 17 | System.out.println(integers); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/projecteuler/Problem02.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.projecteuler; 2 | 3 | import com.examplehub.maths.EvenCheck; 4 | 5 | /** https://projecteuler.net/problem=2 */ 6 | public class Problem02 { 7 | 8 | public static int solution1(int n) { 9 | int sum = 0; 10 | int first = 1; 11 | int second = 2; 12 | while (first <= n) { 13 | if (EvenCheck.isEvenFaster(first)) { 14 | sum += first; 15 | } 16 | System.out.println("first = " + first + " second = " + second); 17 | int temp = first + second; 18 | first = second; 19 | second = temp; 20 | } 21 | return sum; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /src/test/java/com/examplehub/basics/exception/IndexOutOfBoundsExceptionExampleTest.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.basics.exception; 2 | 3 | import static org.junit.jupiter.api.Assertions.*; 4 | 5 | import org.junit.jupiter.api.Test; 6 | 7 | class IndexOutOfBoundsExceptionExampleTest { 8 | @Test 9 | void testArrayAccess() { 10 | int[] nums = {1, 2, 3, 4, 5}; 11 | try { 12 | int sum = 0; 13 | for (int i = 0; i <= nums.length; ++i) { 14 | sum += nums[i]; 15 | } 16 | System.out.println("sum = " + sum); 17 | fail(); 18 | } catch (IndexOutOfBoundsException e) { 19 | assertTrue(true); 20 | } 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /.github/workflows/gradle.yml: -------------------------------------------------------------------------------- 1 | # This workflow will build a Java project with Gradle 2 | # For more information see: https://help.github.com/actions/language-and-framework-guides/building-and-testing-java-with-gradle 3 | 4 | name: Java CI with Gradle 5 | 6 | on: [push, pull_request] 7 | 8 | jobs: 9 | build: 10 | 11 | runs-on: ubuntu-latest 12 | 13 | steps: 14 | - uses: actions/checkout@v2 15 | - name: Set up JDK 12 16 | uses: actions/setup-java@v1 17 | with: 18 | java-version: 12 19 | - name: Grant execute permission for gradlew 20 | run: chmod +x gradlew 21 | - name: Build with Gradle 22 | run: ./gradlew build 23 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/basics/conversion/TypeCast.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.basics.conversion; 2 | 3 | public class TypeCast { 4 | public static void main(String[] args) { 5 | 6 | int number = 'a'; 7 | /* 8 | * 97 9 | */ 10 | System.out.println(number); 11 | 12 | int digit = '8' - '0'; 13 | /* 14 | * 8 15 | */ 16 | System.out.println(digit); 17 | 18 | double price = 3; 19 | 20 | /* 21 | * 8 22 | */ 23 | int sum = (int) (3.5 + 4.6); 24 | System.out.println(sum); 25 | 26 | char letter = 65; 27 | /* 28 | * A 29 | */ 30 | System.out.println(letter); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/leetcode/easy/FindLuckyIntegerInAnArray.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.leetcode.easy; 2 | 3 | /** https://leetcode.com/problems/find-lucky-integer-in-an-array/ */ 4 | public class FindLuckyIntegerInAnArray { 5 | 6 | public static int solution1(int[] arr) { 7 | int maxLuckyNum = -1; 8 | for (int i = 0; i < arr.length; ++i) { 9 | int count = 0; 10 | for (int j = 0; j < arr.length; ++j) { 11 | if (arr[i] == arr[j]) { 12 | count++; 13 | } 14 | } 15 | if (count > 1 && count == arr[i]) { 16 | maxLuckyNum = arr[i]; 17 | } 18 | } 19 | return maxLuckyNum; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /src/test/java/com/examplehub/basics/io/StringReaderExampleTest.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.basics.io; 2 | 3 | import static org.junit.jupiter.api.Assertions.*; 4 | 5 | import com.examplehub.utils.StringUtils; 6 | import java.io.IOException; 7 | import java.io.Reader; 8 | import java.io.StringReader; 9 | import org.junit.jupiter.api.Test; 10 | 11 | class StringReaderExampleTest { 12 | @Test 13 | void test() throws IOException { 14 | try (Reader reader = new StringReader("hello")) { 15 | char[] chars = new char[1024]; 16 | int len = reader.read(chars); 17 | assertEquals("hello", StringUtils.toString(chars, 0, len)); 18 | } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /src/test/java/com/examplehub/matrix/LowerTriangleMatrixTest.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.matrix; 2 | 3 | import static org.junit.jupiter.api.Assertions.*; 4 | 5 | import org.junit.jupiter.api.Test; 6 | 7 | class LowerTriangleMatrixTest { 8 | @Test 9 | void test() { 10 | int[][] matrix = { 11 | {1, 0, 0}, 12 | {2, 8, 0}, 13 | {4, 9, 7} 14 | }; 15 | 16 | int[] dest = LowerTriangleMatrix.storeTriangleMatrix(matrix); 17 | for (int i = 0; i < matrix.length; i++) { 18 | for (int j = 0; j < matrix[i].length; j++) { 19 | assertEquals(matrix[i][j], LowerTriangleMatrix.get(dest, i, j)); 20 | } 21 | } 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /src/test/java/com/examplehub/strings/PalindromeStringByReverseTest.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.strings; 2 | 3 | import static org.junit.jupiter.api.Assertions.*; 4 | 5 | import org.junit.jupiter.api.Test; 6 | 7 | class PalindromeStringByReverseTest { 8 | @Test 9 | void test() { 10 | String[] palindromeString = {"", "1", "a", "aa", "aba", "abc11cba", "amanaplanacanalpanama"}; 11 | for (String item : palindromeString) { 12 | assertTrue(PalindromeStringByReverse.isPalindrome(item)); 13 | } 14 | 15 | assertFalse(PalindromeStringByReverse.isPalindrome("abc")); 16 | assertFalse(PalindromeStringByReverse.isPalindrome("root")); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /src/test/java/com/examplehub/strings/RemoveWhiteSpaceTest.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.strings; 2 | 3 | import static org.junit.jupiter.api.Assertions.*; 4 | 5 | import org.junit.jupiter.api.Test; 6 | 7 | class RemoveWhiteSpaceTest { 8 | @Test 9 | void testRemoveWhitespace() { 10 | assertEquals("ILoveJava", RemoveWhiteSpace.removeWhitespace("I Love Java")); 11 | assertEquals("ILoveJava", RemoveWhiteSpace.removeWhitespace("I Love Java")); 12 | assertEquals( 13 | "ILoveJava", RemoveWhiteSpace.removeWhitespace(" I L ov e Ja va")); 14 | assertEquals("", RemoveWhiteSpace.removeWhitespace(" ")); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/basics/StaticExample.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.basics; 2 | 3 | public class StaticExample { 4 | public static void main(String[] args) { 5 | StaticTest staticTest = new StaticTest(); 6 | StaticTest.increment(); 7 | System.out.println(StaticTest.count); /* 1 */ 8 | staticTest.method(); /* normal method */ 9 | System.out.println(StaticTest.count); /* 2 */ 10 | } 11 | } 12 | 13 | class StaticTest { 14 | public static int count = 0; 15 | 16 | public static void increment() { 17 | count++; 18 | } 19 | 20 | public void method() { 21 | increment(); 22 | System.out.println("normal method"); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/maths/PalindromeNumber.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.maths; 2 | 3 | public class PalindromeNumber { 4 | 5 | /** 6 | * Test if a number is palindrome number. 7 | * 8 | * @param number the number to be checked. 9 | * @return {@code true} if the given number is palindrome number, otherwise {@code false}. 10 | */ 11 | public static boolean isPalindrome(int number) { 12 | int origin = number = Math.abs(number); 13 | int reversedNumber = 0; 14 | while (number != 0) { 15 | reversedNumber = reversedNumber * 10 + number % 10; 16 | number /= 10; 17 | } 18 | return reversedNumber == origin; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/strings/PalindromeStringRecursion.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.strings; 2 | 3 | public class PalindromeStringRecursion { 4 | 5 | /** 6 | * Test if a string is palindrome string or not using recursion. 7 | * 8 | * @param s the string to be checked. 9 | * @return {@code true} if the string is palindrome string, otherwise {@code false}. 10 | */ 11 | public static boolean isPalindrome(String s) { 12 | if (s.length() <= 1) { 13 | return true; 14 | } 15 | if (s.charAt(0) != s.charAt(s.length() - 1)) { 16 | return false; 17 | } 18 | return isPalindrome(s.substring(1, s.length() - 1)); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /src/test/java/com/examplehub/basics/time/DateTimeFormatExampleTest.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.basics.time; 2 | 3 | import static org.junit.jupiter.api.Assertions.*; 4 | 5 | import java.time.LocalDateTime; 6 | import java.time.Month; 7 | import java.time.format.DateTimeFormatter; 8 | import org.junit.jupiter.api.Test; 9 | 10 | class DateTimeFormatExampleTest { 11 | @Test 12 | void test() { 13 | LocalDateTime localDateTime = LocalDateTime.of(2099, Month.DECEMBER, 30, 23, 59, 59); 14 | DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); 15 | assertEquals("2099-12-30 23:59:59", formatter.format(localDateTime)); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /src/test/java/com/examplehub/maths/AllocationTest.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.maths; 2 | 3 | import static org.junit.jupiter.api.Assertions.assertEquals; 4 | 5 | import org.junit.jupiter.api.Test; 6 | 7 | class AllocationTest { 8 | 9 | @Test 10 | void testAllocation() { 11 | assertEquals( 12 | "[1-4161, 4162-8322, 8323-12483, 12484-16647]", Allocation.allocation(16647, 4).toString()); 13 | 14 | assertEquals( 15 | "[1-16666, 16667-33332, 33333-49998, 49999-66664, 66665-83330, 83331-100000]", 16 | Allocation.allocation(100000, 6).toString()); 17 | 18 | assertEquals("[1-10]", Allocation.allocation(10, 1).toString()); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /src/test/java/com/examplehub/maths/MiddleIndexCalculateTest.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.maths; 2 | 3 | import static org.junit.jupiter.api.Assertions.*; 4 | 5 | import org.junit.jupiter.api.Test; 6 | 7 | class MiddleIndexCalculateTest { 8 | @Test 9 | void testMiddle() { 10 | assertEquals(0, MiddleIndexCalculate.middle(0, 1)); 11 | assertEquals(5, MiddleIndexCalculate.middle(0, 10)); 12 | assertEquals( 13 | Integer.MAX_VALUE - 1, 14 | MiddleIndexCalculate.middle(Integer.MAX_VALUE - 1, Integer.MAX_VALUE)); 15 | assertEquals( 16 | Integer.MAX_VALUE, MiddleIndexCalculate.middle(Integer.MAX_VALUE, Integer.MAX_VALUE)); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /src/test/java/com/examplehub/strings/PalindromeStringRecursionTest.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.strings; 2 | 3 | import static org.junit.jupiter.api.Assertions.*; 4 | 5 | import org.junit.jupiter.api.Test; 6 | 7 | class PalindromeStringRecursionTest { 8 | 9 | @Test 10 | void test() { 11 | String[] palindromeString = {"", "1", "a", "aa", "aba", "abc11cba", "amanaplanacanalpanama"}; 12 | for (String item : palindromeString) { 13 | assertTrue(PalindromeStringRecursion.isPalindrome(item)); 14 | } 15 | 16 | assertFalse(PalindromeStringRecursion.isPalindrome("abc")); 17 | assertFalse(PalindromeStringRecursion.isPalindrome("root")); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/maths/Allocation.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.maths; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | public class Allocation { 7 | public static List allocation(int numberOfBytes, int partitions) { 8 | 9 | List allocations = new ArrayList<>(); 10 | int bytesPerPartition = numberOfBytes / partitions; 11 | for (int i = 0; i < partitions; ++i) { 12 | int startBytes = i * bytesPerPartition + 1; 13 | int endBytes = i == partitions - 1 ? numberOfBytes : (i + 1) * bytesPerPartition; 14 | allocations.add(startBytes + "-" + endBytes); 15 | } 16 | return allocations; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/maths/Fibonacci.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.maths; 2 | 3 | /** https://en.wikipedia.org/wiki/Fibonacci_number */ 4 | public class Fibonacci { 5 | 6 | /** 7 | * Get nth item of fibonacci sequence. 8 | * 9 | * @param n the nth 10 | * @return the nth item of fibonacci sequence. 11 | */ 12 | public static int fibonacci(int n) { 13 | if (n < 0) { 14 | throw new IllegalArgumentException("n < 0"); 15 | } 16 | int first = 0; 17 | int second = 1; 18 | for (int i = 1; i <= n; ++i) { 19 | int c = first + second; 20 | first = second; 21 | second = c; 22 | } 23 | return first; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/projecteuler/Problem12.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.projecteuler; 2 | 3 | import com.examplehub.maths.SumToN; 4 | 5 | /** https://projecteuler.net/problem=12 */ 6 | public class Problem12 { 7 | 8 | public static int solution1(int n) { 9 | for (int i = 1; ; i++) { 10 | int numberOfDivisors = 0; 11 | int triangleNumber = SumToN.sum(i); 12 | for (int divisor = 1; divisor <= triangleNumber; divisor++) { 13 | if (triangleNumber % divisor == 0) { 14 | numberOfDivisors++; 15 | } 16 | } 17 | if (numberOfDivisors > n) { 18 | return triangleNumber; 19 | } 20 | } 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /src/test/java/com/examplehub/maths/PalindromeNumberTest.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.maths; 2 | 3 | import static org.junit.jupiter.api.Assertions.*; 4 | 5 | import org.junit.jupiter.api.Test; 6 | 7 | class PalindromeNumberTest { 8 | @Test 9 | void testPalindromeNumber() { 10 | int[] palindromeNumbers = {-12321, -11, -1, 0, 1, 11, 12321, 1111111, 123454321}; 11 | for (int number : palindromeNumbers) { 12 | assertTrue(PalindromeNumber.isPalindrome(number)); 13 | } 14 | 15 | assertFalse(PalindromeNumber.isPalindrome(-12)); 16 | assertFalse(PalindromeNumber.isPalindrome(10)); 17 | assertFalse(PalindromeNumber.isPalindrome(123)); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/datastructures/stack/ExpressionEvaluation.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.datastructures.stack; 2 | 3 | import com.examplehub.strings.RemoveWhiteSpace; 4 | 5 | public class ExpressionEvaluation { 6 | 7 | /** 8 | * Expression evaluation algorithms. 9 | * 10 | * @param expression the expression to be calculated. 11 | * @return result of expression. 12 | * @throws Exception if {@code expression} is invalid. 13 | */ 14 | public static int evaluate(String expression) throws Exception { 15 | 16 | return PostfixEvaluation.evaluate( 17 | Infix2Postfix.infix2PostFix(RemoveWhiteSpace.removeWhitespace(expression))); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/dynamicprogramming/FactorialDP.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.dynamicprogramming; 2 | 3 | public class FactorialDP { 4 | 5 | /** 6 | * Calculate factorial using dynamic programming. 7 | * 8 | * @param n the number. 9 | * @return the factorial of a number. 10 | * @throws ArithmeticException if {@value n} is negative number. 11 | */ 12 | public static long factorial(int n) { 13 | if (n < 0) { 14 | throw new ArithmeticException(); 15 | } 16 | long[] fact = new long[n + 1]; 17 | fact[0] = 1; 18 | for (int i = 1; i <= n; i++) { 19 | fact[i] = fact[i - 1] * i; 20 | } 21 | return fact[n]; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /src/main/java/com/examplehub/maths/Subtract.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.maths; 2 | 3 | public class Subtract { 4 | 5 | /** 6 | * Subtract two integer numbers. 7 | * 8 | * @param a the first number. 9 | * @param b the second number. 10 | * @return sub value of two integer numbers. 11 | */ 12 | public static int subtract(int a, int b) { 13 | return a - b; 14 | } 15 | 16 | /** 17 | * Subtract two double numbers. 18 | * 19 | * @param a the first number. 20 | * @param b the second number. 21 | * @return sub value of two double numbers. 22 | */ 23 | public static double subtract(double a, double b) { 24 | return a - b; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/test/java/com/examplehub/basics/exception/ClassCastExceptionExampleTest.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.basics.exception; 2 | 3 | import static org.junit.jupiter.api.Assertions.*; 4 | 5 | import java.util.ArrayList; 6 | import java.util.Arrays; 7 | import org.junit.jupiter.api.Test; 8 | 9 | class ClassCastExceptionExampleTest { 10 | @Test 11 | void testClassCast() { 12 | try { 13 | String[] strArray = new String[] {"John", "Snow"}; 14 | ArrayList strList = (ArrayList) Arrays.asList(strArray); 15 | System.out.println("String list: " + strList); 16 | } catch (ClassCastException e) { 17 | assertTrue(true); 18 | } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /src/test/java/com/examplehub/dynamicprogramming/FibonacciDPTest.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.dynamicprogramming; 2 | 3 | import static org.junit.jupiter.api.Assertions.*; 4 | 5 | import org.junit.jupiter.api.Test; 6 | 7 | class FibonacciDPTest { 8 | @Test 9 | void testFibonacci() { 10 | assertEquals(0, FibonacciDP.fibonacci(0)); 11 | assertEquals(1, FibonacciDP.fibonacci(1)); 12 | assertEquals(1, FibonacciDP.fibonacci(2)); 13 | assertEquals(34, FibonacciDP.fibonacci(9)); 14 | 15 | try { 16 | long result = FibonacciDP.fibonacci(-1); 17 | fail(); 18 | } catch (IllegalArgumentException e) { 19 | assertTrue(true); 20 | } 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /src/test/java/com/examplehub/leetcode/easy/ImplementQueueUsingStacksTest.java: -------------------------------------------------------------------------------- 1 | package com.examplehub.leetcode.easy; 2 | 3 | import static org.junit.jupiter.api.Assertions.*; 4 | 5 | import org.junit.jupiter.api.Test; 6 | 7 | class ImplementQueueUsingStacksTest { 8 | @Test 9 | void test() { 10 | ImplementQueueUsingStacks queue = new ImplementQueueUsingStacks(); 11 | queue.push(1); 12 | queue.push(2); 13 | queue.push(3); 14 | assertEquals(1, queue.peek()); 15 | assertEquals(1, queue.pop()); 16 | assertEquals(2, queue.pop()); 17 | assertEquals(3, queue.pop()); 18 | assertEquals(-1, queue.pop()); 19 | assertTrue(queue.empty()); 20 | } 21 | } 22 | --------------------------------------------------------------------------------