Tuesday, 25 May 2010

Contoh Program Linked List dalam Bahasa Java

class Link
{
public int iData; // data item (key)
public double dData; // data item
public Link next; // next link in list
public Link(int id, double dd) // constructor
{
iData = id; // initialize data
dData = dd; // ('next' is automatically
} // set to null)

public void displayLink() // display ourself
{
System.out.print("{" + iData + ", " + dData + "} ");
}
} // end class Link
class LinkList
{
private Link first; // ref to first link on list
public LinkList() // constructor
{
first = null; // no items on list yet
}
public boolean isEmpty() // true if list is empty
{
return (first==null);
}
public void insertFirst(int id, double dd)
{ // make new link
Link newLink = new Link(id, dd);
newLink.next = first; // newLink --> old first
first = newLink; // first --> newLink
}
public Link deleteFirst() // delete first item
{ // (assumes list not empty)
Link temp = first; // save reference to link
first = first.next; // delete it: first-->old next
return temp; // return deleted link
}
public void displayList()
{
System.out.print("List (first-->last): ");
Link current = first; // start at beginning of list
while(current != null) // until end of list,
{
current.displayLink(); // print data
current = current.next; // move to next link
}
System.out.println("");
}
} // end class LinkList
class LinkListApp
{
public static void main(String[] args)
{
LinkList theList = new LinkList(); // make new list
theList.insertFirst(22, 2.99); // insert four items
theList.insertFirst(44, 4.99);
theList.insertFirst(66, 6.99);
theList.insertFirst(88, 8.99);
theList.displayList(); // display list
while( !theList.isEmpty() ) // until it's empty,
{
Link aLink = theList.deleteFirst(); // delete link
System.out.print("Deleted "); // display it
aLink.displayLink();
System.out.println("");
}
theList.displayList(); // display list
} // end main()
} // end class LinkListApp

// sumber: Data Structures and Algorithm, Sam Publishing

Monday, 24 May 2010

Search Engine Optimizer (SEO)

Sudah hampir satu bulan ini mencoba SEO. Konon katanya bisa menaikkan rating web di www.alexa.com. Awalnya cuma coba-coba saja. Ingin membuktikan. Ternyata ada benarnya. Sebelum memakai jasa SEO, rating web blog ini dulu ada di nomer 5 jutaan. Hari ini saya lihat di Alexa Toolbar sudah mencapai 2.006.310. Luar Biasa! Hanya dalam waktu sekitar satu bulan sudah bisa naik 3.000.000-an peringkat. Memang traffic pengunjung mengalami peningkatan. Dari yang biasanya 20-30an perhari, sekarang menjadi rata-rata di atas 50 pengunjung sehari. Masih sedikit memang, tapi Alhamdulillah. Artikel yang saya post di web blog ini cukup dicari oleh yang membutuhkannya. Semoga bermanfaat :).

Sekarang sedang menunggu-nunggu, kapan bisa tembus 1.000.000-an. Dan syukur-syukur bisa lebih baik lagi. Dukung ya! :)

Tuesday, 11 May 2010

Contoh Program OOP dengan Inheritance Sederhana dalam Bahasa Java

import java.io.*;
import java.util.Scanner;
class makanan
{
public int porsi;
public double harga;
}
class nasi extends makanan
{
public void setharga()
{
harga = 1000;
}
}
class lauk extends makanan
{
public void setharga()
{
harga = 2000;
}
}
class sayur extends makanan
{
public void setharga(double h) //set harga lewat input user
{
harga = h;
}
public double getharga() //mengambil info harga dr class
{
return(harga);
}
}
public class program //utk menjalankan program utama
{
public static void main(String[] args)
{
double h;
sayur s = new sayur(); //create object utk masing2 class
lauk l = new lauk();
nasi n = new nasi();
Scanner sc = new Scanner(System.in);

System.out.print("Harga : ");
h = sc.nextDouble();
s.setharga(h);
System.out.println("Harga sayur :"+s.getharga());
n.harga = 1000; //akses langsung ke atribut
n.porsi = 2;
biaya = n.harga * n.porsi;
System.out.println("Biaya :"+biaya);

//menu program dan manipulasi object yg lain
//bisa diletakkan di sini
}
}

Monday, 10 May 2010

Contoh Program Queue Array Sederhana dalam Java

import java.io.*;
public class queue_array
{
private int maxsize; //maksimum ukuran queue
private double [] queuearray; //array untuk menyimpan queue
private int front; //indeks elemen terdepan
private int rear; //indeks elemen paling belakang
private int jumlah; //menyimpan jumlah total elemen yang ada

public void inisiasi(int s) //inisialisasi queue
{
maxsize = s;
queuearray = new double [maxsize];
front = rear = jumlah = 0;
}
public void enqueue(double data)
{
if (jumlah==maxsize)
System.out.println("Queue Penuh. "+data+" Tidak Bisa Masuk");
else
{
//System.out.print(rear+" ");
queuearray[rear] = data;
rear = (rear+1)%maxsize;
jumlah++;
System.out.println(data +" Masuk ke Queue");
}
}
public double dequeue()
{
double temp;
if (jumlah==0)
{
System.out.println("Queue Sudah Kosong");
return(-1);
}
else
{
temp = queuearray[front];
front = (front+1)%maxsize;
jumlah--;
System.out.println(temp + " Keluar dari Queue");
return (temp);
}
}
public void view()
{
System.out.print("Isi Queue: ");
if (jumlah > 0 && front < rear) //belum terjadi circular
for(int i=front; i0 && rear <= front) //terjadi circular
{
for(int i=front; i<=maxsize-1; i++) //bagian depan
System.out.print(queuearray[i] + " ");
for(int i=0; i<rear; i++) //bagian belakang
System.out.print(queuearray[i] + " ");
}
System.out.println();
}
public static void main(String[] args)
{
queue_array queue = new queue_array();
queue.inisiasi(3);
queue.enqueue(2);
queue.enqueue(5);
queue.enqueue(3);
queue.enqueue(7);
queue.view();
queue.dequeue();
queue.dequeue();
queue.dequeue();
queue.dequeue();
queue.view();
queue.enqueue(8);
queue.enqueue(9);
queue.enqueue(7);
queue.enqueue(10);
queue.view();
queue.dequeue();
queue.view();
}
}

//Selamat Mencoba ^_^

Download Source Code

Contoh Program Stack Array Sederhana dalam Java

import java.io.*;
public class stack_array
{
private int maxsize; //penentu batas elemen stack maksimum
private double [] stackarray; //array untuk menyimpan stack
private int top; //indeks array
public void inisiasi(int s) //menentukan ukuran kapasitas stack
{
maxsize = s;
stackarray = new double [maxsize];
top = -1;
}
public void push(double data)
{
if (top>=maxsize-1)
System.out.println("Stack Penuh. "+data+" Tidak Bisa Masuk");
else
{
top++;
stackarray[top] = data;
System.out.println(data +" Masuk ke Stack");
}
}
public double pop()
{
double temp;
if (top>=0)
{
temp = stackarray[top];
System.out.println(temp + " Keluar dari Stack");
top--;
return (temp);
}
else
{
System.out.println("Stack Sudah Kosong");
return(-1);
}
}
public void view()
{
System.out.print("Isi Stack: ");
for(int i=0; i<=top; i++)
System.out.print(stackarray[i] + " ");
System.out.println();
}
public static void main(String[] args)
{
stack_array stack = new stack_array();
stack.inisiasi(3);
stack.push(3);
stack.push(4);
stack.push(2);
stack.view();
stack.push(5);
stack.push(1);
stack.pop();
stack.pop();
stack.view();
stack.pop();
stack.pop();
stack.pop();
stack.push(6);
stack.push(8);
stack.push(7);
stack.push(9);
stack.pop();
stack.view();
}
}

//Selamat mencoba ^_^

Tuesday, 22 December 2009

Contoh Program Java Swing Sederhana

Berikut ini adalah sebuah contoh implementasi library swing pada bahasa Java untuk membuat program dengan tampilan GUI (Graphical User Interface). Program sederhana ini adalah tentang penghitungan luas sebuah persegi. User diminta memasukkan data panjang dan lebar, kemudian akan dihitung dan ditampilkan luasnya setelah user menekan tombol "Hitung Luas". Project ini terdiri atas dua class yang terpisah, yaitu class persegi dan class hitung. Class persegi berfungsi untuk pengaturan tampilan antarmuka program, sedangkan class hitung berperan sebagai action yang akan dilakukan sebagai respon terjadinya penekanan tombol oleh user. Berikut adalah source code lengkapnya. Dan terima kasih atas kunjungannya. Semoga bermanfaat :)

isi class persegi:
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.Container;
import java.awt.FlowLayout;
class persegi {
public static void main(String args[]) {
//deklarasikan komponen2
JFrame frame;
Container contentPane;
JTextField tfpanjang, tflebar, tfluas;
JButton button;
FlowLayout layout;
frame = new JFrame();
//inisialisasi/ setting tiap2 komponen
frame.setTitle("Program Perhitungan Luas Persegi");
contentPane = frame.getContentPane();
tfpanjang = new JTextField("Masukkan panjang", 10);
tflebar = new JTextField("Masukkan lebar", 10);
tfluas = new JTextField("", 10);
button = new JButton("Hitung Luas");
//sambungkan button dgn perintah yg akan dijalankan
//kirimkan komponen2 ke action listener
button.addActionListener(new hitung(tfpanjang,tflebar,tfluas));
//rakitkan komponen2
contentPane.add(tfpanjang);
contentPane.add(tflebar);
contentPane.add(button);
contentPane.add(tfluas);
layout = new FlowLayout();
contentPane.setLayout(layout);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
}

isi class hitung:
import javax.swing.JTextField;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
class hitung implements ActionListener {
JTextField tp,tl,tf;
hitung(JTextField tfp,JTextField tfl,JTextField tfL) {
//tampung data dari class container ke local
//by refference
tp = tfp;
tl = tfl;
tf = tfL;
}
public void actionPerformed(ActionEvent e) {
int temp,p,l;
//tampung isi textfield ke string,
//kemudian ekstrak data menjadi angka.
String sp = tp.getText();
p = Integer.parseInt(sp);
String sl = tl.getText();
l = Integer.parseInt(sl);
temp = p*l; //hitung luas persegi
//kirimkan hasil temp ke tf
//tf & tfL adalah identik, yaitu text field Luas
//di class pemanggil actionlistener
tf.setText(String.valueOf(temp));
}
}

Friday, 11 December 2009

Another Example about Insert and Delete Linked List

Below is another example about linked list implementation using C++ language. In this source code there are three basic operation described, those are insertion, deletion and show the nodes inside the linked list. You are recommended to modify some unpractical part of the code, one of it is insertion part. The insertion part will be better if you use an iteration to make it more efficient. Thanks for visiting :)

#include "iostream.h"
#include "string.h"
struct node{
char name [20];
int distance; //data
struct node *next;//pointer to point to the next node
};
typedef node * nodeptr;
void insert(nodeptr &head, int data, char nama []){
nodeptr temp;
temp=new node;
temp->distance=data;
strcpy(temp->name,nama);
temp->next=NULL;
if (head==NULL)
head=temp;
else{
temp->next=head;
head=temp;
}
};
char * delnode(nodeptr &head){

nodeptr temp;
char hold[20];

if (head!=NULL) {
temp=head;
strcpy(hold,temp->name);
head=temp->next;
delete temp;
return hold;}
else{
cout<<"Link list is empty"<<endl; return(0);}
}

void main()
{ nodeptr head=NULL, ptr;
int jarak;
char kota [20];
insert(head, 15, "surabaya");
insert(head, 12, "mojokerto");
insert(head, 23, "lamongan");
cout<>kota;
cout<>jarak;
insert(head,jarak,kota);
//show the begining
for(ptr=head; ptr!=NULL; ptr=ptr->next)
cout<distance<<" "<name<<endl;
//delete the front node
strcpy(kota,delnode(head));
cout<<"deleted = " <<kota<next)
cout<distance<<" "<name<<endl;
}