Posts by Chang Park

Interface vs. Abstract class vs. Concrete class

interface

  • uses “implements” to be implemented
  • multiple interfaces can be implemented
  • interface can extends only interface

interface

abstract class

  • uses “extends” to be extended
  • if it includes at least one abstract method, it should be declared as an abstract class.
    • abstract method: undefined method
    • concrete method: defined method
  • There are already defined methods also
  • Abtract class extends both regular class and abstract class.

abstractClass

Concrete class

  • uses “extends” to be extended
  • Concrete class has only concrete methods(already defined).
class Example{

   public void display1(){
      System.out.println("display1 method");
   }
   public void display2(){
      System.out.println("display2 method");
   }
}

final

Variable

  • Variable cannot be changed

final int num=3;

Method

  • Method cannot be modified
final String getNumber() {
   return "result";
}

Class

  • Nobody can extend this class
  • That means, no body can override methods of this class
  • Possible to use as it is(not modifying).
public final class String{

}

Jimple

Soot program converts Android application file into Jimple code.

Jimple contains two general concepts

  • Local – local variables
  • Units – statements

 

IdentityStatements

  • $r0 := @this: de.ecspride.RV2013;
  • $r1 := @parameter(): android.view.View;

 

AssignStatements

  • Assignments between locals, constants, and fields

 

CastExpr

  • Right-hand side of the assignment is a typecast
  • $r4 := (android.widget.EditText) $r1;

 

Four different expressions to call methods

  • VirtualInvokeExpr
    • Virtual dispatch invoke to an instance method
  • StaticInvokeExpr
    • Calling a static method
  • InterfaceInvokeExpr
    • Calling a method of an object of which only its interface type is known
  • InvokeStmt
    • Any invoke expression which can be part of standalone statement

super vs. this

  • super : used to access methods of the base class
  • this : used to access methods of the current class

superTHis1 superThis2

The third line is printing “animal : eat” because we are calling super.eat().

If we called this.eat(), it would have printed “dog : eat”.

http vs. https (and SSL)

http (hypertext transfer protocol)

  • transfer hypertext data
  • port: 80

https (hypertext transfer protocol over secure sockets layer)

  • Secure called webpage and data
  • But slower the transferring speed
  • Port: 443

SSL (Secure Sockets Layer)

  • Encryption: Hiding what is sent from one computer to another
    • Computers agree on how to encrypt
    • Server sends a certificate
    • Client’s computer says “start encrypting”
      • in here there are 3 messages
        • Client key exchange: Both computers calculate a master secret code
        • Change cipher spec: client’s computer is asking server to encrypt
        • Finished: let’s start now
      • The server says “start encrypting”
        • in here there are 2 messages
          • change cipher spec: I’m going to send encrypted message now
          • finished: let’s go
        • This message gets encrypted
      • All messages are now encrypted
        • It will look as a garbage to other
  • Identification: Making sure the computer you are speaking to is the one you trust
    • 1. Company asks CA(Certificate Authority) for a certificate
      • The company asks CA for a certificate
        • The web server
        • What the company is
        • Where it is located
      • Certificate Authority checks correctness and authenticity of company
    • 2. CA creates certificate and signs itSSL1
    • 3. Certificate installed in server
      • The company will run a web server
      • The certificate is installed into this server
      • The web server is configured to use the certificate
    • 4. Browser issued with root certificates
      • You can check installed certificate authorities on your computer.
      • On my computer(mac), I can check with Keychain Access applicationSSL2
    • 5. Browser trusts correctly signed certs
      • Client’s computer has public key of the certificate authority
      • When it receives a certificate, it will verify a encrypted signature of the certificate with the public key.

Overloading vs. Overriding

Overloading

  • When making multiple methods with using same method’s name
  • Parameter has to be different

overloadingCodeoverloadingResult

Overriding

  • When trying to edit parent class’ method.
  • Method name and parameter has to be same
  • @override is not necessary, but better to write.
  • It is better to write @override before the method.
    • pros
      • Compiler will check error when you make mistake while overriding such as misspelling method name.
      • Otherwise you won’t know whether you made a mistake, and also you will get unexpected behavior
      • Readabilty both for other developers and for you

overridingCodeoverridingResult

Conclusion

Overloading: In same class, make same named methods with different return type or parameter.

Overriding: Redefining parent’s method. One of Polymorphism which is characteristic of object oriented programming .

Levenshtein Distance

Description

Levenshtein distance (LD) is a measure of the similarity between two strings, and it is a minimum costs to transform a string to the other using deletions, insertions, or substitutions. This is really useful algorithm for comparing two strings.

Algorithm

Screen Shot 2016-07-17 at 12.17.24 AM

As shown on the algorithm table, make a calculated distance table.

Next, initialize first row and column as 0 – (m or n).

Get minimum value of left one +1upper one +1, and diagonally above and to the left + cost.

Example ( GUMBO & GAMBOL )

Screen Shot 2016-07-17 at 12.33.39 AM

Step 7 

final Levenshtein Distance is lower right hand corner of the matrix which is 2 in this case.

Java Code

public class Distance {

  //****************************
  // Get minimum of three values
  //****************************

  private int Minimum (int a, int b, int c) {
  int mi;

    mi = a;
    if (b < mi) {
      mi = b;
    }
    if (c < mi) {
      mi = c;
    }
    return mi;

  }

  //*****************************
  // Compute Levenshtein distance
  //*****************************

  public int LD (String s, String t) {
  int d[][]; // matrix
  int n; // length of s
  int m; // length of t
  int i; // iterates through s
  int j; // iterates through t
  char s_i; // ith character of s
  char t_j; // jth character of t
  int cost; // cost

    // Step 1

    n = s.length ();
    m = t.length ();
    if (n == 0) {
      return m;
    }
    if (m == 0) {
      return n;
    }
    d = new int[n+1][m+1];

    // Step 2

    for (i = 0; i <= n; i++) {
      d[i][0] = i;
    }

    for (j = 0; j <= m; j++) {
      d[0][j] = j;
    }

    // Step 3

    for (i = 1; i <= n; i++) {

      s_i = s.charAt (i - 1);

      // Step 4

      for (j = 1; j <= m; j++) {

        t_j = t.charAt (j - 1);

        // Step 5

        if (s_i == t_j) {
          cost = 0;
        }
        else {
          cost = 1;
        }

        // Step 6

        d[i][j] = Minimum (d[i-1][j]+1, d[i][j-1]+1, d[i-1][j-1] + cost);

      }

    }

    // Step 7

    return d[n][m];

  }

}

Android Application

android-application-development

  • Written in Java language
  • They are compiled to Android’s own bytecode format, Dalvik executable(DEX)
  • Doesn’t have a single entry-point, such as the main method in Java
  • Four components
    • Activities: visible parts of an application
    • Services: run in the background and are not interacting with the user directly, used for long running background such as mp3 playback
    • Broadcast receivers: react to global events, such as incoming calls or text messages
    • Content providers: implement domain-specific databases for, e.g. contacts
  • onCreate(): gets called when the activity is launched for the first time
  • Android Emulator: virtual mobile device that runs on a computer and is similar to a real device.
  • Android API calls
    • Log.i(String tag, String msg):
      • Static methods which writes an info messages to the log
      • The log can be browed using the tool LogCat
    • findViewById(int id)
      • returns references to GUI objects

MySQL Basic Queries

Remeber

Do not ever use lowercase letter for query

  • It works, but it is hard to read

Space, enter doesn’t affect to query

  • You can use one query per line if you want.

Example


SELECT names
FROM customers

Queries

SELECT name FROM customers

  • Gets name column from customers table

SELECT name,zip FROM customers

  • Gets name and zip columns from customers table
  • You can get multiple columns by using commas

SELECT * FROM customers

  • Gets all columns from customers table

SELECT DISTINCT state FROM customers

  • Gets only one time for same value(No duplicate)

SELECT id, name FROM customers LIMIT 5

  • Gets first 5 items

SELECT id, name FROM customers LIMIT 5,10

  • From 5 , gets 10 items
  • First 5 is just as index, so it starts from 0
  • In here, you will start getting from 6th item

Hadoop – Mapper & Reducer

Definition

MapReduce is a processing technique and a program model for distributed computing based on java. The MapReduce algorithm contains two important tasks, namely Map and Reduce.

  • Map takes a set of data and converts it into another set of data, where individual elements are broken down into tuples (key/value pairs).
  • Reduce task takes the output from a map as an input and combines those data tuples into a smaller set of tuples.

As the sequence of the name MapReduce implies, the reduce task is always performed after the map job.

diagram

Example code is in my project.