Skip to content

Instantly share code, notes, and snippets.

View jsbonso's full-sized avatar
🎯
portal.tutorialsdojo.com

Jon Bonso jsbonso

🎯
portal.tutorialsdojo.com
View GitHub Profile
@jsbonso
jsbonso / PalindromeExample.java
Last active October 15, 2017 01:14
[Java] Palindrome Examples
/**
* 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());
}
@jsbonso
jsbonso / Java Reverse a String.java
Created October 14, 2017 05:01
[Java] Reverse a String
/**
* 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();
@jsbonso
jsbonso / Java Anagram Check.java
Created October 14, 2017 08:20
Java - Check if 2 strings are an Anagram of each other
/**
* 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.
@jsbonso
jsbonso / Simple Recursive Method.java
Created October 14, 2017 08:31
Simple Recursive Method
/**
* 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;
@jsbonso
jsbonso / Java Fibonacci Sequence.java
Last active December 16, 2017 17:20
Java Fibonacci Sequence
/**
* 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
@jsbonso
jsbonso / Reverse a List.java
Last active December 16, 2017 17:18
Reverse a List in Java
/**
* Reverses a given List
* using Collections.reverse() method
* @param list
* @author Jon Bonso
*/
public static void reverse(List<?> list) {
Collections.reverse(list);
}
@jsbonso
jsbonso / Java Number Primality Check.java
Last active October 14, 2017 22:56
Java: Check if Number is a Prime Number
/**
* 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.
*
@jsbonso
jsbonso / java blackjack implementation.java
Last active October 14, 2017 23:27
Java Blackjack Checker
/**
* 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) {
@jsbonso
jsbonso / Java ForEach Lambda Example.java
Created October 15, 2017 00:01
Java ForEach Lambda Example
/**
* 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};
@jsbonso
jsbonso / Java Stream Filter Example.java
Created October 15, 2017 01:04
Java Stream Filter Examples
/**
* 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