Fungsi Terbilang

December 27, 2010 Leave a comment

Berikut dibawah ini salah satu contoh program terbilang

 

package percobaan;

/**
*
* @author Yuniar Kurniawan
* @date 26 Desember 2010
*
*/

// Simpan file dengan Nama Terbilang.java

import java.io.IOException;

class NumericHelper
{
public static boolean cekNumber(String paramInput)
{
char [] karakter = paramInput.toCharArray();
for(int i=0;i<paramInput.length();i+=1)
{
if(!Character.isDigit(karakter[i]))
{
return false;
}
}

return true;
}
}

class BacaKeyboard
{
public static String bacaInputan()
{
int karakter;
String out = “”;
boolean selesai = false;

while(!selesai)
{
try
{
karakter = System.in.read();
if(karakter < 0 || (char)karakter == ‘\n’)
selesai = true;
else if((char)karakter != ‘\r’)
out = out + (char)karakter;
}
catch (IOException e)
{
System.err.println(“Ada kesalahan”);
selesai = true;
}
}

return out;
}
}

public class Terbilang
{
public static void main(String [] args)
{
System.out.print(“Input Bilangan¬† : “);
String nilaiInputan = BacaKeyboard.bacaInputan();

if(NumericHelper.cekNumber(nilaiInputan))
System.out.println(“Nilai Terbilang : ” + getProses(nilaiInputan).trim());
else
System.out.println(“Inputan Berupa String”);

}

private static String getProses(String paramInput)
{
StringBuilder out = new StringBuilder();
Integer panjang = paramInput.length();

switch (panjang)
{
case 1: out.append(getSatuan(paramInput));
break;
case 2: out.append(getPuluhan(paramInput));
break;
case 3: out.append(getRatusan(paramInput));
break;
case 4: out.append(getRibuan(paramInput));
break;
case 5: out.append(getPuluhanRibu(paramInput));
break;
case 6: out.append(getRatusanRibu(paramInput));
break;
case 7: out.append(getJuta(paramInput));
break;
default:out.append(getPuluhanJuta(paramInput));
break;
}

out.append(” Rupiah”);

return out.toString();
}

private static String getPuluhanJuta(String paramInput)
{
StringBuilder out = new StringBuilder();

out.append(getPuluhan(paramInput.substring(0,2)));
out.append(” Juta”);
out.append(getRatusanRibu(paramInput.substring(2,8)));

return out.toString();
}

private static String getJuta(String paramInput)
{
StringBuilder out = new StringBuilder();

out.append(getSatuan(paramInput.substring(0,1)));
out.append(” Juta”);
out.append(getRatusanRibu(paramInput.substring(1,7)));

return out.toString();
}

private static String getRatusanRibu(String paramInput)
{
StringBuilder out = new StringBuilder();

out.append(getRatusan(paramInput.substring(0,3)));
out.append(” Ribu”);
out.append(getRatusan(paramInput.substring(3,6)));

return out.toString();
}

private static String getPuluhanRibu(String paramInput)
{
StringBuilder out = new StringBuilder();

out.append(getPuluhan(paramInput.substring(0,2)));
out.append(” Ribu”);
out.append(getRatusan(paramInput.substring(2,5)));

return out.toString();
}

private static String getRibuan(String paramInput)
{
StringBuilder out = new StringBuilder();

if(paramInput.substring(0,1).equals(“1″))
{
out.append(” Seribu”);
out.append(getRatusan(paramInput.substring(1,4)));
}
else
{
out.append(paramInput.substring(0,1));
out.append(” Ribu”);
out.append(getRatusan(paramInput.substring(1,4)));
}

return out.toString();
}

private static String getRatusan(String paramInput)
{
StringBuilder out = new StringBuilder();

if(paramInput.substring(0,1).equals(“0”))
out.append(“”);
else if(paramInput.substring(0,1).equals(“1″))
out.append(” Seratus”);
else
{
out.append(getSatuan(paramInput.substring(0,1)));
out.append(” Ratus”);
}

if(paramInput.substring(1,2).equals(“0”))
out.append(getSatuan(paramInput.substring(2,3)));
else
out.append(getPuluhan(paramInput.substring(1,3)));

return out.toString();
}

private static String getPuluhan(String paramInput)
{
StringBuilder out = new StringBuilder();

if(paramInput.substring(0, 1).equals(“1”))
{
String tmp = paramInput.substring(1,2);
if(tmp.equals(“1″))
out.append(” Sebelas”);
else if(tmp.equals(“0″))
out.append(” Sepuluh”);
else
{
out.append(getSatuan(tmp));
out.append(” Belas”);
}
}
else
{
for(int i=1;i<=paramInput.length();i++)
{
if(i==paramInput.length())
out.append(getSatuan(paramInput.substring(i-1,i)));
else
{
out.append(getSatuan(paramInput.substring(i-1,i)));
out.append(” Puluh”);
}
}
}

return out.toString();
}

private static String getSatuan(String paramInput)
{
String out = “”;

if(paramInput.equals(“0”))
out = “”;
else if(paramInput.equals(“1″))
out = ” Satu”;
else if(paramInput.equals(“2″))
out = ” Dua”;
else if(paramInput.equals(“3″))
out = ” Tiga”;
else if(paramInput.equals(“4″))
out = ” Empat”;
else if(paramInput.equals(“5″))
out = ” Lima”;
else if(paramInput.equals(“6″))
out = ” Enam”;
else if(paramInput.equals(“7″))
out = ” Tujuh”;
else if(paramInput.equals(“8″))
out = ” Delapan”;
else
out = ” Sembilan”;

return out;
}

}

Categories: Java

Hibernate Criteria Language

Berikut dibawah ini beberapa contoh CRITERIA LANGUGE pada hibernate.  Pada contoh-contoh dibawah ini, penulis akan menggunakan 2 buah table Рtable siswa dan table kelas Рuntuk mengaplikasikan criteria language.  Berikut dibawah ini Gambar 1. Table Siswa dan Kelas.

Table Siswa dan Kelas

Gambar 1. Table Siswa dan Kelas

Berikut pemodelan dari kedua table tsb.

1. Table Siswa

@Entity
@Table(name=”Siswa”)

public class Siswa extends Model implements Serializable
{

private static final long serialVersionUID = 257908462529205844L;

@Id
@GeneratedValue
@Column(name=”nis”)
private Integer id;

@Column(name=”namaSiswa”)
private String namaSiswa;

@Column(name=”tglLahir”)
private Date tglLahir;

@Column(name=”sex”)
private boolean sex;

@Column(name=”alamat”)
private String alamat;

@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name=”fk_kelas”)
@LazyToOne(LazyToOneOption.NO_PROXY)
@Fetch(FetchMode.SELECT)
private Kelas kelas;

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public String getNamaSiswa() {
return namaSiswa;
}

public void setNamaSiswa(String namaSiswa) {
this.namaSiswa = namaSiswa;
}

public Date getTglLahir() {
return tglLahir;
}

public void setTglLahir(Date tglLahir) {
this.tglLahir = tglLahir;
}

public boolean getSex() {
return sex;
}

public void setSex(boolean sex) {
this.sex = sex;
}

public String getAlamat() {
return alamat;
}

public void setAlamat(String alamat) {
this.alamat = alamat;
}

public Kelas getKelas() {
return kelas;
}

public void setKelas(Kelas kelas) {
this.kelas = kelas;
}

}

Ascending Or Descending

January 4, 2010 Leave a comment

Berikut dibawah ini contoh kode program pada java untuk mengurutkan nilai pada array secara ascending dan desceding

// Simpan file dengan nama TUKAR.java

class baseClass
{
private Integer [] arrayAscending;
private Integer [] arrayDescending;

public Integer[] getArrayDescending()
{
return arrayDescending;
}

public void setArrayDescending(Integer[] arrayDescending)
{
Integer [] nilai = arrayDescending;

for(int i=0;i<nilai.length;i++)
{
for(int elemen=0;elemen<nilai.length-1;elemen++)
{
if(nilai[elemen]<nilai[elemen+1])
{
Integer temp;
temp = nilai[elemen];
nilai[elemen]=nilai[elemen+1];
nilai[elemen+1] = temp;
}
}
}

this.arrayDescending = nilai;
}

public Integer[] getArrayAscending()
{
return arrayAscending;
}

public void setArrayAscending(Integer[] arrayAscending)
{
Integer [] nilai = arrayAscending;

for(int i=0;i<nilai.length;i++)
{
for(int elemen=0;elemen<nilai.length-1;elemen++)
{
if(nilai[elemen]>nilai[elemen+1])
{
Integer temp;
temp = nilai[elemen];
nilai[elemen]=nilai[elemen+1];
nilai[elemen+1] = temp;
}
}
}

this.arrayAscending = nilai;
}

}

public class TUKAR extends baseClass
{
public static void main(String [] ar)
{
Integer nilai[] = new Integer[5];

nilai[0] = 3;
nilai[1] = 10;
nilai[2] = 72;
nilai[3] = 4;
nilai[4] = 99;

baseClass objBaseClass = new baseClass();

objBaseClass.setKeterangan(“Ascending”);
System.out.println(objBaseClass.getKeterangan());
objBaseClass.setArrayAscending(nilai);

for(Integer data : objBaseClass.getArrayAscending())
System.out.println(data);

System.out.println(“======”);

objBaseClass.setKeterangan(“Descending”);
System.out.println(objBaseClass.getKeterangan());
objBaseClass.setArrayDescending(nilai);

for(Integer data : objBaseClass.getArrayDescending())
System.out.println(data);

}

}

Categories: Java