Category Archives: Development

Simple SSL configuration for Apache-Tomcat

For the official instructions (Tomcat 8), read this page

The following instructions are for development servers only:

Step 1: Create a keystore file to store the server’s private key and self-signed certificate:

For Windows:

"%JAVA_HOME%\bin\keytool" -genkey -alias tomcat -keyalg RSA

For Unix/Linux:

$JAVA_HOME/bin/keytool -genkey -alias tomcat -keyalg RSA

You will be asked for various information. Remember the password you typed at this step.

At the end of the procedure, keytool will create a file named “.keystore” to your home directory. By default it is hidden. This file should be moved to Apache-Tomcat home directory and can be renamed (eg to “keystore”).

Step 2: Disable APR:

Edit server.xml and comment out the following line:


Step 3: Activate the SSL connector

Edit server.xml and add (or uncomment and modify) the following lines:

    sslProtocol="TLS" />

You will have you replace “12345678” with your password.

You will also have to replace “keystore” with the full path to the keystore file generated at Step 1.

Step 4: Restart Tomcat

Do not forget to restart Apache-Tomcat



Generate random weighted variables

In this post I describe the common problem how to generate random weighted variables. For example you have a die with 6 faces that is fixed, so the probability of an 1 is not the same with the probability of a 2 etc.

The table below shows an example:

Face Probability
1  25
2  25
3  20
4  20
5  5
6  5

One easy solution is to reduce the problem to another one with a die with 100 faces that is not fixed.

You generate a random value from 0-99 that follows the uniform distribution. Do not use the modulo function to do that. See this post instead.

Once you have that value you perform the following steps:

You check the space within which that value relies.

  1. between 0 and 24, you generate 1 as the weighted variable
  2. between 25 and 49, you generate 2 as the weighted variable
  3. between 50 and 69, you generate 3 as the weighted variable
  4. between 70 and 89, you generate 4 as the weighted variable
  5. between 90 and 94, you generate 5 as the weighted variable
  6. between 95 and 99, you generate 6 as the weighted variable




Generate uniformly distributed random variables

Formulas shown below generate values that follow uniform distribution.

Using simple modulo methods is not considered as a good solution (although used a lot by students) as the module function does not generate all the variables with the same frequency.

Random integer between [ 0, 1 ]

int r = (int) (rand()/(RAND_MAX + 0.0));

Random integer between [ 0, 1 )

int r = (int) (rand()/(RAND_MAX + 1.0));

Random integer between [ 0, N ]

int r = (int) (N * (rand() / (RAND_MAX + 0.0)));

Random integer between [ 0, N )

int r = (int) (N * (rand()/(RAND_MAX + 1.0)));

Random integer between [ M, N ]

int r = (int) (Μ + (rand()/(RAND_MAX + 0.0))*(N-M+1));

Random integer between [ M, N )

int r = (int) (Μ + (rand()/(RAND_MAX + 1.0))*(N-M+1));


The general formula is:

int r = (int) (A + (rand()/(RAND_MAX + C))*B);

Values of M, N, K are shown in the following table:

Range A B  C
 [ 0, 1 ]  0  1  0.0
 [ 0, N ]  0  N  0.0
 [ M, N ]  M  N – M + 1  0.0
 [ 0, 1 )  0  1  1.0
 [ 0, N )  0  N  1.0
 [ M, N )  M  N – M + 1  1.0

Generating values of normal distribution – Marsaglia method

Assuming you can already produce variables of uniform distribution, you can produce variables of normal distribution using various formulas. Two of the most important are:

  1. Box–Muller method
  2. Marsaglia polar method

Most methods are based on Box-Muller method.

The Marsaglia method is my favorite since it does not require using sin() or cos() functions and the steps are very easily to implement.

Here is a sequence of the steps:

  1. Generate a value that follows uniform distribution in any space you want (eg: [ 0 , 1 ))
  2. Map that value to space (-1, +1) and assign it to variable U
  3. Repeat steps 1 and 2 and assign the result to variable V
  4. Calculate S = U*U + V*V
  5. if S = 0 or S >= 1 then free all variables if needed and restart from the beginning
  6. The following two variables will be independent and standard normally distributed (mean = 0, variance = 1):


Optionally you can add m to the quantities above to change the mean value of the distribution.


Java – Appending objects to binary files

Java is tricky, you cannot add objects to already existent files with default classes.

Here is the solution:


public class Test{
    private static String filename = "test";

    public static void main(String[] args) {
        writeToBinary (filename, "a", true);
        writeToBinary (filename, "b", true);
        writeToBinary (filename, "c", true);
        readFromBinaryFile (filename);

    public static void writeToBinary (String filename, Object obj, boolean append){
        File file = new File (filename);
        ObjectOutputStream out = null;

            if (!file.exists () || !append) out = new ObjectOutputStream (new FileOutputStream (filename));
            else out = new AppendableObjectOutputStream (new FileOutputStream (filename, append));
            out.flush ();
        }catch (Exception e){
            e.printStackTrace ();
                if (out != null) out.close ();
            }catch (Exception e){
                e.printStackTrace ();

    public static void readFromBinaryFile (String filename){
        File file = new File (filename);

        if (file.exists ()){
            ObjectInputStream ois = null;
                ois = new ObjectInputStream (new FileInputStream (filename));
                while (true){
                    String s = (String)ois.readObject ();
                    System.out.println (s);
            }catch (EOFException e){

            }catch (Exception e){
                e.printStackTrace ();
                    if (ois != null) ois.close();
                }catch (IOException e){
                    e.printStackTrace ();

    private static class AppendableObjectOutputStream extends ObjectOutputStream {
          public AppendableObjectOutputStream(OutputStream out) throws IOException {

          protected void writeStreamHeader() throws IOException {}



Android Developer Nanodegree

Official links:–nd801

Google Official Vocabulary – Glossary

Google Material design specification

Google Typography

Google Color Palette

Direct links to the free seminars:


Level Group Course Length Projects
Beginner Core UI Design   ☆¹ 4 Weeks Birthday Card, Business Info
Beginner Core User Input   ☆¹ 4 Weeks Coffee Ordering App, Score Tracker, Quiz App
Beginner Core Multi-Screen Apps   ☆¹ 8 Weeks Language Learning App, Tour Guide App
Beginner Core Networking   ☆¹ 5 Weeks Book List, News Reader
Beginner Core Data Storage   ☆¹ 8 Weeks Pet Database
Beginner Core UI Components 4 Weeks Build Your Own App
Beginner Core UX Design 6 Weeks Build a User-Targeted App
Intermediate Core App Development Ⅰ   ☆² 10 Weeks Popular Movies, Weather, Build Your Own App
Intermediate Core Material Design   ☆² 4 Weeks Apply Material Design to News Reader
Intermediate Services Sign-In & Profiles 2 Weeks
Intermediate Services Maps 2 Weeks
Intermediate Services Location & Context 2 Weeks
Intermediate Services Analytics 2 Weeks
Intermediate Services Ads 2 Weeks
Intermediate Games Game Design Ⅰ 8 Weeks Breakout Clone
Intermediate Games Game Design Ⅱ 8 Weeks MegaMan Clone
Advanced Core App Development Ⅱ   ☆² 6 Weeks Stock Tracker
Advanced Core Optimizing App Performance 4 Weeks
Advanced Core Firebase 8 Weeks
Advanced Core Gradle   ☆² 6 Weeks Joke Teller
Advanced Core Developing Scalable Apps 8 Weeks
Advanced Platforms Android TV & Google Cast 1 Week
Advanced Platforms Android Wear   ☆² 2 Weeks Create Wear Interface for Weather App
Advanced Platforms Android Auto 1 Week






maven – configuration for properties file

The desired requirements are the following:

  1. Output should be a JAR
  2. There must exist at least one properties file (e.g. All properties files should not be placed inside the JAR, but should be easily accessible by the user.
  3. It should be easy to add more properties files without changing pom.xml

To address the requirements above we have to do the following steps:

  1. We create and place the properties file in src/main/resources
  2. To copy the properties file in the target directory upon build,  we add an include rule to the resources section.
  3. To create the JAR file, we use the maven-jar-plugin
    1. We should exclude properties files from the JAR build
    2. We should define the main class for the JAR so that it is executable
  4. We should copy the properties files from the resources directory to the target directory
  5. We should change the code to read the properties file from the classpath

To load the properties, we can use the following code:

Properties prop = new Properties();


Example of the pom.xml:

      <!-- Build an executable JAR -->