13 May 2018

17 October 2017

Important features and of C programming language


  1. Function prototype declaration. Like declaring a variable in java, you can also declare a function. It is useful if you are going to call the function before the definition of the function. 
  2. C has pointer operator.
  3. C doesn't have a string data type. Instead we can use char array. 
  4. Structure is a custom data type. It's kind of an "object".
  5. "%lf" to format double
  6. "%.2f" to format float with 2 digit after a comma. 
  7. "%d" to format an integer. 
  8.  Store only positiv integer value: unsigned int a;
  9. int A[4], printf("%d\n", A); will return the base address (Address of first element)
  10. int A[4]; A[0] = 3;  printf("%d\n", A*); will return the value of the first element.
  11. Casting an Array to a pointer, now you can access the element without brackets

12 October 2017

32 Bit & 64 Bit floating number

single precision

double precision

Source: Wikipedia

Please watch this video for better understanding

11 October 2017

Angular 4, create your own custom binding

Sometimes, you want to create your own data binding, for example to use the same properties in different components to support modular programming. If you're unfamiliar with binding and component in Angular, please check my previous post about Angular. We use data binding to bind the html file to the business logic on the typescript file. But instead of using any existing properties of the html tag like [src]="" or [(ngModel)]= "" we can create our own.

  1. create a new sub-component. You can create it manually or using cli command: ng g c yourComponent --spec false. Now you can use the <app-yourComponent> tag in your app-component.html
  2. In the sub-component create an object you want to bind for e.g user
  3. Add @Input() decorator before the object declaration
  4. your HTML file will look like this when binding element object(in serverComponent) and serverElements array(in app.component)

10 October 2017

Creating own library in C

To create your own library in C please follow this steps:


  1. Create two folders, name the first folder 'utils' and the second folder 'headers'
  2. Write your functions in a .c file and put it in the utils folder. 
  3. Create a .h file with the same name as the .c file that contains the functions. Put the header files in the headers folder. The .h file should contains following code. 
  4. Open terminal and go to the utils folder and compile the .c files using this command gcc -c *.c
  5. Build the library by putting all the .o files in an archive using this command ar -cvq yourlibraryname.a *.o library name starts with lib, and don't froget the .a extension at the end
  6. Now you can use the library in your program by adding a file header #include "headers/yourLibraryName.h"
  7. Compile your program by providing the library name gcc -o programName programSourceCode.c utils/libraryName.a
  8. If youre using C-Lion IDE, you can simply add your .c and .h paths to the CMakeList.txt like this:

3 October 2017

Angular basic knowledges, installing, adding bootstrap, and two ways data binding

  1. Untuk memulai menggunakan framework Angular, kamu harus menginstall node terlebih dahulu. Pergi ke Node browser, lalu ikuti instruksi untuk mendownload node.js
  2. Setelah itu pergi ke terminal, lalu ketik npm install -g @angular/cli untuk menginstall angular command line interface. kunjungi websitenya untuk mendapatkan informasi lebih lanjut Angular CLI
  3. untuk memulai suatu project ketik ng new <yourappname>
  4. lalu masuk ke folder applikasi kamu dengan mengetik cd <yourappname>
  5. lalu jalankan applikasi kamu dengan mengetik ng serve cek applikasi kamu dengan membuka browser dan ketik localhost:4200
  6. Untuk menambahkan bootstrap, caranya install bootstrap melalui npm npm install bootstrap --save lalu buka folder .angular-cli.json dan tambahkan di bagian "styles" "../node_modules/bootstrap/dist/css/bootstrap.min.css"
  7. Data binding satu arah dari HTML ke Business logic biasanya dilakukan menggunakan event handling <button (click) = "YourMethod()"> atau <input type="text" (input) = "YourMethod($event)">
  8. Sebaliknya, data binding satu arah dari Business logic ke HTML biasanya dilakukan menggunakan property binding <button [disabled]="sex === 'male'">
  9. Untuk melakukan komunikasi dua arah antara typescript(Business Logic) dan Template(HTML) kita bisa mengunakan data binding dua arah <input type="text" [(ngModel)] = "data">

23 September 2017

Linux Ubuntu Server Important Commands

  • Adding new user: adduser {username}
  • Adding user to sudo group (seperti administrator) gpasswd -a {username} sudo
  • Change user from root su - {username}
  • Update package dengan versi terbaru seandainya ada, atau updating packages dari repository sudo apt-get update
  • Upgrade software dengan versi terbaru sudo apt-get upgrade
  • Memberi nama kepada host (hostname) echo "{yourHostname}" > /etc/hostname
  • Editing hosts /etc/hosts file nano /etc/hosts
  • Editing local timezone dpkg-reconfigure tzdata
  • Setelah kamu menginstall MongoDB, kamu bisa men-run mongodb server dengan sudo service mongodb start
  • Untuk melihat daftar program yang sedang berjalan 'running processes' ps -ef
  • Untuk menghentkan suatu program atau running process kill {processID} atau kill -9 {processID}
  • Seandainya kamu menggunakan 'putty' untuk mengakses remote server, pasti kamu tahu bahwa ketika kita mengakhiri suatu session, maka program yang berjalan di foreground akan mati. Untuk mengakalinya, kita bisa menggunakan programm 'screen'. Setelah kamu menginstall screen sudo apt-get install screen, kamu bisa memulai screen dengan mengetik screen dan untuk keluar dari session tanpa mematikan process yang sedang berjalan kamu bisa men-detach screen dengan CTRL + A + D. untuk kembali masuk ke screen yang tadi kamu detach, kamu bisa ketik screen -r {screenID} untuk men-rettach screen tersebut

Using Retrofit to build HTTP Client on Android

Untuk berkomunikasi dengan remote server, kamu bisa menggunakan java 3th party library 'Retrofit'. Dengan library ini kamu bisa mengirim dan menerima data ke dan dari remote server dalam bentuk JSON. Retrofit adalah library favorit saya karena mudah sekali untuk menggunakannya. Disini saya akan merangkum step-step bagaimana kamu bisa menggunakan retrofit dengan cepat di project kamu

  1. Tambahkan internet permission ke dalam manifest XML file.
  2. Tambahkan Retrofit dan Gson kedalam gradle build file
  3. Buat Object class yang akan di kirim atau di terima sebagai JSON file, jangan lupa bahwa semua fieldnya harus di declared dengan tipe String
  4. Buat ApiClient class
  5. Definisikan API Interface
  6. Gunakan ApiClient di Aplikasi kamu

Terkadang kamu harus membuat suatu class tambahan yang bisa menampung respon object dari server. Misal, kamu mengirim ("POST") user data melalui User object, akan tetapi jawaban (respond) dari server memiliki properties yang berbeda dari User class. Kamu bisa membuat Class Result yang bisa menampung respon dari server kemudian kamu bisa menambahkan Result Object kedalam Class User. Tool yang saya pakai untuk membuat Result class ini adalah JsonSchemaToPOJO Caranya mudah, masukan respon (JSON) yang kamu dapatkan dari aplikasi POSTMAN kedalam formular yang ada di Json2Pojo.

22 September 2017

NPM modules for a simple web application

  1. Express
  2. Nodejs backend framework untuk applikasi web. Memudahkan kamu untuk menghandle routes, get and post requests. untuk menginstall ketik npm install express --save
  3. Handlebar
  4. Adalah javascript templating engine dengan syntax yang sangat simple. Dengan menggunakan syntax {{yourObjectProperty}}. Untuk mengkombinasikan express.js dan handlebars.js kamu harus menginstall hbs packaged module dari npm. npm install hbs --save
  5. Nodemon
  6. Request
  7. Yargs
  8. Mongoose
  9. Body-parser
  10. Lodash
  11. validator
  12. jsonwebtoken
  13. bcrypt

20 September 2017

MongoDB important commands

  • Setelah kamu sukses menginstall MongoDB, kamu bisa masuk ke 'bin' directory dimana MongoDB diinstall. Di Windows biasanya directory ini berada di C:\Program Files\MongoDB\Server\YourMongoDBVersion\bin didalamnya kamu bisa menyalakan db server dengan mengetik mongod.exe --dbpath \Masukan\Path\Folder\pilihan\kamu lalu tekan enter.
  • Kalian bisa berkomunikasi dengan database dengan men-run mongo.exe di bin directory, atau dengan menggunakan mongo UI 'Robo 3T' (formerly 'Robomongo')

Node important commands

Starting a Project

Untuk memulai node project kita harus membuat directory, dan melalui command line, kita masukan npm init untuk memulai sebuah node project. Setelah menginisialisasi npm kita nantinya bisa menginstal npm modules ke applikasi yang kita buat.

Debugging:

  1. Untuk debugging, gunakan module nodemon, sehingga setiap perubahan pada code langsung bisa kita lihat effekt running time nya. Nodemon merestart programm kita setiap kali kita melakukan perubahan. cara menginstall 
    • npm install -g nodemon -g disini berarti global
    • nodemon [your node app] untuk menjalankannya.
  2. Debugging tool dengan menggunakan chrome browser nodemon --inspect-brk [your node app] lalu buka chrome browser dan ketik chrome://inspect
    disini kamu akan melihat app kamu di list di 'Remote Target' section. Setelah debugging window terbuka, kamu bisa mengklick di line number untuk meletakan stop points

19 September 2017

ES6 Features


ES6-string-injection

Di ES6 Ada cara mudah memasukan variable ke dalam String. Kalian cukup menggunakan tanda `Put your String here and add your variable like this ${yourVariable}`



Variable 'let'

Perbedaan utama di variable let adalah pada scope-nya. let syntax memungkinkan kita untuk mengimplementasi local variable dengan mudah.



Arrow Function

Arrow function sangat memudahkan kita menulis code di javascript. Tapi penggunaan arrow function saya sarankan hanya seandainya kamu sudah tau menulis fungsi tanpa menggunakan arrow function. karena penggunaan arrow function akan membingungkan tanpa memahami proses apa yang terjadi di balik itu.



Object destructuring

Kalian bisa membuat variable baru dari objects' properties



Promises

Untuk menghandle Asynchronous call, kita bisa menggunakan Promise methode. Cara ini lebih baik karena bisa meminimalisir kesalahan ketika berkerja dengan asynchronous methods.



26 April 2017

MatLab | Backslash ('\') or Left matrix division

Backslash denotes left matrix division. x=A\b is a solution to A*x=b.

25 March 2017

Off-Topic | Ubuntu | No wifi connection after standby modus

If you're using Ubuntu on Lenovo ThinkPad T430 series, you might have the same problem as me: No internet connection after you've woken up your laptop from standby modus.

I haven't found any better solution yet but this will solve your problem: go to terminal and run this code sudo service network-manager restart please write comments if you have more automated solution for me.

12 March 2017

Java | Client & Server | Simple Socket Programming

To make an application that can communicate with other applications like chat-app or multiplayer games, we need a server that serve data between clients. A Client send data to the server and the server send it to another client. Server and client send data to each other via sockets.
How to create a Server
  • Create a class with a main method
  • Create an instance of ServerSocket. You need to pass any port number as a parameter ServerSocket ss = new ServerSocket(9999);
  • In order to accept an incoming client connection, we have to call the ServerSocket.accept() method. As soon as connection request is made from client, this method will return a socket that created specifically for this client.
  • To send and receive data from client, you can use OutputStreamWriter and BufferedReader
  • The Server
How to create a Client
  • Create a class with a main method
  • Create an instance of Socket class and pass the port number and the private ip address of your server as parameter.  You can use localhost instead of ip for local testing. Socket s = new Socket(ip, port);
  • To send and receive data from client, you can use OutputStreamWriter and BufferedReader
  • The Client
How it works
Run the server. once the accept() method is called, the server will wait for a client to connect. When you run the client, a connection request is made and ServerSocket will then create a socket that is connected to the client's socket until Socket.close() method is called. To make a simple chat application, put a Writer and a Reader in a while loop and use a Scanner to take user input.

10 March 2017

Java | Important Syntax

  • Ternary Operator
  • It is basically a simplified if statement. If the condition is true, the first value will be assigned to the variable. if false, the second value will be assigned.
    The statement is false. So x will have the value of b

Java | try with resources statement

Since Java 1.7 you can declare all resources that can be closed. e.g BufferedReader right after the try statement. With this feature, all resources will be closed and errors will be caught automatically. Before Java 1.7 you'll have to close all resources manually. like this:
managing resources before java 1.7
Since Java 1.7 this is simplified to this:
managing resources after java 1.7

9 March 2017

Java | Important classes

BufferedReader vs Scanner
  • BufferedReader has a bigger buffer (8192 chars) than the one using in scanner (1024 chars). Use BufferedReader if you want to get long String from a stream.
  • BufferedReader is synchronized or thread safe while scanner is not
  • Use scanner if you want to parse a file and use BufferedReader if you want to read line by line
  • To use readLine() method, you have to create BufferedReader object and put a class that extends Reader object as a parameter: BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); The parameter of the InputStreamReader is your input source, in this example we get an input from the console. System.in Be sure to put BufferedReader object in try block to manage the resources.
using BufferedReader in try block
Serializable
Let's think about a game. After some hours of playing game, you might want to have a break. You want to save the current state of your game so you can continue later from the same point. When saving a game or an application. You want to save the state of objects that have been created during the runtime of your application. In Java an Object cannot be stored in a file as an Object. It can only be stored if it converted to a stream (of bytes) beforehand. The process of converting is called serialization.

Some reasons for serialization are:
  • An object is created on the heap and the heap is for temporary storage. Only serialized objects can be saved as a file
  • To transmit data or state of an object to different location through the network
To make an Object serializable the class muss implement Serializable interface
make a class serializable
This is how you save an object into a file.
save an object as a file
And to retrieve the saved object.
retrieve the state of an object from a file

JavaFX | Basic Knowledge

The Stage

In JavaFX the The user interface is the 'Stage'. Like in a theater, the stage is where all the events will be presented to the audiences or the users.
To create a JavaFX application on IntelliJ, you simply have to create a new JavaFX class.
Creating JavaFX application
After you created the JavaFX application, you can see, there is a start method to be implemented. For now the curtains of the stage is still closed. You can open the curtains by adding primaryStage.show(); to the start method.
adding show method to open the stage
If you now run the application, you'll see an empty 'stage'. The stage is there, now it is up to you to perform a show

The Scene

Now that you have a stage, you might want to perform a show. To do that, you'll need a scene. In a scene, there are some props or in Java terms, 'nodes'. A node can be a button, text field or just a label. To create a good scene you might want all these nodes to be organized nicely. Some nodes that build one feature, should also be placed side by side so the user will know intuitively what these nodes are about. Nodes is organized in a class called Parent. In Web term, it is similar to div tags. You can organize the nodes vertically using VBox Parent or horizontally using HBox Parent.

Layouts

BorderPane
BorderPane is a layout with five areas: Top, left, center, right and bottom.
GridPane
As the name said, GridPane works with grid system.
use indexes to organize the nodes
use hgap and vgap to control the space between grid
when there is not enough space, the nodes will shrink to fit the window