Syafi'i Blog's'

Rabu, 31 Desember 2008

Terbilang Java






// terbilangjava


/**
* @(#)Text4.java
*
*
* @author
* @version 1.00 2008/12/4
*/

import javax.swing.SwingUtilities;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JLabel;
import java.awt.Rectangle;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JTextArea;

public class TerbilangJava extends JFrame {

private static final long serialVersionUID = 1L;
private JPanel jContentPane = null;
private JLabel jLabel = null;
private JTextField txtBilangan = null;
private JButton btnTerbilang = null;
private JTextArea jTextArea = null;
private StringBuffer sb = new StringBuffer();
// @jve:decl-index=0:
String bilangan[] = new String[] { "", "Satu ", "Dua ", "Tiga ", "Empat ",
"Lima ", "Enam ", "Tujuh ", "Delapan ", "Sembilan ", "Sepuluh ",
"Sebelas " };
private JButton jButton = null;

/**
* This method initializes txtBilangan
*
* @return javax.swing.JTextField
*/
private JTextField getTxtBilangan() {
if (txtBilangan == null) {
txtBilangan = new JTextField();
txtBilangan.setBounds(new Rectangle(135, 15, 166, 31));
}
return txtBilangan;
}

/**
* This method initializes btnTerbilang
*
* @return javax.swing.JButton
*/
private JButton getBtnTerbilang() {
if (btnTerbilang == null) {
btnTerbilang = new JButton();
btnTerbilang.setBounds(new Rectangle(315, 15, 152, 31));
btnTerbilang.setText("Terbilang");
btnTerbilang.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
jTextArea.setText("");
String hasil = generate((double)Integer.parseInt(txtBilangan.getText()));
jTextArea.append(Integer.parseInt(txtBilangan.getText())+" = "+hasil);
txtBilangan.setText("");
sb.delete(0, 1000);
}
});
}
return btnTerbilang;
}

/**
* This method initializes jTextArea
*
* @return javax.swing.JTextArea
*/
private JTextArea getJTextArea() {
if (jTextArea == null) {
jTextArea = new JTextArea();
jTextArea.setBounds(new Rectangle(30, 60, 800, 91));
}
return jTextArea;
}

/**
* This method initializes jButton
*
* @return javax.swing.JButton
*/
private JButton getJButton() {
if (jButton == null) {
jButton = new JButton();
jButton.setBounds(new Rectangle(510, 15, 121, 31));
jButton.setText("About");
jButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
System.out.println("actionPerformed()"); // TODO Auto-generated Event stub actionPerformed()
JOptionPane.showMessageDialog(null, "Ng Elyi Junaidi (ng_elyi@yahoo.co.id) (ngelyi.wordpress.com)");
}
});
}
return jButton;
}

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
SwingUtilities.invokeLater(new Runnable() {
public void run() {
TerbilangJava thisClass = new TerbilangJava();
thisClass.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
thisClass.setVisible(true);
}
});
}

/**
* This is the default constructor
*/
public TerbilangJava() {

super();
initialize();
}

/**
* This method initializes this
*
* @return void
*/
private void initialize() {
this.setSize(900, 207);
this.setContentPane(getJContentPane());
this.setTitle("JFrame");
}

/**
* This method initializes jContentPane
*
* @return javax.swing.JPanel
*/
private JPanel getJContentPane() {
if (jContentPane == null) {
jLabel = new JLabel();
jLabel.setBounds(new Rectangle(15, 15, 106, 31));
jLabel.setText("Bilangan");
jContentPane = new JPanel();
jContentPane.setLayout(null);
jContentPane.add(jLabel, null);
jContentPane.add(getTxtBilangan(), null);
jContentPane.add(getBtnTerbilang(), null);
jContentPane.add(getJTextArea(), null);
jContentPane.add(getJButton(), null);
}
return jContentPane;
}

public String generate(double number) {

// logger.debug(”num {}”, number);

if (number < 12) {
sb.append(bilangan[(int)number]);
}

if (number >= 12 && number < 20) {
generate( number - 10);
sb.append("belas ");
}

if(number >= 20 && number < 100){
generate(number/10);
sb.append("puluh ");
generate(number % 10);
}

if (number >= 100 && number < 200) {
sb.append("seratus ");
generate( number % 100 );
}

if (number >= 200 && number < 1000){
generate(number / 100);
sb.append("ratus ");
generate( number % 100 );
}

if (number >= 1000 && number < 2000){
sb.append("seribu ");
generate( number % 1000 );
}

if(number >= 2000 && number < 1000000){
generate(number/1000);
sb.append("ribu ");
generate( number % 1000);
}

if (number >= 1000000 && number < 1000000000){
generate(number/1000000);
sb.append("juta ");
generate( number % 1000000 );
}

return sb.toString();
}

}

reverensi : syafii-jauhari.awardspace.com

1 komentar:

Arwan_JM mengatakan...

makasih atas pencerahannya gan...tp q bingung jika bilangannya dalam "milyar", "trilyun","kuadriliun",
"kuintiliun","heksiliun" dst
bagaimana ya coz tipe data int gak bisa lebih dari 1 milyar
mohon pencerahan

Template by : auraipank x-template.blogspot.com