Tuesday, December 15, 2009

Using GSON to parse Yelp JSON result

This is a very basic example about using GSON to parse JSON result from Yelp. These few lines of codes will do the trick:

Monday, December 14, 2009

Models and Theories by Peter Norvig

Sunday, December 13, 2009

Getting current GPS coordinates in Android

All you need are these few lines of codes: Also don't forget to add this permission in the manifest file:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>

Friday, November 27, 2009

Nonblocking mechanism for Android GUI

Do you realize that fetching data from the web, for example a GET request, may actually block the GUI and then creates an error? I experienced that and would want to share my thoughts on resolving it. I have not implemented a sound solution but these are the things I have in my mind. There are two components to implement in the code:
1. Thread
2. Handler

Let's see if this works :)

Wednesday, November 25, 2009

Developers are born brave

This is something motivational I want to share with the readers :)
Credits to the creative artist.

Saturday, November 21, 2009

Removing status and title bars in an Android app (suggested method)

I discovered that the status and title bars can be removed in an Android app by inserting one simple line in the manifest xml file. Feel free to try this out and let's discuss if this does not work for you.

<activity android:name=".YOUR_ACTIVITY_NAME"
android:label="@string/app_name"
android:screenOrientation="portrait"
android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

Thursday, November 19, 2009

Distinct intersecting values of two arrays

I have written two different methods that gives the same output. The output is a list of distinct intersecting values of two arrays. I am amazed with the different amount of time taken to solve this problem. It would be something interesting to investigate. Here are the suggested solutions:
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;

public class ArraysIntersection {

private void findIntersectionMethod1(int[] a, int[] b){
long start = System.nanoTime();
int indexA = 0;
int indexB = 0;
ArrayList unique = new ArrayList();
while (indexA < a.length && indexB < b.length) {
if (a[indexA] == b[indexB]) {
if (!unique.contains(a[indexA])) {
unique.add(a[indexA]);
}
indexA++;
indexB++;
} else if (a[indexA] < b[indexB]) {
indexA++;
} else {
indexB++;
}
}
long elapsed = (System.nanoTime() - start);
System.out.println("Elapsed time (findIntersectionMethod1): " + elapsed + "ns");
System.out.println("Intersection of elements: " + unique);
}

private void findIntersectionMethod2(int[] arr1, int[] arr2){
long start = System.nanoTime();
ArrayList unique = new ArrayList();
for (int i = 0; i < arr1.length; i++) {
for (int j = 0; j < arr2.length; j++) {
if (arr1[i] == arr2[j]) {
if (!unique.contains(arr1[i])) {
unique.add(arr1[i]);
}
}
}
}
long elapsed = (System.nanoTime() - start);
System.out.println("Elapsed time (findIntersectionMethod2): " + elapsed + "ns");
System.out.println("Intersection of elements: " + unique);
}

private void findIntersectionMethod3(int[] a, int[] b){
long start = System.nanoTime();
Set unique = new HashSet();
int indexA = 0;
int indexB = 0;
while (indexA < a.length && indexB < b.length) {
if (a[indexA] == b[indexB]) {
unique.add(a[indexA]);
indexA++;
indexB++;
} else if (a[indexA] < b[indexB]) {
indexA++;
} else {
indexB++;
}
}

long elapsed = (System.nanoTime() - start);
System.out.println("Elapsed time (findIntersectionMethod3): " + elapsed + "ns");
System.out.println("Intersection of elements: " + unique);
}

public static void main(String[] args) {

int a[]={1, 1, 2, 4, 4, 7, 8, 8, 9, 14, 14, 20, 20, 20};
int b[]={1, 1, 1, 2, 2, 8, 8, 20};

ArraysIntersection arrIntersect = new ArraysIntersection();
arrIntersect.findIntersectionMethod1(a, b);
arrIntersect.findIntersectionMethod2(a, b);
arrIntersect.findIntersectionMethod3(a, b);
}
}

The output of these methods are as below (I'm telling you, you'll be amazed looking at the time taken for each method):
Elapsed time (findIntersectionMethod1): 455000ns
Intersection of elements: [1, 2, 8, 20]
Elapsed time (findIntersectionMethod2): 15000ns
Intersection of elements: [1, 2, 8, 20]
Elapsed time (findIntersectionMethod3): 65000ns
Intersection of elements: [2, 8, 1, 20]

I'm sure there are much better solutions to solve this interesting problem that I have not thought of.