Pages

Subscribe:

Ads 468x60px

♪ ♥ Get Paid Daily - The Best Sites PTC ♥ ♪
DonkeyMails.com: No Minimum Payout
AyuWage Services - Get Paid to Visits Sites and Complete Surveys

Minggu, 15 Juli 2012

Dasar-dasar GUI Java


Contoh program: guijava.java
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JOptionPane;
import java.awt.GridLayout;
import javax.swing.JFrame;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class guijava implements ActionListener {
JLabel labelBilangan1 = new JLabel(“bilangan 1: “);
JTextField fieldBilangan1 = new JTextField(10);
JLabel labelBilangan2 = new JLabel(“bilangan 2: “);
JTextField fieldBilangan2 = new JTextField(10);
JLabel labelHasil = new JLabel(“hasil: “);
JTextField fieldHasil = new JTextField(10);
JButton buttonTambah = new JButton(“tambah”);
JButton buttonHapus = new JButton(“hapus”);
JPanel panelPenjumlahan = new JPanel();
guijava(){
GridLayout gl = new GridLayout(4,2);
panelPenjumlahan.setLayout(gl);
panelPenjumlahan.add(labelBilangan1);
panelPenjumlahan.add(fieldBilangan1);
panelPenjumlahan.add(labelBilangan2);
panelPenjumlahan.add(fieldBilangan2);
panelPenjumlahan.add(labelHasil);
panelPenjumlahan.add(fieldHasil);
panelPenjumlahan.add(buttonTambah);
panelPenjumlahan.add(buttonHapus);
buttonTambah.addActionListener(this);
buttonHapus.addActionListener(this);
JFrame jendela = new JFrame(“Contoh GUI Java”);
jendela.setContentPane(panelPenjumlahan);
jendela.setSize(400,150);
jendela.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jendela.setVisible(true);
jendela.setResizable(false);
}
public static void main(String[] args) {
guijava contohGUI = new guijava();
}
public void actionPerformed(ActionEvent kejadian) {
try {
if(kejadian.getSource() == buttonTambah) {
int bilangan1 = Integer.parseInt(fieldBilangan1.getText());
int bilangan2 = Integer.parseInt(fieldBilangan2.getText());
int hasil = bilangan1 + bilangan2;
fieldHasil.setText(Integer.toString(hasil));
}
else {
fieldBilangan1.setText(“”);
fieldBilangan2.setText(“”);
fieldHasil.setText(“”);
fieldBilangan1.requestFocus();
}
}
catch(Exception a) {
JOptionPane.showMessageDialog(panelPenjumlahan, “anda salah memasukkan angka.”);
}
}
}
Output program:

Contoh Program Manajemen Database Sederhana dengan Java


Contoh Program: insert.java
import javax.swing.*;
import java.awt.GridLayout;
import java.awt.event.*;
import java.sql.*;
public class insert implements ActionListener {
JLabel labelNama = new JLabel(“nama:”);
JTextField fieldNama = new JTextField(10);
JLabel labelNim = new JLabel(“nim:”);
JTextField fieldNim = new JTextField(10);
JLabel labelProdi = new JLabel(“prodi:”);
JTextField fieldProdi = new JTextField(10);
JLabel labelAlamat = new JLabel(“alamat:”);
JTextField fieldAlamat = new JTextField(10);
JLabel labelKota = new JLabel(“kota:”);
JTextField fieldKota = new JTextField(10);
JButton tombolSimpan = new JButton(“simpan”);
JButton tombolBatal = new JButton(“batal”);
JPanel panel = new JPanel();
insert(){
GridLayout gl = new GridLayout(6,2);
panel.setLayout(gl);
panel.add(labelNim);
panel.add(fieldNim);
panel.add(labelNama);
panel.add(fieldNama);
panel.add(labelProdi);
panel.add(fieldProdi);
panel.add(labelAlamat);
panel.add(fieldAlamat);
panel.add(labelKota);
panel.add(fieldKota);
panel.add(tombolSimpan);
panel.add(tombolBatal);
tombolSimpan.addActionListener(this);
tombolBatal.addActionListener(this);
JFrame frame = new JFrame(“input data”);
frame.setContentPane(panel);
frame.setSize(400,200);
frame.setVisible(true);
//frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
}
public void actionPerformed(ActionEvent event) {
try {
if(event.getSource()==tombolSimpan) {
String nama = fieldNama.getText();
String nim = fieldNim.getText();
String prodi = fieldProdi.getText();
String alamat = fieldAlamat.getText();
String kota = fieldKota.getText();
Class.forName(“com.mysql.jdbc.Driver”);
System.out.println(“driver JDBC ditemukan…”);
//String url = “jdbc:mysql://localhost:3306/dbmahasiswa?user=root&password=t3l0g0dh0g”;
Connection koneksi = DriverManager.getConnection(“jdbc:mysql://localhost:3306/dbmahasiswa”,
“root”,”t3l0g0dh0g”);
System.out.println(“koneksi sukses…”);
Statement statement = koneksi.createStatement();
String sql = “INSERT INTO mahasiswa values (‘”+nim+”‘,’”+nama+”‘,’”+prodi+”‘,’”+alamat+”‘,’”+kota+”‘);”;
statement.executeUpdate(sql);
fieldNama.setText(“”);
fieldNim.setText(“”);
fieldProdi.setText(“”);
fieldAlamat.setText(“”);
fieldKota.setText(“”);
statement.close();
koneksi.close();
JOptionPane.showMessageDialog(panel, “data sukses tersimpan…”);
}
}
catch(Exception e) {
System.out.println(“salah”);
}
if(event.getSource()==tombolBatal){
fieldNama.setText(“”);
fieldNim.setText(“”);
fieldProdi.setText(“”);
fieldAlamat.setText(“”);
fieldKota.setText(“”);
}
}
public static void main(String[] args) {
insert insertRecord = new insert();
}
}
Output program:

tampilan program insert.java
Contoh program: update.java
import javax.swing.*;
import java.awt.GridLayout;
import java.awt.event.*;
import java.sql.*;
public class update implements ActionListener {
JTextField fieldCari = new JTextField(10);
JButton tombolCari = new JButton(“cari”);
JLabel labelNama = new JLabel(“nama:”);
JTextField fieldNama = new JTextField(10);
JLabel labelNim = new JLabel(“nim:”);
JTextField fieldNim = new JTextField(10);
JLabel labelProdi = new JLabel(“prodi:”);
JTextField fieldProdi = new JTextField(10);
JLabel labelAlamat = new JLabel(“alamat:”);
JTextField fieldAlamat = new JTextField(10);
JLabel labelKota = new JLabel(“kota:”);
JTextField fieldKota = new JTextField(10);
JButton tombolUpdate = new JButton(“update”);
JButton tombolBatal = new JButton(“batal”);
JPanel panel = new JPanel();
update(){
GridLayout gl = new GridLayout(7,2);
panel.setLayout(gl);
panel.add(fieldCari);
panel.add(tombolCari);
panel.add(labelNim);
panel.add(fieldNim);
panel.add(labelNama);
panel.add(fieldNama);
panel.add(labelProdi);
panel.add(fieldProdi);
panel.add(labelAlamat);
panel.add(fieldAlamat);
panel.add(labelKota);
panel.add(fieldKota);
panel.add(tombolUpdate);
panel.add(tombolBatal);
tombolCari.addActionListener(this);
tombolUpdate.addActionListener(this);
tombolBatal.addActionListener(this);
JFrame frame = new JFrame(“update data”);
frame.setContentPane(panel);
frame.setSize(400,200);
frame.setVisible(true);
//frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
}
public void actionPerformed(ActionEvent event) {
try {
if(event.getSource() == tombolUpdate) {
String nim = fieldNim.getText();
String nama = fieldNama.getText();
String prodi = fieldProdi.getText();
String alamat = fieldAlamat.getText();
String kota = fieldKota.getText();
Class.forName(“com.mysql.jdbc.Driver”);
System.out.println(“driver JDBC ditemukan…”);
//String url = “jdbc:mysql://localhost:3306/dbmahasiswa?user=root&password=t3l0g0dh0g”;
Connection koneksi = DriverManager.getConnection(“jdbc:mysql://localhost:3306/dbmahasiswa”,
“root”,”t3l0g0dh0g”);
System.out.println(“koneksi sukses…”);
Statement statement = koneksi.createStatement();
String sql= “UPDATE mahasiswa set nama=’”+nama+”‘,prodi=’”+prodi+”‘,alamat=’”+alamat+”‘,kota=’”+kota+”‘ WHERE nim=’”+nim+”‘”;
statement.executeUpdate(sql);
fieldNama.setText(“”);
fieldNim.setText(“”);
fieldProdi.setText(“”);
fieldAlamat.setText(“”);
fieldKota.setText(“”);
statement.close();
koneksi.close();
JOptionPane.showMessageDialog(panel, “data telah ter-update…”);
}
else if(event.getSource() == tombolBatal) {
fieldNama.setText(“”);
fieldNim.setText(“”);
fieldProdi.setText(“”);
fieldAlamat.setText(“”);
fieldKota.setText(“”);
}
else if(event.getSource() == tombolCari) {
String cari = fieldCari.getText();
Class.forName(“com.mysql.jdbc.Driver”);
System.out.println(“driver JDBC ditemukan…”);
//String url = “jdbc:mysql://localhost:3306/dbmahasiswa?user=root&password=t3l0g0dh0g”;
Connection koneksi = DriverManager.getConnection(“jdbc:mysql://localhost:3306/dbmahasiswa”,
“root”,”t3l0g0dh0g”);
System.out.println(“koneksi sukses…”);
Statement statement = koneksi.createStatement();
String sql = “SELECT * FROM mahasiswa WHERE nim LIKE ‘”+cari+”‘”;
ResultSet rs = statement.executeQuery(sql);
if(rs.next()) {
fieldNim.setText(rs.getString(1));
fieldNama.setText(rs.getString(2));
fieldProdi.setText(rs.getString(3));
fieldAlamat.setText(rs.getString(4));
fieldKota.setText(rs.getString(5));
} else {
JOptionPane.showMessageDialog(panel, “nim ” + cari + ” tidak ditemukan”);
}
statement.close();
koneksi.close();
}
}
catch(Exception e) {
System.out.println(“terjadi kesalahan”);
}
}
public static void main(String[] args) {
update deleteRecord = new update();
}
}
Output program:

tampilan program update.java
Contoh program: delete.java
import javax.swing.*;
import java.awt.GridLayout;
import java.awt.event.*;
import java.sql.*;
public class delete implements ActionListener {
JTextField fieldCari = new JTextField(10);
JButton tombolCari = new JButton(“cari”);
JLabel labelNama = new JLabel(“nama:”);
JTextField fieldNama = new JTextField(10);
JLabel labelNim = new JLabel(“nim:”);
JTextField fieldNim = new JTextField(10);
JLabel labelProdi = new JLabel(“prodi:”);
JTextField fieldProdi = new JTextField(10);
JLabel labelAlamat = new JLabel(“alamat:”);
JTextField fieldAlamat = new JTextField(10);
JLabel labelKota = new JLabel(“kota:”);
JTextField fieldKota = new JTextField(10);
JButton tombolHapus = new JButton(“hapus”);
JButton tombolBatal = new JButton(“batal”);
JPanel panel = new JPanel();
delete(){
GridLayout gl = new GridLayout(7,2);
panel.setLayout(gl);
panel.add(fieldCari);
panel.add(tombolCari);
panel.add(labelNim);
panel.add(fieldNim);
panel.add(labelNama);
panel.add(fieldNama);
panel.add(labelProdi);
panel.add(fieldProdi);
panel.add(labelAlamat);
panel.add(fieldAlamat);
panel.add(labelKota);
panel.add(fieldKota);
panel.add(tombolHapus);
panel.add(tombolBatal);
tombolCari.addActionListener(this);
tombolHapus.addActionListener(this);
tombolBatal.addActionListener(this);
JFrame frame = new JFrame(“delete data”);
frame.setContentPane(panel);
frame.setSize(400,200);
frame.setVisible(true);
//frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
}
public void actionPerformed(ActionEvent event) {
try {
if(event.getSource() == tombolHapus) {
String nim = fieldNim.getText();
Class.forName(“com.mysql.jdbc.Driver”);
System.out.println(“driver JDBC ditemukan…”);
//String url = “jdbc:mysql://localhost:3306/dbmahasiswa?user=root&password=t3l0g0dh0g”;
Connection koneksi = DriverManager.getConnection(“jdbc:mysql://localhost:3306/dbmahasiswa”,
“root”,”t3l0g0dh0g”);
System.out.println(“koneksi sukses…”);
Statement statement = koneksi.createStatement();
String sql = “DELETE FROM mahasiswa WHERE nim=’”+nim+”‘”;
statement.executeUpdate(sql);
fieldNama.setText(“”);
fieldNim.setText(“”);
fieldProdi.setText(“”);
fieldAlamat.setText(“”);
fieldKota.setText(“”);
statement.close();
koneksi.close();
JOptionPane.showMessageDialog(panel, “data telah terhapus…”);
}
else if(event.getSource() == tombolBatal) {
fieldNama.setText(“”);
fieldNim.setText(“”);
fieldProdi.setText(“”);
fieldAlamat.setText(“”);
fieldKota.setText(“”);
}
else if(event.getSource() == tombolCari) {
String cari = fieldCari.getText();
Class.forName(“com.mysql.jdbc.Driver”);
System.out.println(“driver JDBC ditemukan…”);
//String url = “jdbc:mysql://localhost:3306/dbmahasiswa?user=root&password=t3l0g0dh0g”;
Connection koneksi = DriverManager.getConnection(“jdbc:mysql://localhost:3306/dbmahasiswa”,
“root”,”t3l0g0dh0g”);
System.out.println(“koneksi sukses…”);
Statement statement = koneksi.createStatement();
String sql = “SELECT * FROM mahasiswa WHERE nim LIKE ‘”+cari+”‘”;
ResultSet rs = statement.executeQuery(sql);
if(rs.next()) {
fieldNim.setText(rs.getString(1));
fieldNama.setText(rs.getString(2));
fieldProdi.setText(rs.getString(3));
fieldAlamat.setText(rs.getString(4));
fieldKota.setText(rs.getString(5));
} else {
JOptionPane.showMessageDialog(panel, “nim ” + cari + ” tidak ditemukan”);
}
statement.close();
koneksi.close();
}
}
catch(Exception e) {
System.out.println(“terjadi kesalahan”);
}
}
public static void main(String[] args) {
delete deleteRecord = new delete();
}
}
Output program:

tampilan program delete.java
Contoh program: gabungan insert, delete, dan hapus (gabung.java)
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.FlowLayout;
public class gabung implements ActionListener {
JMenuBar menu = new JMenuBar();
JMenu menuFile = new JMenu(“File”);
JMenuItem subMenuInsert = new JMenuItem(“Insert Data”,’S’);
JMenuItem subMenuDelete = new JMenuItem(“Delete Data”,’D’);
JMenuItem subMenuUpdate = new JMenuItem(“Update Data”,’U’);
JMenuItem subMenuQuit = new JMenuItem(“Quit”,’Q’);
Container container = new Container();
JPanel panel = new JPanel();
gabung() {
JFrame frame = new JFrame(“Java DataBase Manajemen Sistem”);
frame.setSize(640,480);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setJMenuBar(menu);
menuFile.setMnemonic(‘F’);
subMenuInsert.addActionListener(this);
subMenuDelete.addActionListener(this);
subMenuUpdate.addActionListener(this);
subMenuQuit.addActionListener(this);
menu.add(menuFile);
menuFile.add(subMenuInsert);
menuFile.add(subMenuDelete);
menuFile.add(subMenuUpdate);
menuFile.addSeparator();
menuFile.add(subMenuQuit);
menu.setSize(400,100);
menu.setLocation(60,60);
menu.setVisible(true);
FlowLayout fl = new FlowLayout();
panel.setLayout(fl);
frame.setContentPane(panel);
frame.setVisible(true);
}
public static void main(String[] args) {
gabung gabungan = new gabung();
}
public void actionPerformed(ActionEvent event) {
if(event.getSource() == subMenuInsert) {
frameInsert();
} else if(event.getSource() == subMenuDelete) {
frameDelete();
} else if(event.getSource() == subMenuUpdate) {
frameUpdate();
} else if (event.getSource() == subMenuQuit) {
System.exit(0);
}
}
private void frameInsert() {
insert frameInsert = new insert();
}
private void frameDelete() {
delete frameDelete = new delete();
}
private void frameUpdate() {
update frameUpdate = new update();
}
}
Output program:

Tampilan program gabungan.java

Akses Database Menggunakan Java

Akses Database Menggunakan Java

import javax.swing.*;
import java.awt.GridLayout;
import java.awt.BorderLayout;
import java.awt.event.*;
import java.sql.*;
import javax.swing.table.DefaultTableModel;
public class sisip implements ActionListener {
JLabel labelNama = new JLabel(“nama:”);
JTextField fieldNama = new JTextField(10);
JLabel labelNim = new JLabel(“nim:”);
JTextField fieldNim = new JTextField(10);
JLabel labelProdi = new JLabel(“prodi:”);
JTextField fieldProdi = new JTextField(10);
JLabel labelAlamat = new JLabel(“alamat:”);
JTextField fieldAlamat = new JTextField(10);
JLabel labelKota = new JLabel(“kota:”);
JTextField fieldKota = new JTextField(10);
JButton tombolSimpan = new JButton(“simpan”);
JButton tombolBatal = new JButton(“batal”);
JPanel panelSemua = new JPanel();
JPanel panelAtas = new JPanel();
JPanel panelCenter = new JPanel();
JScrollPane scrollPane = new JScrollPane();
JTable tabel = new JTable();
DefaultTableModel tabMode;
Connection koneksi;
sisip(){
BorderLayout bl = new BorderLayout();
panelSemua.setLayout(bl);
GridLayout gl1 = new GridLayout(6,2);
panelAtas.setLayout(gl1);
panelAtas.add(labelNim);
panelAtas.add(fieldNim);
panelAtas.add(labelNama);
panelAtas.add(fieldNama);
panelAtas.add(labelProdi);
panelAtas.add(fieldProdi);
panelAtas.add(labelAlamat);
panelAtas.add(fieldAlamat);
panelAtas.add(labelKota);
panelAtas.add(fieldKota);
panelAtas.add(tombolSimpan);
panelAtas.add(tombolBatal);
panelSemua.add(“North”, panelAtas);
Object[] row = {“NIM”, “Nama”, “Prodi”, “Alamat”, “Kota”};
tabMode = new DefaultTableModel(null, row);
tabel.setModel(tabMode);
scrollPane.getViewport().add(tabel, null);
panelSemua.add(“Center”, scrollPane);
koneksiDatabase();
tampilDataKeTabel();
tombolSimpan.addActionListener(this);
tombolBatal.addActionListener(this);
JFrame frame = new JFrame(“input data”);
frame.setContentPane(panelSemua);
frame.setSize(400,250);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(true);
}
public void actionPerformed(ActionEvent kejadian) {
try {
if(kejadian.getSource()==tombolSimpan) {
String nama = fieldNama.getText();
String nim = fieldNim.getText();
String prodi = fieldProdi.getText();
String alamat = fieldAlamat.getText();
String kota = fieldKota.getText();
//Class.forName(“com.mysql.jdbc.Driver”);
//System.out.println(“driver JDBC ditemukan…”);
//String url = “jdbc:mysql://localhost:3306/dbmahasiswa?user=root&password=t3l0g0dh0g”;
//Connection koneksi = DriverManager.getConnection(“jdbc:mysql://localhost:3306/dbmahasiswa”,
//                                                 “root”,”t3l0g0dh0g”);
//System.out.println(“koneksi sukses…”);
koneksiDatabase();
Statement perintah = koneksi.createStatement();
String sql = “INSERT INTO mahasiswa VALUES (‘”+nim+”‘,’”+nama+”‘,’”+prodi+”‘,’”+alamat+”‘,’”+kota+”‘);”;
perintah.executeUpdate(sql);
fieldNama.setText(“”);
fieldNim.setText(“”);
fieldProdi.setText(“”);
fieldAlamat.setText(“”);
fieldKota.setText(“”);
//perintah.close();
//koneksi.close();
JOptionPane.showMessageDialog(panelSemua, “data sukses tersimpan…”);
//hapusTabel();
tampilDataKeTabel();
}
}
catch(Exception e) {
System.out.println(“terjadi error…”);
}
if(kejadian.getSource()==tombolBatal){
fieldNama.setText(“”);
fieldNim.setText(“”);
fieldProdi.setText(“”);
fieldAlamat.setText(“”);
fieldKota.setText(“”);
}
}
public void koneksiDatabase() {
try {
Class.forName(“com.mysql.jdbc.Driver”);
System.out.println(“JDBC ditemukan…”);
try {
String url = “jdbc:mysql://localhost:3306/dbmahasiswa?user=root&password=t3l0g0dh0g”;
koneksi = DriverManager.getConnection(url);
System.out.println(“koneksi sukses…”);
}
catch (SQLException se) {
System.out.println(“koneksi gagal = ” + se);
System.exit(0);
}
}
catch (ClassNotFoundException cnfe) {
System.out.println(“class tidak ditemukan… error: ” + cnfe);
System.exit(0);
}
}
public void tampilDataKeTabel() {
hapusTabel();
try {
String sql = “SELECT * FROM mahasiswa”;
Statement stat = koneksi.createStatement();
ResultSet set = stat.executeQuery(sql);
while(set.next()) {
String nim = set.getString(“nim”);
String nama = set.getString(“nama”);
String prodi = set.getString(“prodi”);
String alamat = set.getString(“alamat”);
String kota = set.getString(“kota”);
String[] data = {nim, nama, prodi, alamat, kota};
tabMode.addRow(data);
}
}
catch (Exception e) {
}
}
public void hapusTabel() {
int row = tabMode.getRowCount();
for(int i=0; i<row; i++) {
tabMode.removeRow(0);
}
}
public static void main(String[] args) {
sisip insertRecord = new sisip();
}
}
Output Program:


sebelum sisip record baru

setelah penyisipan record baru

Pencarian Elemen dalam Array

Terdapat dua algoritma pencarian elemen array yaitu algoritma linear search dan binary search. Dalam artikel pertama ini ditulis contoh program yang menggunakan pencarian elemen array dengan algoritma linier search. Ada dua contoh program, linear search berbasis teks dan GUI (graphical user interface). Untuk algoritma yang lain akan ditulis pada artikel berikutnya.
LINEAR SEARCH – NON GUI
Nama Program: linearSearch.Java
public class linearSearch {
public static void main(String[] args) {
int array[] = new int[5];
array[0] = 10;
array[1] = 25;
array[2] = 15;
array[3] = 35;
array[4] = 5;
for(int index=0; index<array.length; index++) {
System.out.print(array[index] + ” “);
}
System.out.println(“”);
int cari = 25;
boolean ketemu = false;
for(int index=0; index<array.length; index++) {
if(array[index] == cari){
ketemu = true;
break;
}
}
if(ketemu == true) {
System.out.println(“elemen ” + cari + ” ditemukan…”);
} else {
System.out.println(“elemen ” + cari + ” tidak ditemukan…”);
}
}
}
Output program:
10 25 15 35 5
elemen 25 ditemukan…
LINEAR SEARCH – GUI
Nama Program: LinearSearchGui.java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class LinearSearchGui {
JPanel panelKiri = new JPanel();
JPanel panelKanan = new JPanel();
JPanel panelSemua = new JPanel();
JTextField[] fieldArray = new JTextField[10];
JLabel[] label = new JLabel[10];
JTextField sisipElemen = new JTextField(5);
JButton buttonSisip = new JButton(“Sisip”);
JTextField hapusElemen = new JTextField(5);
JButton buttonHapus = new JButton(“Hapus”);
JTextField cariElemen = new JTextField(5);
JButton buttonCari = new JButton(“Cari”);
LinearSearchGui() {
BorderLayout bl = new BorderLayout();
panelSemua.setLayout(bl);
GridLayout gl = new GridLayout(10,2);
panelKiri.setLayout(gl);
for(int x=0; x<fieldArray.length; x++) {
panelKiri.add(label[x] = new JLabel(Integer.toString(x)));
panelKiri.add(fieldArray[x] = new JTextField(5));
}
panelSemua.add(“Center”,panelKiri);
GridLayout gl2 = new GridLayout(3,2);
panelKanan.setLayout(gl2);
panelKanan.add(sisipElemen);
panelKanan.add(buttonSisip);
panelKanan.add(hapusElemen);
panelKanan.add(buttonHapus);
panelKanan.add(cariElemen);
panelKanan.add(buttonCari);
panelSemua.add(“North”,panelKanan);
JFrame jendela = new JFrame(“Array GUI”);
jendela.setContentPane(panelSemua);
jendela.setSize(200,400);
jendela.setResizable(false);
//jendela.pack();
jendela.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jendela.setVisible(true);
LinearSearchProcess proses = new LinearSearchProcess(this);
buttonSisip.addActionListener(proses);
buttonHapus.addActionListener(proses);
buttonCari.addActionListener(proses);
}
public static void main(String[] args) {
LinearSearchGui linearSearch = new LinearSearchGui();
}
}
Nama Program: LinearSearchProcess.java
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class LinearSearchProcess implements ActionListener {
int array[] = new int[10];
int nElemen = 0;
LinearSearchGui induk;
LinearSearchProcess(LinearSearchGui induk) {
this.induk = induk;
}
public void actionPerformed(ActionEvent kejadian) {
if(kejadian.getSource() == induk.buttonSisip) {
array[nElemen] = Integer.parseInt(induk.sisipElemen.getText());
induk.fieldArray[nElemen].setText(Integer.toString(array[nElemen]));
induk.fieldArray[nElemen].setBackground(Color.green);
induk.sisipElemen.setText(“”);
nElemen++;
tampil();
}
else if(kejadian.getSource() == induk.buttonHapus){
int j;
for(int x=0; x<nElemen; x++) {
induk.fieldArray[x].setText(“”);
}
for(j=0; j<nElemen; j++){
if( Integer.parseInt(induk.hapusElemen.getText()) == array[j] )
break;
}
if(j==nElemen)
JOptionPane.showMessageDialog(null, “tidak ada”);
else
{
for(int k=j; k<nElemen; k++) {
array[k] = array[k+1];
//nElemen–;
}
nElemen–;
induk.fieldArray[nElemen].setBackground(Color.white);
}
induk.hapusElemen.setText(“”);
tampil();
}
else if(kejadian.getSource() == induk.buttonCari) {
int cari = Integer.parseInt(induk.cariElemen.getText());
//int cari = 35;
boolean ketemu = false;
int index;
for(index=0; index<nElemen; index++) {
if(array[index] == cari){
ketemu = true;
break;
}
}
if(ketemu == true) {
JOptionPane.showMessageDialog(induk.panelSemua, “elemen ” + cari + ” ditemukan…”);
} else if (ketemu == false) {
JOptionPane.showMessageDialog(induk.panelSemua,”elemen ” + cari + ” tidak ditemukan…”);
}
induk.cariElemen.setText(“”);
}
}
public void tampil(){
for(int x=0; x<nElemen; x++) {
induk.fieldArray[x].setText(” “);
}
for(int x=0; x<nElemen; x++) {
induk.fieldArray[x].setText(Integer.toString(array[x]));
}
}
}
Output program:
Penyisipan elemen arrayPenyisipan elemen array
Pencarian elemen arrayPencarian elemen array

Pencarian Elemen dalam Array (Binary Search)

BINARY SEARCH – NON GUI
Nama program: binarySearch.java
public class binarySearch {
public static void main(String[] args) {
int array[] = new int[5];
array[0] = 25;
array[1] = 30;
array[2] = 35;
array[3] = 40;
array[4] = 45;
int batasAtas = array.length-1;
int batasBawah = 0;
for(int index = 0 ; index<array.length; index++){
System.out.print(array[index] + ” “);
}
System.out.println(“”);
int cari = 30;
boolean belumKetemu = true;
while(belumKetemu) {
int posisiSekarang = (batasAtas + batasBawah)/2;
if (array[posisiSekarang] == cari) {
belumKetemu=false;
System.out.println(“ditemukan ” + cari);
} else if(batasBawah > batasAtas) {
System.out.println(“tidak ditemukan ” + cari);
break;
}
else {
if (array[posisiSekarang] < cari) {
batasBawah = posisiSekarang + 1;
} else {
batasAtas = posisiSekarang – 1;
}
}
}
}
}
Output program:
25 30 35 40 45
ditemukan 30
BINARY SEARCH – GUI
Nama program: BinarySearchGui.java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class BinarySearchGui {
JPanel panelKiri = new JPanel();
JPanel panelKanan = new JPanel();
JPanel panelSemua = new JPanel();
JTextField[] fieldArray = new JTextField[10];
JLabel[] label = new JLabel[10];
JTextField sisipElemen = new JTextField(5);
JButton buttonSisip = new JButton(“Sisip”);
JTextField hapusElemen = new JTextField(5);
JButton buttonHapus = new JButton(“Hapus”);
JTextField cariElemen = new JTextField(5);
JButton buttonCari = new JButton(“Cari”);
BinarySearchGui() {
BorderLayout bl = new BorderLayout();
panelSemua.setLayout(bl);
GridLayout gl = new GridLayout(10,2);
panelKiri.setLayout(gl);
for(int x=0; x<fieldArray.length; x++) {
panelKiri.add(label[x] = new JLabel(Integer.toString(x)));
panelKiri.add(fieldArray[x] = new JTextField(5));
}
panelSemua.add(“Center”,panelKiri);
GridLayout gl2 = new GridLayout(3,2);
panelKanan.setLayout(gl2);
panelKanan.add(sisipElemen);
panelKanan.add(buttonSisip);
panelKanan.add(hapusElemen);
panelKanan.add(buttonHapus);
panelKanan.add(cariElemen);
panelKanan.add(buttonCari);
panelSemua.add(“North”,panelKanan);
JFrame jendela = new JFrame(“Array GUI”);
jendela.setContentPane(panelSemua);
jendela.setSize(200,400);
jendela.setResizable(false);
//jendela.pack();
jendela.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jendela.setVisible(true);
BinarySearchProcess proses = new BinarySearchProcess(this);
buttonSisip.addActionListener(proses);
buttonHapus.addActionListener(proses);
buttonCari.addActionListener(proses);
}
public static void main(String[] args) {
BinarySearchGui linearSearch = new BinarySearchGui();
}
}
Nama program: BinarySearchProcess.java
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class BinarySearchProcess implements ActionListener{
int array[] = new int[10];
int nElemen = 0;
BinarySearchGui induk;
BinarySearchProcess(BinarySearchGui induk) {
this.induk = induk;
}
public void actionPerformed(ActionEvent kejadian) {
if(kejadian.getSource() == induk.buttonSisip) {
int j;
for(j=0; j<nElemen; j++){
if(array[j] > Integer.parseInt(induk.sisipElemen.getText()))
break;
}
for(int k=nElemen; k>j; k–) {
array[k] = array[k-1];
}
array[j] = Integer.parseInt(induk.sisipElemen.getText());
induk.sisipElemen.setText(“”);
nElemen++;
tampil();
}
else if(kejadian.getSource() == induk.buttonHapus){
int j;
for(int x=0; x<nElemen; x++) {
induk.fieldArray[x].setText(“”);
}
for(j=0; j<nElemen; j++){
if( Integer.parseInt(induk.hapusElemen.getText()) == array[j] )
break;
}
if(j==nElemen)
JOptionPane.showMessageDialog(null, “tidak ada”);
else
{
for(int k=j; k<nElemen; k++) {
array[k] = array[k+1];
//nElemen–;
}
nElemen–;
induk.fieldArray[nElemen].setBackground(Color.white);
}
induk.hapusElemen.setText(“”);
tampil();
}
if(kejadian.getSource() == induk.buttonCari) {
int batasAtas = nElemen-1;
int batasBawah = 0;
int cari = Integer.parseInt(induk.cariElemen.getText());
boolean belumKetemu = true;
while(belumKetemu) {
int posisiSekarang = (batasAtas + batasBawah)/2;
if (array[posisiSekarang] == cari) {
belumKetemu=false;
JOptionPane.showMessageDialog(induk.panelSemua, cari + ” ditemukan”);
} else if(batasBawah > batasAtas) {
JOptionPane.showMessageDialog(induk.panelSemua, cari + ” tidak ditemukan”);
break;
}
else {
if (array[posisiSekarang] < cari) {
batasBawah = posisiSekarang + 1;
} else {
batasAtas = posisiSekarang – 1;
}
}
}
induk.cariElemen.setText(“”);
}
}
public void tampil(){
for(int x=0; x<nElemen; x++) {
induk.fieldArray[x].setText(” “);
}
for(int x=0; x<nElemen; x++) {
induk.fieldArray[x].setText(Integer.toString(array[x]));
}
}
}
Output program:
Pencarian elemen array
Pencarian elemen array

Array (Sorting)

methode rekursif adalah perulangan dengan menggunakan teknik memanggil methode itu sendiri. reskursif tidak mengunakan teknik perulangan iteratif (misalnya for atau while). berikut contoh programnya:SORTING – NON GUI
Nama program: bubbleSort.java


public class BubbleSort {

public static void main(String[] args) {

int array[] = new int[5];

array[0] = 7;
array[1] = 5;

array[2] = 3;

array[3] = 8;

array[4] = 2;

for(int index=0; index<array.length; index++) {

System.out.print(array[index] + ” “);

 }

System.out.println(“”);<!--more-->

int kanan;

int kiri;
for(kanan=array.length-1; kanan>0; kanan–) {

for(kiri=0; kiri<kanan; kiri++) {

if( array[kiri] > array[kiri+1] ) {

int temp = array[kiri];

array[kiri] = array[kiri+1];

array[kiri+1] = temp;

}

}
}
Output program:
7 5 3 8 2
2 3 5 7 8

Nama program: SelectionSort.java

<strong>
</strong>

public class SelectionSort {

public static void main(String[] args) {

int array[] = new int[5];

for(int index=0; index<array.length; index++) {

array[index] = (int) (Math.random()*100);

 }

for(int index=0; index<array.length; index++) {

System.out.print(array[index] + ” “);

}

System.out.println();
18
19int x, y, min;
20 for(x=0; x<array.length; x++) {
21 min = array[x];
22 for(y=x+1; y<array.length; y++) {
23 if(array[y] < array[x]) {
24 min = array[y];
25 int temp = min;
26 array[y] = array[x];
27 array[x] = temp;
28 }
29 }
30 }
31
32for(int index=0; index<array.length; index++) {
33 System.out.print(array[index] + ” “);
34 }
35 System.out.println();
36 }
37
38}
Output program:
9 69 96 97 47
9 47 69 96 97
Nama program: InsertionSort.java
01public class InsertionSort {
02
03public static void main(String[] args) {
04
05int array[] = new int[5];
06 array[0] = 7;
07 array[1] = 5;
08 array[2] = 3;
09 array[3] = 8;
10 array[4] = 2;
11
12int a;
13 int b;
14
15for(int index=0; index<array.length; index++) {
16 System.out.print(array[index] + ” “);
17 }
18 System.out.println(“”);
19
20for(a=1; a<array.length; a++)
21 {
22 int temp = array[a];
23 b = a;
24 while(b>0 && array[b-1] >= temp)
25 {
26 array[b] = array[b-1];
27 –b;
28 }
29 array[b] = temp;
30 }
31
32for(int index=0; index<array.length; index++) {
33 System.out.print(array[index] + ” “);
34 }
35 System.out.println(“”);
36 }
37 }
Output program:
7 5 3 8 2
2 3 5 7 8
SORTING – GUI

Nama program: SortingGui.java
01import javax.swing.*;
02 import java.awt.*;
03 import java.awt.event.ActionListener;
04 import java.awt.event.ActionEvent;
05
06public class SortingGui {
07
08JPanel panelTengah = new JPanel();
09 JPanel panelKanan = new JPanel();
10 JPanel panelBawah = new JPanel();
11 JPanel panelSemua = new JPanel();
12 JTextField[] fieldArray = new JTextField[10];
13 JLabel[] label = new JLabel[10];
14 JTextField sisipElemen = new JTextField(5);
15 JButton buttonSisip = new JButton(“Sisip”);
16 JTextField hapusElemen = new JTextField(5);
17 JButton buttonHapus = new JButton(“Hapus”);
18 JButton buttonLinearSort = new JButton(“Linear Sort”);
19 JButton buttonInsertionSort = new JButton(“Insertion Sort”);
20 JButton buttonBubbleSort = new JButton(“Bubble Sort”);
21
22SortingGui() {
23 BorderLayout bl = new BorderLayout();
24 panelSemua.setLayout(bl);
25
26GridLayout gl = new GridLayout(10, 2);
27 panelTengah.setLayout(gl);
28 for (int x = fieldArray.length-1; x >=0; x–) {
29 panelTengah.add(label[x] = new JLabel(Integer.toString(x)));
30 panelTengah.add(fieldArray[x] = new JTextField(5));
31 }
32 panelSemua.add(“Center”, panelTengah);
33
34GridLayout gl2 = new GridLayout(2, 2);
35 panelKanan.setLayout(gl2);
36 panelKanan.add(sisipElemen);
37 panelKanan.add(buttonSisip);
38 panelKanan.add(hapusElemen);
39 panelKanan.add(buttonHapus);
40 panelSemua.add(“North”, panelKanan);
41
42GridLayout gl3 = new GridLayout(2, 2);
43 panelBawah.setLayout(gl3);
44 panelBawah.add(buttonLinearSort);
45 panelBawah.add(buttonInsertionSort);
46 panelBawah.add(buttonBubbleSort);
47 panelSemua.add(“South”, panelBawah);
48
49JFrame jendela = new JFrame(“Sorting GUI”);
50 jendela.setContentPane(panelSemua);
51 //jendela.setSize(200,400);
52 //jendela.setResizable(false);
53 jendela.pack();
54 jendela.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
55 jendela.setVisible(true);
56
57ProsesSorting proses = new ProsesSorting(this);
58 buttonSisip.addActionListener(proses);
59 buttonHapus.addActionListener(proses);
60 buttonLinearSort.addActionListener(proses);
61 buttonInsertionSort.addActionListener(proses);
62 buttonBubbleSort.addActionListener(proses);
63 }
64
65public static void main(String[] args) {
66 SortingGui sort = new SortingGui();
67 }
68 }
Nama program: ProsesSorting
001import java.awt.Color;
002 import javax.swing.JOptionPane;
003 import java.awt.event.*;
004 public class ProsesSorting implements ActionListener {
005
006SortingGui induk;
007 int array[] = new int[10];
008 int nElemen = 0;
009
010ProsesSorting(SortingGui induk) {
011 this.induk = induk;
012 }
013
014public void actionPerformed(ActionEvent kejadian) {
015
016if(kejadian.getSource() == induk.buttonSisip) {
017 array[nElemen] = Integer.parseInt(induk.sisipElemen.getText());
018 //int sisip = Integer.parseInt(induk.sisipElemen.getText());
019 induk.fieldArray[nElemen].setText(Integer.toString(array[nElemen]));
020 induk.fieldArray[nElemen].setBackground(Color.green);
021 induk.sisipElemen.setText(“”);
022 nElemen++;
023 tampil();
024 }
025 else if(kejadian.getSource() == induk.buttonHapus){
026 int j;
027
028for(int x=0; x<nElemen; x++) {
029 induk.fieldArray[x].setText(“”);
030 }
031
032for(j=0; j<nElemen; j++){
033 if( Integer.parseInt(induk.hapusElemen.getText()) == array[j] )
034 break;
035 }
036 if(j==nElemen)
037 JOptionPane.showMessageDialog(null, “tidak ada”);
038 else
039 {
040 for(int k=j; k<nElemen; k++) {
041 array[k] = array[k+1];
042 //nElemen–;
043 }
044 nElemen–;
045 induk.fieldArray[nElemen].setBackground(Color.white);
046 }
047 induk.hapusElemen.setText(“”);
048 tampil();
049 } else if(kejadian.getSource() == induk.buttonLinearSort) {
050 for(int x = 0; x<nElemen-1; x++) {
051 for(int y = x+1; y<nElemen; y++) {
052 if(array[x] > array[y]){
053 int temp = array[x];
054 array[x] = array[y];
055 array[y] = temp;
056 }
057 }
058 }
059 tampil();
060 } else if(kejadian.getSource() == induk.buttonInsertionSort) {
061 int in, out;
062 for(out=1; out<nElemen; out++)
063 {
064 int temp = array[out];
065 in = out;
066 while(in>0 && array[in-1] >= temp)
067 {
068 array[in] = array[in-1];
069 –in;
070 }
071 array[in] = temp;
072 }
073 tampil();
074
075} else if(kejadian.getSource() == induk.buttonBubbleSort) {
076 for (int x = nElemen-1; x > 1; x–) {
077 for (int y = 0; y < x; y++) {
078 if (array[y] > array[y + 1]) {
079 int temp = array[y];
080 array[y] = array[y + 1];
081 array[y + 1] = temp;
082 }
083 }
084 }
085 tampil();
086 }
087
088}
089
090public void tampil(){
091 for(int x=0; x<nElemen; x++) {
092 induk.fieldArray[x].setText(” “);
093 }
094
095for(int x=0; x<nElemen; x++) {
096 induk.fieldArray[x].setText(Integer.toString(array[x]));
097 }
098 }
099
100}
Output program:
Elemen array sebelum diurutkan
Elemen array sebelum diurutkan
Elemen array setelah diurutkan
Elemen array setelah diurutkan
Unique 1:1 Traffic Exchange

♪ ♥ Ma liste de blogs ♥ ♪

♪ ♥ Please Votes! Thanks. ♪ ♥
×