Friday, September 12, 2008

List and Invoke Methods Runtime Using Reflection

In this tutorial i will show you how to list all methods in a class and try to invoke some method runtime. Ok, lets begin.
1. Create a new class, in this case i called Reflection:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package org.kazao.tips.tip028;

/**
*
* @author Mr. Kazao
*/
public class Reflection {

public Reflection() {
}

public static void main(String... args) {
new Reflection();
}
}
2. Now we will try to create a inner class, i called Calculator in this class is provide 4 simple calculation method addition, substraction, division, and multiply, write like this:
class class Calculator {

public int add(int a, int b) {
return a + b;
}

public int sub(int a, int b) {
return a - b;
}

public double div(double a, double b) {
return a / b;
}

public int mul(int a, int b) {
return a * b;
}
}
3. Now we will try to list all method runtime, fisrt make a Calculator variable like this:
Calculator calc = new Calculator();
and now create array of method list all available method in Calculator class:
Method[] methods = calc.getClass().getDeclaredMethods();
4. Now try to print out all methods:
for (Method m : methods) {
System.out.println("-> " + m.getName());
}
and we will get 4 methods like this:
-> add
-> sub
-> div
-> mul
5. Now try to get sub method:
Method sub = calc.getClass().getMethod("add", new Class[]{int.class, int.class});
and invoke it to addition 2 number:
int a = 4;
int b = 6;
System.out.println("Addition: " + a + " + " + b + " = " + sub.invoke(calc, new Object[]{a, b}));
we will get like this:
Addition: 4 + 6 = 10
6. Ok, now we try to get div method:
Method div = calc.getClass().getMethod("div", new Class[]{double.class, double.class});
and try to division 2 number:
double x = 12;
double y = 2;
System.out.println("Division: " + x + " / " + y + " = " + div.invoke(calc, new Object[]{x, y}));
we will get like this:
Division: 12.0 / 2.0 = 6.0

Conclusion:
Listing and invoking method in a class runtime is very simple, you will like it.

Complete Source
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package org.kazao.tips.tip028;

import java.lang.reflect.Method;

/**
*
* @author Mr. Kazao
*/
public class Reflection {

public Reflection() {
Calculator calc = new Calculator();
Method[] methods = calc.getClass().getDeclaredMethods();
for (Method m : methods) {
System.out.println("-> " + m.getName());
}
try {
Method sub = calc.getClass().getMethod("add", new Class[]{int.class, int.class});
int a = 4;
int b = 6;
System.out.println("Addition: " + a + " + " + b + " = " + sub.invoke(calc, new Object[]{a, b}));
Method div = calc.getClass().getMethod("div", new Class[]{double.class, double.class});
double x = 12;
double y = 2;
System.out.println("Division: " + x + " / " + y + " = " + div.invoke(calc, new Object[]{x, y}));

} catch (Exception e) {
e.printStackTrace();
}
}

public static void main(String... args) {
new Reflection();
}

class Calculator {

public int add(int a, int b) {
return a + b;
}

public int sub(int a, int b) {
return a - b;
}

public double div(double a, double b) {
return a / b;
}

public int mul(int a, int b) {
return a * b;
}
}
}

+++Read More+++Summary only+++View Complete Source +++Hide Complete Source+++
Diposting oleh M. Jumari di Friday, September 12, 2008 2 komentar
Label: English, Java, Reflection, Tips and Tricks, Tutorial

Monday, September 8, 2008

Encrypting Data Using DES in Java

In this tutorial i will show you how to encrypt and decrypt text using DES in java, encrypt and decrypt is very simple, lets begin.
1. Create a new class, i called DES:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package org.kazao.tips.tip027;

/**
*
* @author Mr. Kazao
*/
public class DES {

public DES() {
}

public static void main(String... args) {
new DES();
}
}
2.Defining a DES key, atleast 8 character:
byte[] key = "MyPassword".getBytes(); // DES key atleast 8 character
3. Defining plaintext:
byte[] plaintext = "Who are you?".getBytes();
4. Creating SecreeyKey:
SecretKey sKey = SecretKeyFactory.getInstance("DES").generateSecret(new DESKeySpec(key));
5.Instanting cipher using DES algorithm:
Cipher cipher = Cipher.getInstance("DES");
6. Initialize cipher for encryption:
cipher.init(Cipher.ENCRYPT_MODE, sKey);
7. Encrypt plaintext into ciphertext:
byte[] ciphertext = cipher.doFinal(plaintext);
8. Initialize cipher for decryption:
cipher.init(Cipher.DECRYPT_MODE, sKey);
9. Decrypt ciphertext into plaindecrypt:
byte[] plaindecrypt = cipher.doFinal(ciphertext);
10. Printing to STDOUT:
System.out.println("Plain Text     : "+convert(plaintext));
System.out.println("Cipher Text : "+convert(ciphertext));
System.out.println("Plain Decrypted: "+convert(plaindecrypt));
11. Here the result:
run-single:
Plain Text : 57 68 6f 20 61 72 65 20 79 6f 75 3f
Cipher Text : 8d fa 05 d2 3f 4a bc cc 09 ff 46 e3 27 c3 7b 84
Plain Decrypted: 57 68 6f 20 61 72 65 20 79 6f 75 3f
BUILD SUCCESSFUL (total time: 10 seconds)
Conclusion:
To encrypt data using DES is simple, create cipher DES instance, init for encrypt or decrypt mode and finally encrypt or decrypt using doFinal method
Complete Source:

* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package org.kazao.tips.tip027;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;

/**
*
* @author Mr. Kazao
*/
public class DES {

public DES() {
byte[] key = "MyPassword".getBytes(); // DES key atleast 8 character
try {
// defining plaintext
byte[] plaintext = "Who are you?".getBytes();
// creating SecretKey
SecretKey sKey = SecretKeyFactory.getInstance("DES").generateSecret(new DESKeySpec(key));
// instanting cipher using DES algorithm
Cipher cipher = Cipher.getInstance("DES");
// initialize cipher for encryption
cipher.init(Cipher.ENCRYPT_MODE, sKey);
// encryption
byte[] ciphertext = cipher.doFinal(plaintext);
// initialize cipher for decryption
cipher.init(Cipher.DECRYPT_MODE, sKey);
// decryption
byte[] plaindecrypt = cipher.doFinal(ciphertext);
// printing to STDOUT
System.out.println("Plain Text : "+convert(plaintext));
System.out.println("Cipher Text : "+convert(ciphertext));
System.out.println("Plain Decrypted: "+convert(plaindecrypt));
} catch (Exception e) {
}
}

private int byte2int(byte data) {
return data < 0 ? 256 + data : data;
}

private String convert(byte[] data) {
StringBuffer buffer = new StringBuffer();
for (byte b : data) {
String s = Integer.toHexString(byte2int(b));
if (s.length() < 2) {
buffer.append("0" + s);
} else {
buffer.append(s);
}
buffer.append(" ");
}
return buffer.toString();
}

public static void main(String... args) {
new DES();
}
}

+++Read More+++Summary only+++View Complete Source +++Hide Complete Source+++
Diposting oleh M. Jumari di Monday, September 08, 2008 4 komentar
Label: English, Java, Tips and Tricks, Tutorial

Antivirus

Biasanya aku pasang antivirus pada PC atau Laptop dan itupun harus diinstall, tapi kali ini antivirus itu harus aku makan.

Ceritanya begini, awal puasa badan sudah terasa kurang enak badan istilah jawanya "nggregesi" akhirnya pada hari senin setelah berbuka seperti biasanya untuk menguatkan stamina aku minum supradin, pada hari selasa kayaknya tubuh semakin gak enak aja akhirnya didouble dosisnya, sore habis buka minum satu dan setelah sahur minum satu lagi (mohon jangan ditiru karena seharusnya dosisnya satu hari maksimal satu).

Pada hari rabu tanda-tanda serangan virus ini mulai tampak, akhir sore hari kuputuskan untuk periksa ke dokter ternyata positif terkena virus, nama virusnya adalah "Varicella-zoster" atau kata lainnya cacar air.

Oleh dokter diberi antivirus "Acyclovir 400" dengan dosis 3 kali sehari, setelah sampe rumah tak lupa juga konsultasi dengan adikku yang juga calon dokter, dan ada revisi dosis dari 3 kali sehari menjadi 4 kali sehari. Untuk 4 kali sehari 10 tablet pil hanya bisa untuk 2 hari akhirnya kuputuskan untuk membeli tambahan antivirus yang sama di apotek.

Lima hari Kamis, Jumat, Sabtu, Minggu dan Senin intensif minum obat ini, pada hari ke 3 (Sabtu) badan sudah terasa agak enak, sudah tidak ada lagi benjolan-benjolan yang tumbuh ditubuh. Hari Minggu adalah masa penyembuhan.

Pada hari minggu malam keadaan tubuhku sudah sangat baik, artinya benjolan-benjolan di wajah sudah tinggal sedikit akhirnya kuputuskan Senin untuk masuk kantor lagi.

Setelah lima hari puasa bolong karena harus minum obat akhirnya hari ini sudah berpuasa lagi, tapi sayang sudah pasang weker jam 3.30 tapi bangunnya baru jam 4.15 menit-menit saat imsyak datang dan kuputuskan untuk tidak sahur.

Terima kasih buat penemu dan pembuat Acyclovir yang sudah sangat membantu mengobatiku, sehingga virus ini tidak sampai menyebar dan merusak kulit badan dan mukaku.

+++Read More+++Summary only+++View Complete Source +++Hide Complete Source+++
Diposting oleh M. Jumari di Monday, September 08, 2008 4 komentar
Label: Bahasa Indonesia, Curhat

Tuesday, September 2, 2008

Google Chrome: Sebuah Web Browser Open Source Baru Dari Google

Google Chrome:
Rencananya hari ini tanggal 2 September 2008 waktu setempat atau kira2 jam 11 siang WIB Google Chrome akan diluncurkan.

Google menjanjikan konsep baru pada browser ini, baca komiknya disini atau download disini.

Jadi gak sabar pengen mencobanya. :-D


Update:
Setelah kemaren gagal untuk download google chrome karena belum dibuka aksesnya, pagi ini iseng-iseng buka http://www.google.com/chrome ternyata sudah bisa. Dengan segera aku klik "Download Google Chrome" sampai akhirnya proses download selesai.

Ku klik ganda file "ChromeSetup.exe" untuk memulai instalasi, tampaknya file yang didownload adalah hanya sekedar mini installer karena ukurannya cuma 475KB installer sebenarnya mesti harus didownload secara online.

Setelah proses instalasi online selesai ternyata ada proses import bookmark, history, dll (maaf lupa) dari browser lama yang kita gunakan, setelah proses import selesai aku buka aplikasi google chrome ini, ternyata semua bookmark yang ada difirefox berhasil diimport dengan sempurna.

Akhirnya mencoba membuka aplikasi dan web-web yang wajib aku kunjungi semuanya berjalan dengan lancar, sayang kecepatan membuka aplikasi tersebut tidak secepat seperti yang dijanjikan yah ini mungkin dikarenakan koneksi internet kantorku aja yang lambat.

Berikut screenshoot google chrome saat membuka aplikasi dan web wajib.

+++Read More+++Summary only+++View Complete Source +++Hide Complete Source+++
Diposting oleh M. Jumari di Tuesday, September 02, 2008 1 komentar
Label: Bahasa Indonesia, Berita

Monday, September 1, 2008

Mudik Lebaran 1428H


Keterangan Foto:
1. Danau toba dari atas mobil yang sedang menuruni bukit.
2. Pulau Samosir dari atas kapal motor.
3. Batu Gantung di danau toba.
4. Mama di kapal motor menuju Tomok.
5. Vanya di tepi danau toba.
6. Kazao di daerah Porsea.
+++Read More+++Summary only+++View Complete Source +++Hide Complete Source+++
Diposting oleh M. Jumari di Monday, September 01, 2008 0 komentar
Label: Bahasa Indonesia, Foto
Newer Posts Older Posts Home
Subscribe to: Posts (Atom)

About Me

My Photo
My name is M. Jumari @cyberworld i'am also known as Kazao aka Kazao™ aka Mr. Kazao. I'am a simply man enjoying life in a beautifull country.
     All source code you get from here is absolutely free. You may view my complete profile or view my website.

Messenger

m.jumari

Archive

  • April 2009 (6)
  • March 2009 (1)
  • January 2009 (3)
  • December 2008 (4)
  • October 2008 (1)
  • September 2008 (5)
  • August 2008 (12)
  • July 2008 (20)
  • May 2008 (2)

Labels

  • Bahasa Indonesia (29)
  • Berita (1)
  • Cuap-Cuap (3)
  • Curhat (10)
  • English (18)
  • Filosofi (1)
  • Foto (2)
  • Gambar (5)
  • Java (17)
  • Joke (2)
  • JPanel (2)
  • JTable (2)
  • JTextArea (1)
  • JTree (3)
  • Printing (2)
  • Reflection (1)
  • Swing (10)
  • Tips and Tricks (21)
  • Trojan (1)
  • Tutorial (17)
  • Video (1)
  • Virus (1)

Friends

  • Mama Vino
    KBB#18 – French Macarons
    10 hours ago
  • Herman Saksono
    Salt
    1 day ago
  • Tikabanget™
    Kelas Kemewahan Internet ituh..
    5 days ago
  • Obie XP
    819936000 + n detik (n=n+1)
    3 months ago
  • Oktarianti
    Banda Aceh
    4 months ago
  • Herwin Saputra
    YellowNarita and Riyantie
    5 months ago
  • Indra Wahyudi
    Pesan Itu Mengisyaratkan "BAHAYA"
    11 months ago
  • A r l e y N o v a ™
    why marry?
    1 year ago
  • Dony Iswantoro
    Tentang seorang "Pangeran"
    1 year ago
  • Akhmad Fathonih
    Finally, a proper kde 4.2
    1 year ago
  • The Vemo™
    Terima Kasih
    1 year ago
  • Harvey
    Kekuatan Maaf
    2 years ago
  • Teguh Susanto
    Bermain Seks Gunakan Otak....
    2 years ago
Show 5 Show All

Subscribe

Posts
Atom
Posts
All Comments
Atom
All Comments

Ketawa.com

Loading...

World Clocks

Followers

Widgets

Google Groups
Berlangganan ke informatika98
Email:
Kunjungi grup ini
Google Groups
informatics-uii
Kunjungi grup ini
Kazao™

YouTube

Loading...
Copyright © M. Jumari 2008 Powered by www.blogger.com
Visit my web @ mr.kazao.net and you can call me @ +6281904091661 or +622743251763