Thursday, December 24, 2015

Java Programming: Swap two integer values without using a temporary variable


Every time I had to take programming exam for a software developer position, this is one of the tricky questions I usually encountered: "How to swap two integer values without using a temporary variable?". The first time I had this question I spent about 45 minutes thinking about the solution until I gave up and thought that it was impossible to solve. Fast forward many years after, I am wiser now so I already know the solutions. It turned out that there are two possible ways to solve this tricky question.

Solution #1: Using Arithmetic Operator

public class SwapVariablesDemo1
{
  public static void main(String[] args)
  {
    int x = 1;
    int y = 2;
    
    System.out.println("Values before swapping: x = " + x + ", y = " + y);
    
    x = x + y; // x = 3, y = 2
    y = x - y; // x = 3, y = 1
    x = x - y; // x = 2, y = 1
    
    System.out.println("Values after swapping: x = " + x + ", y = " + y);
  }
}

Output:

Values before swapping: x = 1, y = 2
Values after swapping: x = 2, y = 1


Solution #2: Using XOR Bitwise Operator

Aside from the arithmetic solution, the more technical way to solve this problem is to use the XOR bitwise operator. The XOR bitwise operator returns 0 if both operands are the same and returns 1 if both operands are different (refer to the XOR truth table below).

X Y X ^ Y
0 0 0
0 1 1
1 0 1
1 1 0

public class SwapVariablesDemo2
{
  public static void main(String[] args)
  {
    int x = 1; // 0001 in binary
    int y = 2; // 0010 in binary
    
    System.out.println("Values before swapping: x = " + x + ", y = " + y);
    
    x = x ^ y; // x = 3 (0011), y = 2 (0010)
    y = x ^ y; // x = 3 (0011), y = 1 (0001)
    x = x ^ y; // x = 2 (0010), y = 1 (0001)
    
    System.out.println("Values after swapping: x = " + x + ", y = " + y);
  }
}

Output:

Values before swapping: x = 1, y = 2
Values after swapping: x = 2, y = 1

If you want to impress your future employer during your programming exam, then I suggest that you use the XOR bitwise solution provided that you can explain the logic behind it.

Now, try asking your fellow developers this question and find out how many of them know the solution :)

Monday, December 14, 2015

Eclipse Plugin Tutorial: Add New Menu and Toolbar Items to Eclipse 3.x Plugin


This tutorial is based on Eclipse Mars and Java 1.7.

In the previous tutorial, I showed you how to create a basic Hello World Eclipse 3.x plugin using the plugin template wizard. This time I will show you how you can manually add (contribute) new menu and toolbar items to your own plugin.

Tutorial Outline:

Prerequisites
Command
Handler
Bindings
Menu Item
Toolbar Item
Run the plugin

Prerequisites

You have already created a basic Hello World plugin using the plugin template wizard.

Why are there only few female software developers?


When I was still on my first year in college, the very first thing I noticed was that I only had 12 fellow female computer science students out of 40 in my batch. The number decreased each year and by the time I graduated there were only last 5 women standing proud for surviving long hours of solving brain crunching programming problems. When I started with my first job, I was the sole female software developer in the company. With my second job, there were only two of us. For my current job, there were five of us in our department. I was already so used to a work environment where most of my colleagues are male. If you are like me who wonder why there are so few female software developers, then I came up with the following probable reasons.

  1. Most women don't like studying Math. Since Computer Science is a course that has lot of Math in the curriculum, it is a big turn off to female college students. I myself needed to put extra effort just to pass all of my Math and Physics subjects in college. Most of my female classmates simply gave up the course after failing their Math subjects.
  2. Most female software developers can't stand the stressful nature of the job. Seriously, every single day of my life as a software developer involves STRESS. Stress from tight deadlines, lots of sleepless nights trying to figure out how to solve bugs, the need to learn new programming language/framework, etc. I can't blame why most female software developers shifted to QA Engineers or management positions after a few years.
  3. Since the IT (Information Technology) is a male-dominated industry, there are some female software developers who feel awkward and lonely to be surrounded by male colleagues. I remembered on my first job, during lunch breaks all of my male colleagues are busy discussing stuffs like NBA, Dota and every single sports and computer games you could think of, while I was left alone on my cubicle reading Hollywood Gossip websites. Every time there is a job opening for software developer on my work, I always wish that the company will hire a female software developer so I will have someone to share girly talks with.
Those are some of my observations and opinions of why there are only few female software developers. If you have other opinions to share about this matter, let me know in the comment section below.

Friday, December 4, 2015

Eclipse Plugin Tutorial: Eclipse 3.x Plugin Development for Beginners


This tutorial is based on Eclipse Mars and Java 1.7.

Before I became an Eclipse Plugin developer, I used to wonder if it is possible to extend the Eclipse IDE to add an MP3 player so I can easily switch music without leaving the IDE. It turned out that it is possible because the Eclipse IDE is made out of several reusable software components called plugins. This means that you can write your own plugin to contribute a new functionality to the Eclipse IDE (like an MP3 player, pretty cool huh?).

For this tutorial, I will show you how to create a basic “Hello World” plugin using Eclipse 3.x framework which is the traditional way of creating Eclipse plugins. With the release of Eclipse version 4.x (E4), you can already create plugins based on a model and it also supports dependency injection and CSS styling. I will write a separate tutorial for E4 plugin development. So, let’s start!