/** Creates new form Kalkulator */
String angka="";
double hasil, bil1, bil2;
int pilihan;
public Kalkulator() {
initComponents();
}
private void LayarActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
angka +="1";
Layar.setText(angka);
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
angka +="2";
Layar.setText(angka);
}
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
angka +="3";
Layar.setText(angka);
}
private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
angka +="4";
Layar.setText(angka);
}
private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
angka +="5";
Layar.setText(angka);
}
private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
angka +="6";
Layar.setText(angka);
}
private void jButton7ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
angka +="7";
Layar.setText(angka);
}
private void jButton8ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
angka +="8";
Layar.setText(angka);
}
private void jButton9ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
angka +="9";
Layar.setText(angka);
}
private void jButton0ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
angka +="0";
Layar.setText(angka);
}
private void KomaActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
angka+=".";
Layar.setText(angka);
}
private void SamaDenganActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
switch(pilihan){
case 1:
bil2 = Double.parseDouble(angka);
hasil = bil1+bil2;
angka = Double.toString(hasil);
Layar.setText(angka);
break;
case 2:
bil2 = Double.parseDouble(angka);
hasil = bil1-bil2;
angka = Double.toString(hasil);
Layar.setText(angka);
break;
case 3:
bil2 = Double.parseDouble(angka);
hasil = bil1*bil2;
angka = Double.toString(hasil);
Layar.setText(angka);
break;
case 4:
bil2 = Double.parseDouble(angka);
hasil = bil1/bil2;
angka = Double.toString(hasil);
Layar.setText(angka);
break;
case 5:
//hasil = Double.toBinaryString(bil1);
bil2=Double.parseDouble(angka);
hasil = bil1%bil2;
angka=Double.toString(hasil);
Layar.setText(angka);
break;
case 6:
bil2=Double.parseDouble(angka);
hasil=Math.pow(bil1, bil2);
angka=Double.toString(hasil);
Layar.setText(angka);
break;
case 7:
binokthex();
break;
case 8:
binokthex();
break;
case 9:
binokthex();
break;
case 10:
binokthex();
break;
default:
break;
}
}
public class biner {
public biner() {
}
public String Biner(int biner){
String hasil="";
hasil +=(biner%2);
while(true){
if(biner>1){
biner /=2;
hasil +=(biner%2);
}else{
break;
}
}
String a="";
for (int i = hasil.length(); i >0 ; i--) {
a +=hasil.charAt(i-1);
}
return a;
}
public String Debiner(String debiner){
int hasil=0;int pangkat=0;
for (int i = debiner.length(); i > 0; i--) {
if(debiner.charAt(i-1)=='1' || debiner.charAt(i-1)=='0'){
String a=""+debiner.charAt(i-1);
hasil +=Integer.parseInt(a) * (int)(Math.pow(2, pangkat));
pangkat++;
}
}
return ""+hasil;
}
}
public class oktal {
public oktal() {
}
public String Octal (int octal){
String hasil="";
hasil +=(octal%8);
while(true){
octal /=8;
if(octal>0){
hasil +=(octal%8);
}else{
break;
}
}
String a="";
for (int i = hasil.length(); i >0 ; i--) {
a +=hasil.charAt(i-1);
}
return a;
}
public String Deoctal(String deoctal){
int hasil=0;int pangkat=0;
for (int i = deoctal.length(); i > 0; i--) {
String a=""+deoctal.charAt(i-1);
hasil +=Integer.parseInt(a) * (int)(Math.pow(8, pangkat));
pangkat++;
}
return ""+hasil;
}
}
public class hex {
public hex() {
}
String bilanganHexa(int desimal){
int a=desimal%16;
if(a==10)
return "A";
else if (a==11)
return "B";
else if (a==12)
return "C";
else if (a==13)
return "D";
else if (a==14)
return "E";
else if (a==15)
return "F";
else if (a==16)
return "G";
else
return ""+a;
}
String bilanganHexa(String desimal){
if(desimal.equalsIgnoreCase("A"))
desimal="10";
else if(desimal.equalsIgnoreCase("B"))
desimal= "11";
else if(desimal.equalsIgnoreCase("C"))
desimal= "12";
else if(desimal.equalsIgnoreCase("D"))
desimal= "13";
else if(desimal.equalsIgnoreCase("E"))
desimal= "14";
else if(desimal.equalsIgnoreCase("F"))
desimal= "15";
else if(desimal.equalsIgnoreCase("G"))
desimal= "16";
else
desimal=desimal;
return desimal;
}
public String Hexa(int hex){
String hasil="";
hasil +=bilanganHexa(hex);
while(true){
if(hex>1){
hex /=16;
hasil +=bilanganHexa(hex);
} else {
break;
}
}
String a="";
for (int i = hasil.length(); i >0; i--) {
a +=hasil.charAt(i-1);
}
return a;
}
public int dehexa(String Desimal){
int hasil=0;
for (int i = 0; i < Desimal.length(); i++) {
hasil +=Integer.parseInt(bilanganHexa(""+Desimal.charAt((Desimal.length()-1)-i)))*(Math.pow(16, i));
}
return hasil;
}
}
void binokthex(){
biner bin=new biner();
hex hexa=new hex();
oktal okt=new oktal();
String lay=Layar.getText();
if(oper.equalsIgnoreCase("bin") && oper1.equalsIgnoreCase("hex")){
String desi=bin.Debiner(lay);
Layar.setText(hexa.Hexa(Integer.parseInt(desi)));
oper="hex";
}else if(oper.equalsIgnoreCase("bin") && oper1.equalsIgnoreCase("okt")){
String desi=bin.Debiner(lay);
Layar.setText(okt.Octal(Integer.parseInt(desi)));
oper="okt";
}else if(oper.equalsIgnoreCase("bin") && oper1.equalsIgnoreCase("Desimal")){
String desi=bin.Debiner(lay);
Layar.setText(desi);
oper="Desimal";
}else if(oper.equalsIgnoreCase("hex") && oper1.equalsIgnoreCase("bin")){
String desi=""+hexa.dehexa(lay);
Layar.setText(bin.Biner(Integer.parseInt(desi)));
oper="bin";
}else if(oper.equalsIgnoreCase("hex") && oper1.equalsIgnoreCase("okt")){
String desi=""+hexa.dehexa(lay);
Layar.setText(okt.Octal(Integer.parseInt(desi)));
oper="okt";
}else if(oper.equalsIgnoreCase("hex") && oper1.equalsIgnoreCase("Desimal")){
String desi=""+hexa.dehexa(lay);
Layar.setText(desi);
oper="Desimal";
}else if(oper.equalsIgnoreCase("okt") && oper1.equalsIgnoreCase("bin")){
String desi=""+okt.Deoctal(lay);
Layar.setText(bin.Biner(Integer.parseInt(desi)));
oper="bin";
}else if(oper.equalsIgnoreCase("okt") && oper1.equalsIgnoreCase("hex")){
String desi=""+okt.Deoctal(lay);
Layar.setText(hexa.Hexa(Integer.parseInt(desi)));
oper="hex";
}else if(oper.equalsIgnoreCase("okt") && oper1.equalsIgnoreCase("Desimal")){
String desi=""+okt.Deoctal(lay);
Layar.setText(desi);
oper="Desimal";
}else if(oper.equalsIgnoreCase("Desimal") && oper1.equalsIgnoreCase("bin")){
String desi=""+bin.Biner(Integer.parseInt(lay));
Layar.setText(desi);
oper="bin";
}else if(oper.equalsIgnoreCase("Desimal") && oper1.equalsIgnoreCase("hex")){
String desi=""+hexa.Hexa(Integer.parseInt(lay));
Layar.setText(desi);
oper="hex";
}
else if(oper.equalsIgnoreCase("Desimal") && oper1.equalsIgnoreCase("okt")){
String desi=""+okt.Octal(Integer.parseInt(lay));
Layar.setText(desi);
oper="okt";
}
}
private void BagiActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
bil1 = Double.parseDouble(angka);
angka="";
pilihan=4;
}
private void KaliActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
bil1=Double.parseDouble(angka);
angka="";
pilihan=3;
}
private void MinusActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
bil1=Double.parseDouble(angka);
angka="";
pilihan=2;
}
private void PlusActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
bil1=Double.parseDouble(angka);
angka="";
pilihan=1;
}
private void jButtonENDActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
System.exit(0);
}
private void jButtonACActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here
angka="";
bil1=0;
bil2=0;
hasil=0;
Layar.setText(" ");
jRadioButton4.setSelected(true);
oper="Desimal";
}
private void jButtonCActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
String a=Layar.getText();
angka=a.substring(0,a.length()-1);
Layar.setText(angka);
}
private void ModulusActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
bil1=Double.parseDouble(angka);
angka="";
pilihan=5;
}
private void PangkatActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
bil1=Double.parseDouble(angka);
angka="";
hasil = Math.pow(bil1,2);
angka=Double.toString(hasil);
Layar.setText(angka);
}
private void jToggleButton2ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
bil1=Double.parseDouble(angka);
angka="";
pilihan=6;
}
String oper="Desimal",oper1="";
private void jRadioButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
pilihan=7;
oper1 ="bin";
}
private void jRadioButton2ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
pilihan=8;
oper1 ="okt";
}
private void jRadioButton3ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
pilihan=9;
oper1 ="hex";
}
private void jRadioButton4ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
pilihan=10; oper1 ="Desimal";
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Kalkulator().setVisible(true);
}
});
}
Posted by 23.13 and have
0
komentar
, Published at
Tidak ada komentar:
Posting Komentar