Sunday 26 December 2010

Contoh Program Java: Membaca Isi File Teks

import java.io.*;
public class baca_file {
public static void main(String arg[])
{
String file = "c://program1.txt"; //lokasi file
FileInputStream in = null;
try {
in = new FileInputStream(file);//
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
String line = null;
while ((line = reader.readLine()) != null) {
System.out.println(line);
in.close();
}
} catch (IOException x) {
}
}
}

Thursday 23 December 2010

Rangkuman Software Testing

Testing perangkat lunak bertujuan untuk memastikan bahwa perangkat lunak yang dibuat dapat beroperasi/ berfungsi sebagaimana yang diinginkan oleh customer. Selain itu, testing juga dapat dipandang sebagai kegiatan untuk menemukan errors yang mungkin masih tersembunyi. Testing umumnya membutuhkan resource antara 30-40% dari total usaha yang dilakukan dalam proyek. Untuk software yang berkaitan erat dengan nyawa banyak manusia, biaya testing bisa mencapai 3-5 kali lipat software biasa.

Contoh Program C++: OOP Rekening Bank

#include "iostream.h"
#include "string.h"
#include "stdlib.h"

struct deposit {
double nominal;
struct deposit *next;
};//
typedef struct deposit *simpul;
class rekening_bank {
protected:
char nama[20];
double saldo;
simpul deposits;
int nomer_rek;
public:
void transaksi()
{
double trans=0.0;
simpul baru=NULL;
cout<<"Saldo Anda : "<<saldo<<endl;
cout<>trans;
if (trans<0) //penarikan
{
if ((abs(trans)+5000) nominal = trans;
baru->next = NULL;
if (deposits == NULL)
deposits = baru;
else
{
baru->next = deposits;
deposits = baru;
}
}
else
cout<<"Saldo tidak mencukupi"<nominal = trans;
baru->next = NULL;
if (deposits == NULL)
deposits = baru;
else
{
baru->next = deposits;
deposits = baru;
}
}
};
void cetak()
{
simpul temp=deposits;
cout<<"Saldo Anda : "<<saldo<<endl;
cout<<"Rekap Transaksi :"<next)
if(temp->nominal < 0)
cout<<"Penarikan "<nominal)<<" + 5000 [biaya]"<<endl;
else cout<<"Setoran "<nominal<<endl;
}
};
class cek : public rekening_bank
{
private:
public:
cek(double d) {
cout<<"Rekening cek dibuat..."<<endl;
deposits=NULL;
saldo=d;
};
};
class tabungan : public rekening_bank
{
private:
float bunga;
public:
tabungan(double d)
{
cout<<"Rekening tabungan dibuat..."<<endl;
deposits=NULL;
saldo=d;
};
void hitung_bunga()
{
simpul baru=NULL;
cout<>bunga;
baru = (simpul) malloc(sizeof(struct deposit));
baru->nominal = saldo*bunga;
baru->next = NULL;
saldo = saldo + (saldo*bunga);
if (deposits == NULL)
deposits = baru;
else
{
baru->next = deposits;
deposits = baru;
}
cout<<"saldo sekarang = "<<saldo<<endl;
}
};
void main()
{
cek c(100000);
c.transaksi();
c.cetak();
c.transaksi();
c.cetak();
tabungan b(130000);
b.transaksi();
b.cetak();
b.hitung_bunga();
b.cetak();
//lebih baik dibuatkan menu program
}

Download Source Code

Wednesday 22 December 2010

Contoh Program C++: Konversi Heksa-Desimal

#include "iostream.h"
#include "string.h"
#include "math.h"
#include "stdlib.h"
#include "stdio.h"

class hexa {
int decimal;
public:
hexa() {decimal=0;};
hexa(int d) {decimal=d;};//

Maaf, berhubung banyak karakter source code yang autodelete oleh WP, maka silahkan download source code.

Setelah sukses di-download, ubahlah ekstensi file dari .doc menjadi .cpp. Semoga bermanfaat :)

Monday 20 December 2010

Contoh Program Java: Menjalankan Aplikasi Exe Lain

Aplikasi yang dibuat memakai bahasa Java bisa digunakan untuk memanggil/ menjalankan aplikasi executable lain. Misalnya membuka Notepad.exe, Microsoft Word, Aplikasi Game dsb. Syarat utamanya adalah harus diketahui lokasi/ path file .exe-nya. Library untuk menjalankan proses tersebut adalah java.io. Berikut contoh program untuk menjalankan Microsoft Word (mungkin file path di komputer Anda bisa jadi tidak sama):

import java.io.*;
/**
*
* @author bluejundi
*/
public class java_exec {
public static void main(String[] args) {
String path = new String("E:\\OFFICE\\OFFICE11\\winword.exe");
System.out.println("Menjalankan MS Word");
try {
Process theProcess = Runtime.getRuntime().exec(path);
} catch(IOException e)
{
System.err.println("Error on exec() method");
e.printStackTrace();
}
}
}

Contoh Program Java: Exception Handling

Exception adalah setiap kejadian yang abnormal, tidak diharapkan, dan kondisi yang tidak umum yang mungkin terjadi pada saat run-time program. Java exception handling digunakan untuk menangani kondisi error dalam sebuah program secara sistematis dengan melakukan tindakan yang diperlukan. Dengan adanya exception, maka program akan terhindar dari kondisi hang, atau mati dalam kondisi yang tidak wajar/ tidak jelas. Java exception dimulai dengan kata kunci throw dan ditangani dalam sebuah blok catch. Berikut contoh sederhananya:

public class java_exception {
public static void main(String[] args) {
int result = division(100,0); // Line 2
System.out.println("hasil : "+result);

result = division(100,2);
System.out.println("hasil : "+result);
}
public static int division(int totalSum, int totalNumber) {
int quotient = -1;
System.out.println("Komputasi Pembagian "+totalSum+" dengan "+totalNumber);
try
{ quotient = totalSum/totalNumber; }
catch(Exception e)
{ System.out.println("Pesan Exception : "+ e.getMessage()); }
finally
{ if(quotient != -1)
{ System.out.println("Finally Block Dijalankan");
System.out.println("hasil fungsi : "+ quotient); }
else
{ System.out.println("Finally Block DIjalankan. Terjadi Exception");
System.out.println("hasil fungsi : "+ quotient);
return quotient;
}
}
return quotient;
}
}

Monday 10 May 2010

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 ^_^