This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Check if the given input is a Palindrome | |
* using a StringBuilder. | |
* @author Jon Bonso | |
* @param input | |
* @return boolean | |
*/ | |
private static boolean isPalindrome(String input) { | |
return input.equals(new StringBuilder(input).reverse().toString()); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Reverses a string using StringBuilder. | |
* You can replace it with a StringBuffer as well, | |
* but in this case, you don't need it to be thread-safe, | |
* hence, a StringBuilder will do. | |
* @param input | |
* @return | |
*/ | |
static String reverseString(String input) { | |
return (new StringBuilder(input)).reverse().toString(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Check if the two strings are an anagram of each other. | |
* For example: LISTEN is an Anagram of SILENT. | |
* | |
* Two strings are an anagram of each other if the characters | |
* and the numbers of characters that are composing them, | |
* are exactly the same. So for example, SUE and USE are anagrams | |
* because both words have one E, one S and one U, | |
* hence: ESU = ESU. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* A simple recursive method that outputs numbers from 1 - 10 | |
* @param number of times the method will recurse / repeat | |
* @return | |
*/ | |
static int recursiveMethod(int num) { | |
// An important line, which defines the case when will the recursion ends | |
if (num == 0) | |
return num; | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Calculates the next fibonacci sequence. | |
* | |
* To properly implement a Fibonacci sequence, | |
* we need to know the Mathematical formula first, and that is: | |
* | |
* F<sub>n</sub> = F<sub>n-1</sub> + F<sub>n-2</sub> | |
* | |
* The above formula basically reads as this: | |
* The next sequence (Fn) is the sum of the |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Reverses a given List | |
* using Collections.reverse() method | |
* @param list | |
* @author Jon Bonso | |
*/ | |
public static void reverse(List<?> list) { | |
Collections.reverse(list); | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Checks the primality of the given number. | |
* Prime numbers can be used in password encryptions | |
* and cryptography. | |
* | |
* Basically, a prime number is: | |
* 1. A whole number which is greater than 1 | |
* 2. And can only be divided evenly by 1 | |
* and the number itself. | |
* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Simple blackjack method that: | |
* | |
* 1. Returns the input which is the nearest to number 21 | |
* 2. Returns 0 if both numbers are over 21 | |
* @author Jon Bonso | |
* @param a | |
* @param b | |
*/ | |
static int blackjack(int a, int b) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Example of Java's Stream API - ForEach | |
* @author jonbonso | |
* @param args | |
*/ | |
public static void main(String... args) { | |
System.out.println("Iterate using the traditional for loop..."); | |
int[] numArray = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* filter() example of Java's Stream API | |
* | |
* | |
* How to get certain items from a collection using the filter() | |
* intermediate operation, and then show the result using the | |
* collect() terminal operation. | |
* | |
* @author jonbonso | |
* @param args |
OlderNewer