
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");
}
}
}
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class BelahKetupat {
public static void main(String[] args) throws IOException{
int tinggi, setengahTinggi;
String input;
System.out.println("Masukkan tinggi:");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
input = br.readLine();
tinggi = Integer.parseInt(input);
int l = 1;
if (tinggi%2 == 0) {
setengahTinggi = tinggi/2;
for (int i = 0; i < setengahTinggi; i++) {
for (int j = setengahTinggi; j > i; j--) {
System.out.print(" ");
}
for (int k = 1; k <= l; k++) {
if (k == 1 || k == l) {
System.out.print("*");
} else {
System.out.print(" ");
}
}
l += 2;
System.out.println();
}
l -= 2;
for (int i = 0; i < setengahTinggi; i++) {
for (int j = 0; j <= i; j++) {
System.out.print(" ");
}
for (int j = l; j > 0; j--) {
if (j == 1 || j == l) {
System.out.print("*");
} else {
System.out.print(" ");
}
}
l -= 2;
System.out.println();
}
} else {
setengahTinggi = (tinggi / 2);
for (int i = 0; i < setengahTinggi; i++) {
for (int j = setengahTinggi; j >= i; j--) {
System.out.print(" ");
}
for (int k = 1; k <= l; k++) {
if (k == 1 || k == l) {
System.out.print("*");
} else {
System.out.print(" ");
}
}
l += 2;
System.out.println();
}
for (int i = 0; i < tinggi-setengahTinggi; i++) {
for (int j = 0; j <= i; j++) {
System.out.print(" ");
}
for (int j = l; j > 0; j--) {
if (j == l || j == 1) {
System.out.print("*");
} else {
System.out.print(" ");
}
}
l -= 2;
System.out.println();
}
}
}
}