Why do we need Generic Types in Java?

Generic types are extensively used in Java collections. Why do we need Generic types in Java? Understanding this question can help us better understand a lot of related concepts. In this article, I will use a very short example to illustrate why Generic is useful. 1. Overview of Generics The goal of implementing Generics is … Read more

Python “Hello World” web application in Ubuntu

This post shows how to setup a python web development environment under Ubuntu and make a hello world example. So Python has become a hot word in recent years. But before you can do anything about it, you need a “hello world” sample application. In this post, I will build a hello world Python web … Read more

Github: add local directory/files to remote repository of Github

Github is a more popular and social code repository. It is the trend to be a user, at least it is true for now. There are a lot of problems we may meet, most of them can be solved by reading the documentation. But that is not fast or something we like to do. Problem … Read more

LeetCode – Repeated String Match (Java)

Given two strings A and B, find the minimum number of times A has to be repeated such that B is a substring of it. If no such solution, return -1. For example, with A = “abcd” and B = “cdabcdab”. Return 3, because by repeating A three times (“abcdabcdabcd”), B is a substring of … Read more

LeetCode – Permutation in String (Java)

Given two strings s1 and s2, write a function to return true if s2 contains the permutation of s1. In other words, one of the first string’s permutations is the substring of the second string. For example: Input: s1 = “ab” s2 = “eidbaooo” Output: True Explanation: s2 contains one permutation of s1 (“ba”). Java … Read more

Evaluate math expression with plus, minus and parentheses (java)

Given a string of math expression, such as 1-(2+3), evaluate the value. The expression contains only digits, +, – and parentheses. Java Solution import java.util.ArrayList; import java.util.Stack;   public class ExpressionEvaluator { static class Node { Boolean isPositive; Integer value; ArrayList<Node> list;   public Node(boolean isPositive, Integer value) { this.isPositive = isPositive; this.value = value; … Read more

LeetCode – Backspace String Compare (Java)

Given two strings S and T, return if they are equal when both are typed into empty text editors. # means a backspace character. Example 1: Input: S = “ab#c”, T = “ad#c” Output: true Explanation: Both S and T become “ac”. Example 2: Input: S = “a##c”, T = “#a#c” Output: true Explanation: Both … Read more

JUnit Tutorial (2) – Annotations

This article contains JUnit annotations and examples for showing how to use them. Annotations enables us to use any method names without following any conventions. Actually, it has becomes a very popular way for a lot of projects, framework, etc. You may ask questions like: How to skip a test case in Junit? How to … Read more

Java Calculate Square Root Without Using Library Method

If we want to calculate square root, we can use Math.sqrt() method. It is simple. Now do you know how to write such a method by yourself? Here is the equation you need. The first sqrt number should be the input number / 2. Using the equation, we can come up with a Java Square … Read more

JUnit Tutorial (1) – Use JUnit under Eclipse

Introduction and Installation JUnit is the defacto standard for unit testing. JUnit is part of Eclipse Java Development Tools (JDT). So, we can either install JDT via Software Updates site, or download and install Eclipse IDE for Java Developers. Using JUnit in Eclipse Environment 1. Create a project and create a class. This should contains the method you want … Read more

Why do we need software testing?

Why do we need to test our program? When people talk about the importance of software testing, common examples they give frequently are military software, aircraft, etc. That is not concrete enough to understand why we need to test our software. Here I provide an example from daily programming work. You don’t need to work … Read more

Java Method to Shuffle an Array

How to shuffle an array in Java? There are two approaches to shuffle an int array(randomizes the order of the elements in an array), one is to use the Collections.shuffle() method, the other is to manipulate array elements. Approach 1: Shuffle elements in an array This is flexible, and easy to be changed to fit … Read more