Friday, April 19, 2013

Give location information in Android Emulator

When we dealing with simulating the GPS or location-based service on emulator of Eclispes ADT bundle, the results are null generally, namely, we don't have the access of the longitude and latitude of the coordination. However, in this kind of scenarios, there are two commons ways to address this problem.


  1. open the command in Windows and make sure that you open the telnet services. For Windows 7 the telnet service is automatically closed unless we manually start it. (Please Google how to start telnet service if you have doubts) In the command line, type telnet localhost 5554. If your emulator is port 5556 or something else, type it accordingly. The port number will be shown in the title when you start the emulator. Then type geo fix 0 0. The two numbers here are the coordination (longitude and latitude respectively) of the location you desired.
  2. OR you can use the DDMS on the upper right corner of the Eclipse ADT. Then choose the device and type the coordinates and press send.
press the above icon on the top right of the Eclipse window and choose DDMS


Either way will do the trick.

p.s.
One more solution is to use this brilliant guy dpdearing's solution. An Android GPS Location Emulator

Sunday, April 14, 2013

How to get your own SHA1 fingerprint

When you first starts the Android development, you will need the SHA1 fingerprint. If this is your time you may find this a little bit hard to find out.

In the command terminal, first step is to locate the keytool.exe, which is the tool to obtain the SHA1.
Usually, the location of the file is located under JDK directory. In my case it locates at
C:\Program Files\Java\jdk1.7.0_11\jre\bin

After locating the directory, next step is to find the .android directory location which typically locates at the user directory under your name, for example, C:\Users\Bowen in my case.

Finally, type the following command to get the fingerprint:
keytool.exe -list -alias androiddebugkey -keystore "...\.android\debug.keystore" -storepass android -keypass android

The ... in the parentheses represents the directory location I mentioned above.
If you follow the above steps correctly, the system will display the Certificate fingerprint (SHA1).

Please leave a comment if you have any questions. Thx

Saturday, April 13, 2013

Java - Iteration and Arrays


"while" loops to check a number is prime or not.

public static boolean isPrime (int n) {
  int divisor =2;
  while (divisor < n) {
    if (n%divisor == 0) {
      return false;
    }
  return true;
}

"for" loops

for (initialize; test; next) {
  statement;
}
  Equivalent to: 
initialize;
while (test) {
  statement;
}
next;

ARRAYS
initialize a list of charactors:
char[] c = new char[4]; // reference to array of charactors of any length

For strings c.length() is a method
For characters c.length is a field.

Print the primes from 1 to n using Sieve of Eratosthenes:
public static void printPrimes(int n) {
boolean[] prime = new boolean[n+1];
        int i;

for (i=2; i<=n; i++){
prime[i]=true;
}
for(int divisor=2; divisor*divisor<=n; divisor++){
if(prime[divisor]){
for(i=2*divisor; i<=n; i=i+divisor){
prime[i]=false;
}
}
}
for(i=2; i<=n; i++){
if(prime[i]){
System.out.print(" "+i);
}
}
}
}

Friday, April 12, 2013

Java - Object & Methods

Object & Methods

String s1; //Step 1: declare a String variable.
s1 = new String(); //Step 2: assign it a value: a new empty string.

first line create a box, store references in strings.
second line construct a brand new string object.
= create a reference

A syntax sugar in Java:
String s2 = new String();

s1 = "Yow!"; also creates a constructor with Yow in the box.

now s1 forget the last one and remembers yow instead of the empty one.

s2 = s1; // reference to the same object;
s2 = new String (s1); //now 2 different, identical objects.

3 String constructors:

  1. new String() constructs empty string - contains no character. 
  2. "whatever"
  3. new String(s1) takes a parameter s1. Makes copy of object that s1 references.
Constructors always have the same name as their class, except "stuff in quotes".

s2 = s1.toUppercase();
String s3= s2.concat("!!"); // or just s2+"!!";
String s4 = "*".concat(s2).concat("*");//"*".concat(s2) compute first

In Java, String object are immutable unlike in C: their contents never change.


Java I/O Classes

Objects in System class for interacting with a user:
System.out is a PrintStream object that outputs to the screen.
System.in is an InputStream " " reads from the keyboard.
Actually, the two mentioned above should be variables that references to the object.

readLine method is defined on BufferedReader objects.

How do we construct a BufferedReader(compose into entire lines of text)? With an InputStream.Reader.
How .. an InputStreamReader(compose into characters typically 2 bytes)? We need an InputStream 
How .. an InputStream(read raw data)? System.in is one.
Figure this out from online Java libraries API. Specifically, java.io library here.

import java.io.*;//to use the java library, other than java.lang

class SimpleIO{
  public static void main(String[] arg) throws Exception{
    BufferedReader keybd = new BufferedReader(new InputStreamReader(System.in));
    System.out.println(keybd.readLine());
  }

-------------------------------------------------------------------------------------------------------

import java.net.*;
import java.io.*;

public class readURL {
public static void main(String[] arg) throws Exception{
URL u = new URL("http://www.bbc.co.uk");
InputStream s = u.openStream();
InputStreamReader inputrd = new InputStreamReader(s);
BufferedReader bbc = new BufferedReader(inputrd);
System.out.println(bbc.readLine());
}

}

Java – OOP intro


Some concepts in Object-Oriented Programming.
Object: Repository of data, such as milk or jam. 
Class: Type of object. 
Method: Procedure or Function that operates on an object (or class).
Inheritance: A class can inherit properties from a more general class. For instance, Shopping List inherits from List class the property of storing a sequence of items.
Polymorphism: One method call works on several classes, even if the classes need different implementation. e.g. “addItem” method on every kind of List, even though adding item to ShoppingList is different from ShoppingCart.
Polymorphism is the thing that distincts from other languages.
Object-oriented: Each object knows its own class&method. Each ShoppingList & ShoppingCart knows which addItem method it uses.


Code example:

String myString;
// mystring is a variable
myString = new String();
// new String() is called a constructor. It creates a string object,
// "=" causes myString to a reference the object

Differences between Java & Scheme:

  1. Everything in Java has a type; you must declare it.
  2. Scheme program (.sch) --> (eval) Answer. Java program (.java) --> (javac) .class Files --> (java) Answer