
Silahkan download projectnya disini
TableHeader.rar
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.PlainDocument;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author hauw
*/
public class PhoneValidator extends PlainDocument {
private int maxCharacter;
private boolean phoneOnly;
private String phoneChars = "+#*0123456789";
public PhoneValidator() {
this(-1, false);
}
public PhoneValidator(int maxCharacter, boolean phoneOnly) {
this.maxCharacter = maxCharacter;
this.phoneOnly = phoneOnly;
}
@Override
public void insertString(int offs, String str, AttributeSet a) throws BadLocationException {
if (phoneOnly) {
if (maxCharacter == -1) {
if (checkString(str)) {
super.insertString(offs, str, a);
}
} else {
int panjangTextLama = getLength();
int panjangTextBaru = str.length();
if ((panjangTextLama + panjangTextBaru) <= maxCharacter) {
if (checkString(str)) {
super.insertString(offs, str, a);
}
}
}
} else {
if (maxCharacter == -1) {
super.insertString(offs, str, a);
} else {
int panjangTextLama = getLength();
int panjangTextBaru = str.length();
if ((panjangTextLama + panjangTextBaru) <= maxCharacter) {
super.insertString(offs, str, a);
}
}
}
}
private boolean checkString(String input) {
boolean result = false;
for (int i = 0; i < input.length(); i++) {
if (phoneChars.indexOf(input.charAt(i)) == -1) {
result = false;
break;
} else {
result = true;
}
}
return result;
}
}
import javax.swing.JFrame;
import javax.swing.JTextField;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author hauw
*/
public class TestValidasi {
public static void main(String[] args) {
JFrame frame = new JFrame("Test validasi text field");
JTextField textNoTelpon = new JTextField();
textNoTelpon.setDocument(new PhoneValidator(15, true));
frame.add(textNoTelpon);
frame.setVisible(true);
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
public String convertToBinary(int number) {
String binary = "";
while(number != 0) {
binary += number%2;
number /= 2;
}
return new StringBuffer(binary).reverse().toString();
}
public class PerfectNumber {
public static void main(String[] args) {
System.out.println("Perfect numbers from 1 to 650");
for (int i = 1; i < 10000; i++) {
searchPerfectNumber(i);
}
}
public static void searchPerfectNumber(int number) {
int sum = 0;
for (int divisor = 1; divisor < number; divisor++) {
if ((number % divisor) == 0) {
sum += divisor;
}
}
if (sum == number) {
System.out.println(number + " is a perfect number");
}
}
}
public class Fibonaci {
public static void main(String[] args) throws IOException {
String input;
int n = 0;
int temp = 0;
int[] array;
System.out.print("Input: ");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
input = br.readLine();
if (input.equals("fibonaci")) {
System.out.print("Masukkan jumlah n: ");
try {
n = Integer.parseInt(br.readLine());
array = new int[n];
System.out.println("Deret Fibonaci:");
for (int i = 0; i < array.length; i++) {
if (i == 0) {
array[i] = 0;
} else if (i == 1) {
array[i] = 1;
} else {
temp = 0;
for (int j = 0; j < i; j++) {
temp += array[j];
array[i] = temp;
}
}
System.out.println(array[i]);
}
} catch (NumberFormatException numberFormatException) {
System.out.println("Masukkan angka yang benar");
}
} else {
System.out.println("Input Salah");
}
}
}