Berikut saya sajikan penerapan salah satu algoritma sorting yang bernama bubble sort
===============================================================================
uses crt;
var
index : byte;
last_compare_at : byte;
n,i,temp : byte;
a : array [1..6] of byte;
begin
n:=6;
clrscr;
for i:=1 to n do
begin
write('Masukkan angka ke - ',i);
write(' : ');
readln(a[i]);
end;
writeln('ini hasil sorting menggunakan bubble sort');
for last_compare_at:=n-1 downto 1 do
begin
for index:=1 to last_compare_at do
begin
if(a[index] > a[index + 1]) then
begin
temp := a[index];
a[index] := a[index + 1];
a[index + 1] := temp;
end;
end;
end;
for i:=1 to n do
begin
write(a[i]);
write(' ');
end;
readln;
end.
Selasa, 20 Januari 2009
Source code binary search (dalam pascal)
Berikut saya sajikan tentang penerapan algoritma binary Search. Di mana binary search bekerja saat data sudah diurutkan terlebih dahulu.
===================================================================================
uses crt;
var
a : array[1..10] of byte;
low, high, mid, n, i, x : byte;
found : boolean;
begin
// n menunjukkan banyaknya data yang hendak dimasukkan ke dalam array
n := 10;
low:=1;
high:=n;
found := false;
clrscr;
for i:=1 to 10 do
begin
write('Angka ke- ',i);
write(' : ');
readln(a[i]);
end;
write('Masukkan target yang dicari : ');
readln(x);
while ((low <= high) and (found = false)) do
begin
mid := low + ((high-low) div 2);
if (a[mid] = x) then found:=true
else
if (a[mid] < x) then
low := mid + 1
else
if(a[mid] > x) then
high := mid;
end;
writeln('Data diketemukan di index ke : ',mid);
readln;
end.
===================================================================================
uses crt;
var
a : array[1..10] of byte;
low, high, mid, n, i, x : byte;
found : boolean;
begin
// n menunjukkan banyaknya data yang hendak dimasukkan ke dalam array
n := 10;
low:=1;
high:=n;
found := false;
clrscr;
for i:=1 to 10 do
begin
write('Angka ke- ',i);
write(' : ');
readln(a[i]);
end;
write('Masukkan target yang dicari : ');
readln(x);
while ((low <= high) and (found = false)) do
begin
mid := low + ((high-low) div 2);
if (a[mid] = x) then found:=true
else
if (a[mid] < x) then
low := mid + 1
else
if(a[mid] > x) then
high := mid;
end;
writeln('Data diketemukan di index ke : ',mid);
readln;
end.
Kamis, 15 Januari 2009
Source code Java enkripsi file sederhana
Berikut enkripsi file sederhana misal anda punya file yang berisi karakter abc disimpan dalam file coba1.txt kemudian anda ingin mengenkripsikan dengan mengganti karakter abc menjadi bcd dan disimpan dengan nama file lain...
Berikut listing code-nya :
==================================================================================
import java.io.*;
public class EnkripsiFile
{
public EnkripsiFile(String sumber, String tujuan, int angka)
{
FileInputStream fis = null;
try
{
fis = new FileInputStream(sumber);
} catch(FileNotFoundException ex)
{
System.out.println("File Not Found");
}
try
{
char data;
int temp, tampung;
FileOutputStream fos;
fos = new FileOutputStream(tujuan);
do
{
//ingat fis.reaad() mengambil kode unicode per karakter
temp = fis.read();
tampung = temp+angka;
//ubah kode ascii jadi huruf
data = (char)tampung;
try
{
fos.write(data);
} catch(FileNotFoundException ex)
{
System.out.println("File cannot opened for writing");
}
} while(temp != -1);
} catch(IOException ex)
{
System.out.println("Problem");
}
}
public static void main(String args[]) throws IOException
{
System.out.println("How many number do you wanto skip ?");
String Strangka;
BufferedReader bl = new BufferedReader(new InputStreamReader(System.in));
Strangka = bl.readLine();
//cara konvert dari kelas String ke tipe integer
int angka = Integer.parseInt(Strangka);
System.out.println("What is the name of the source file ?");
String sumber;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
sumber = br.readLine();
System.out.println("Enter data to write to "+sumber+"...");
System.out.println("What is the name of the aim file ?");
String tujuan;
BufferedReader bd = new BufferedReader(new InputStreamReader(System.in));
tujuan = bd.readLine();
System.out.println("Enter data to write to "+tujuan+"...");
new EnkripsiFile(sumber,tujuan,angka);
}
}
Berikut listing code-nya :
==================================================================================
import java.io.*;
public class EnkripsiFile
{
public EnkripsiFile(String sumber, String tujuan, int angka)
{
FileInputStream fis = null;
try
{
fis = new FileInputStream(sumber);
} catch(FileNotFoundException ex)
{
System.out.println("File Not Found");
}
try
{
char data;
int temp, tampung;
FileOutputStream fos;
fos = new FileOutputStream(tujuan);
do
{
//ingat fis.reaad() mengambil kode unicode per karakter
temp = fis.read();
tampung = temp+angka;
//ubah kode ascii jadi huruf
data = (char)tampung;
try
{
fos.write(data);
} catch(FileNotFoundException ex)
{
System.out.println("File cannot opened for writing");
}
} while(temp != -1);
} catch(IOException ex)
{
System.out.println("Problem");
}
}
public static void main(String args[]) throws IOException
{
System.out.println("How many number do you wanto skip ?");
String Strangka;
BufferedReader bl = new BufferedReader(new InputStreamReader(System.in));
Strangka = bl.readLine();
//cara konvert dari kelas String ke tipe integer
int angka = Integer.parseInt(Strangka);
System.out.println("What is the name of the source file ?");
String sumber;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
sumber = br.readLine();
System.out.println("Enter data to write to "+sumber+"...");
System.out.println("What is the name of the aim file ?");
String tujuan;
BufferedReader bd = new BufferedReader(new InputStreamReader(System.in));
tujuan = bd.readLine();
System.out.println("Enter data to write to "+tujuan+"...");
new EnkripsiFile(sumber,tujuan,angka);
}
}
Jumat, 12 Desember 2008
Source Code Java penggunaan JFrame
import javax.swing.*;
import java.awt.*;
class SwingDemo
{
JFrame frame;
JPanel panel;
JTextField textfield;
JButton button;
Container contentPane;
void launchFrame()
{
//inisialisasi
frame = new JFrame("My First Swing Application");
panel = new JPanel();
textfield = new JTextField("Default Text");
button = new JButton("Click Me !");
contentPane = frame.getContentPane();
//Menambahkan komponen-komponen ke panel menggunakan FlowLayout sebagai default
panel.add(textfield);
panel.add(button);
//menambahkan komponen-komponen content pane menggunakan BorderLayout
contentPane.add(panel, BorderLayout.CENTER);
frame.pack();
//menyebabkan ukuran frame menjadi dasar pengaturan komponen
frame.setVisible(true);
}
public static void main(String args[])
{
SwingDemo sd = new SwingDemo();
sd.launchFrame();
}
}
Source code diambil dari Java Education Network Indonesia(JENI)
import java.awt.*;
class SwingDemo
{
JFrame frame;
JPanel panel;
JTextField textfield;
JButton button;
Container contentPane;
void launchFrame()
{
//inisialisasi
frame = new JFrame("My First Swing Application");
panel = new JPanel();
textfield = new JTextField("Default Text");
button = new JButton("Click Me !");
contentPane = frame.getContentPane();
//Menambahkan komponen-komponen ke panel menggunakan FlowLayout sebagai default
panel.add(textfield);
panel.add(button);
//menambahkan komponen-komponen content pane menggunakan BorderLayout
contentPane.add(panel, BorderLayout.CENTER);
frame.pack();
//menyebabkan ukuran frame menjadi dasar pengaturan komponen
frame.setVisible(true);
}
public static void main(String args[])
{
SwingDemo sd = new SwingDemo();
sd.launchFrame();
}
}
Source code diambil dari Java Education Network Indonesia(JENI)
Source Code Java penggunaan Flow Layout, BorderLayout, Panel
import java.awt.*;
class ComplexLayout extends Frame
{
public static void main(String args[])
{
ComplexLayout cl = new ComplexLayout();
Panel panelNorth = new Panel();
Panel panelCenter = new Panel();
Panel panelSouth = new Panel();
//panel utara
//panel menggunakan FlowLayout sebagai default
panelNorth.add(new Button("ONE"));
panelNorth.add(new Button("TWO"));
panelNorth.add(new Button("THREE"));
//panel tengah
panelCenter.setLayout(new GridLayout(4,4));
panelCenter.add(new TextField("1st"));
panelCenter.add(new TextField("2nd"));
panelCenter.add(new TextField("3rd"));
panelCenter.add(new TextField("4th"));
//Panel Selatan
panelSouth.setLayout(new BorderLayout());
panelSouth.add(new Checkbox("Choose Me !"), BorderLayout.CENTER);
panelSouth.add(new Checkbox("I am here !"), BorderLayout.EAST);
panelSouth.add(new Checkbox("Pick Me !"), BorderLayout.WEST);
//menambahkan panel pada kontainer Frame
//Frame menggunakan BorderLayout sebagai frame
cl.add(panelNorth, BorderLayout.NORTH);
cl.add(panelCenter, BorderLayout.CENTER);
cl.add(panelSouth, BorderLayout.SOUTH);
cl.setSize(300,300);
cl.setVisible(true);
}
}
Source code diambil dari Java Education Network Indonesia (JENI).
class ComplexLayout extends Frame
{
public static void main(String args[])
{
ComplexLayout cl = new ComplexLayout();
Panel panelNorth = new Panel();
Panel panelCenter = new Panel();
Panel panelSouth = new Panel();
//panel utara
//panel menggunakan FlowLayout sebagai default
panelNorth.add(new Button("ONE"));
panelNorth.add(new Button("TWO"));
panelNorth.add(new Button("THREE"));
//panel tengah
panelCenter.setLayout(new GridLayout(4,4));
panelCenter.add(new TextField("1st"));
panelCenter.add(new TextField("2nd"));
panelCenter.add(new TextField("3rd"));
panelCenter.add(new TextField("4th"));
//Panel Selatan
panelSouth.setLayout(new BorderLayout());
panelSouth.add(new Checkbox("Choose Me !"), BorderLayout.CENTER);
panelSouth.add(new Checkbox("I am here !"), BorderLayout.EAST);
panelSouth.add(new Checkbox("Pick Me !"), BorderLayout.WEST);
//menambahkan panel pada kontainer Frame
//Frame menggunakan BorderLayout sebagai frame
cl.add(panelNorth, BorderLayout.NORTH);
cl.add(panelCenter, BorderLayout.CENTER);
cl.add(panelSouth, BorderLayout.SOUTH);
cl.setSize(300,300);
cl.setVisible(true);
}
}
Source code diambil dari Java Education Network Indonesia (JENI).
Surce Code Java mengubah Spasi menjadi underscore
Berikut adalah contoh source code Java mengubah karakter spasi dalam file teks menjadi karakter underscore atau "_"
Selamat mencoba
import java.io.*;
public class UbahFile
{
public UbahFile(String sumber, String tujuan)
{
FileInputStream fis = null;
try
{
fis = new FileInputStream(sumber);
}catch(FileNotFoundException ex)
{
System.out.println("File Not Found");
}
try
{
char data;
int temp;
FileOutputStream fos;
fos = new FileOutputStream(tujuan);
do
{
temp = fis.read();
data = (char)temp;
if(temp != -1)
{
try
{
if((char)temp == ' ')
{
data = '_';
fos.write(data);
}else
{
fos.write(data);
}
}catch(FileNotFoundException ex)
{
System.out.println("File Cannot be opened for writing");
}
}
}while(temp != -1);
} catch(IOException ex)
{
System.out.println("Problem for reading of this file");
}
}
public static void main(String args[]) throws IOException
{
System.out.println("What is the name of the source file ?");
String sumber;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
sumber = br.readLine();
System.out.println("Enter data to write to "+sumber+"...");
System.out.println("What is the name of the aim file ?");
String tujuan;
BufferedReader bd = new BufferedReader(new InputStreamReader(System.in));
tujuan = bd.readLine();
System.out.println("Enter data to write to "+tujuan+"...");
new UbahFile(sumber,tujuan);
}
}
Selamat mencoba
import java.io.*;
public class UbahFile
{
public UbahFile(String sumber, String tujuan)
{
FileInputStream fis = null;
try
{
fis = new FileInputStream(sumber);
}catch(FileNotFoundException ex)
{
System.out.println("File Not Found");
}
try
{
char data;
int temp;
FileOutputStream fos;
fos = new FileOutputStream(tujuan);
do
{
temp = fis.read();
data = (char)temp;
if(temp != -1)
{
try
{
if((char)temp == ' ')
{
data = '_';
fos.write(data);
}else
{
fos.write(data);
}
}catch(FileNotFoundException ex)
{
System.out.println("File Cannot be opened for writing");
}
}
}while(temp != -1);
} catch(IOException ex)
{
System.out.println("Problem for reading of this file");
}
}
public static void main(String args[]) throws IOException
{
System.out.println("What is the name of the source file ?");
String sumber;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
sumber = br.readLine();
System.out.println("Enter data to write to "+sumber+"...");
System.out.println("What is the name of the aim file ?");
String tujuan;
BufferedReader bd = new BufferedReader(new InputStreamReader(System.in));
tujuan = bd.readLine();
System.out.println("Enter data to write to "+tujuan+"...");
new UbahFile(sumber,tujuan);
}
}
Rabu, 26 November 2008
Copy file menggunakan library java.io
Berikut adalah source code untuk mengkopi file. Misal lokasi dari file ini di c:\simple java\copyfile.java
kemudian kita compile file copyfile.java dengan cara c:\simple java>javac copyfile.java ,maka akan menghasilkan CopyFile.class kemudian kita jalankan dengan cara
c:\simple java>java CopyFile Halo.txt c:\tes.txt
Output yang akan dihasilkan adalah file tes.txt yang isinya sama persis dengan halo.txt
Lokasi dari tes.txt adalah di folder c.
=======Source code copy file============
import java.io.*;
class CopyFile
{
void copy(String input, String output)
{
FileReader baca;
FileWriter tulis;
int data;
try
{
baca = new FileReader(input);
tulis = new FileWriter(output);
// != -1 artinya setiap array byte maupun character dibaca untuk kemudian ditulis //dari index ke n sampai index ke 0
while((data = baca.read()) != -1)
{
tulis.write(data);
}
baca.close();
tulis.close();
} catch(IOException ie)
{
ie.printStackTrace();
}
}
public static void main(String args[])
{
String inputFile = args[0];
String outputFile = args[1];
CopyFile cf = new CopyFile();
cf.copy(inputFile, outputFile);
}
}
kemudian kita compile file copyfile.java dengan cara c:\simple java>javac copyfile.java ,maka akan menghasilkan CopyFile.class kemudian kita jalankan dengan cara
c:\simple java>java CopyFile Halo.txt c:\tes.txt
Output yang akan dihasilkan adalah file tes.txt yang isinya sama persis dengan halo.txt
Lokasi dari tes.txt adalah di folder c.
=======Source code copy file============
import java.io.*;
class CopyFile
{
void copy(String input, String output)
{
FileReader baca;
FileWriter tulis;
int data;
try
{
baca = new FileReader(input);
tulis = new FileWriter(output);
// != -1 artinya setiap array byte maupun character dibaca untuk kemudian ditulis //dari index ke n sampai index ke 0
while((data = baca.read()) != -1)
{
tulis.write(data);
}
baca.close();
tulis.close();
} catch(IOException ie)
{
ie.printStackTrace();
}
}
public static void main(String args[])
{
String inputFile = args[0];
String outputFile = args[1];
CopyFile cf = new CopyFile();
cf.copy(inputFile, outputFile);
}
}
Langganan:
Postingan (Atom)