Biner on Java


import javax.swing.JOptionPane;
class Biner3
{
public static void main(String[] args)
{

int i;
int pilih;
int hasil = 0;
int hasil2 = 0;
String inputUser;
String choice;

do{
inputUser = JOptionPane.showInputDialog(“input biner[max 8 digit]:”);
int[] arrInput = new int[inputUser.length()];
if(inputUser.length() <= 8) {

for(i =0; i<arrInput.length; i++) {
arrInput[i] = Integer.parseInt(inputUser.substring(i, i+1));
}

for(i =0; i<arrInput.length; i++)  {
if ((arrInput[i] != 1) && (arrInput[i] !=0)) {
JOptionPane.showMessageDialog(null,”masukan harus dalam basis 2. Ulangi dari awal !…”);
System.exit(0);
}
}

pilih = Integer.parseInt(JOptionPane.showInputDialog(null,”pilih konversi biner :\n1. Basis 4\n2. Basis 8\n3. Basis 10\n4. Basis 16 \n5. Sign Magnitude \n6. Two Complement”));
switch (pilih)
{
case 1 : int a = Basis10(arrInput);
JOptionPane.showMessageDialog(null,”Basis 4 adalah : “+Basis4(a),”Hasil”,JOptionPane.INFORMATION_MESSAGE);
break;
case 2 : int d = Basis10(arrInput);
JOptionPane.showMessageDialog(null,”Basis 8 adalah : “+Basis8(d),”Hasil”,JOptionPane.INFORMATION_MESSAGE);
break;
case 3 :     JOptionPane.showMessageDialog(null,”Basis 10 adalah : “+Basis10(arrInput),”Hasil”,JOptionPane.INFORMATION_MESSAGE);
break;
case 4 :    int b = Basis10(arrInput);
JOptionPane.showMessageDialog(null,”Basis 16 adalah : “+Basis16(b),”Hasil”,JOptionPane.INFORMATION_MESSAGE);
break;
case 5 :    if (inputUser.length() <= 8)
{
if (arrInput[0] == 0){
JOptionPane.showMessageDialog(null,”Basis 10 dalam SM adalah : “+Basis10sm(arrInput),”Hasil”,JOptionPane.INFORMATION_MESSAGE);
} else
JOptionPane.showMessageDialog(null,”Basis 10 dalam SM adalah : -“+Basis10sm(arrInput),”Hasil”,JOptionPane.INFORMATION_MESSAGE);
} else
JOptionPane.showMessageDialog(null,”Lebih dari 8 digit”,”Error”,JOptionPane.ERROR_MESSAGE);
break;
case 6 : if (arrInput[0] == 1)
{
hasil = -1*arrInput[0]*pangkat(inputUser.length()-1);
hasil2 = Basis10sm(arrInput);
JOptionPane.showMessageDialog(null,hasil+hasil2,”Hasil”,JOptionPane.INFORMATION_MESSAGE);
} else {
hasil2 = Basis10(arrInput);
JOptionPane.showMessageDialog(null,hasil2,”Hasil”,JOptionPane.INFORMATION_MESSAGE); }
break;
default: JOptionPane.showMessageDialog(null,”Hanya 1-6″,”Error”,JOptionPane.ERROR_MESSAGE);
}
}

else {
JOptionPane.showMessageDialog(null,”Lebih dari 8 digit”,”Error”,JOptionPane.ERROR_MESSAGE);
}
choice = JOptionPane.showInputDialog(null,”Mau coba lagi?[Y/T]”,”COBA-COBA”,JOptionPane.QUESTION_MESSAGE);
if (choice.equalsIgnoreCase(“t”)){
System.exit(0);
}
else {
if(!choice.equalsIgnoreCase(“y”)) {
JOptionPane.showMessageDialog(null,”pilihan harus [Y] atau [T] “);
}
}
}

while (choice.equalsIgnoreCase(“Y”)); {System.exit(0);}
}

public static int pangkat (int a) {
int i;
int hasil=1;
for (i=1;i<=a ;i++ )
{
hasil = hasil*2;
}
return hasil;
}

public static int Basis10 (int a[]) {
int i,j;
int hasil=0;
i=0;
for (j=a.length-1;j>=0 ;j– )
{
hasil = hasil + (a[i]*pangkat(j));
i++;
}

return hasil;
}

public static int Basis10sm (int c[]) {
int i,j;
int hasil=0;
i=1;
for (j=c.length-2;j>=0 ;j– ){
hasil = hasil + (c[i]*pangkat(j));
i++;
}
return hasil;
}

public static String Basis4 (int a) {
int sisa;
String output=””;

do
{
sisa = a%4;
a = a/4;
switch (sisa)
{
case 0:    output = “0”+output;
break;
case 1:    output = “1”+output;
break;
case 2:    output = “2”+output;
break;
case 3:    output = “3”+output;
break;
}
}
while (a!=0);

return output;
}

public static String Basis8 (int a) {
int sisa;
String output=””;

do
{
sisa = a%8;
a = a/8;
switch (sisa)
{
case 0:    output = “0”+output;
break;
case 1:    output = “1”+output;
break;
case 2:    output = “2”+output;
break;
case 3:    output = “3”+output;
break;
case 4:    output = “4”+output;
break;
case 5:    output = “5”+output;
break;
case 6:    output = “6”+output;
break;
case 7:    output = “7”+output;
break;
}
}
while (a!=0);

return output;
}
public static String Basis16 (int a) {
int sisa;
String output=””;

do
{
sisa = a%16;
a = a/16;
switch (sisa)
{
case 0:    output = “0”+output;
break;
case 1:    output = “1”+output;
break;
case 2:    output = “2”+output;
break;
case 3:    output = “3”+output;
break;
case 4:    output = “4”+output;
break;
case 5:    output = “5”+output;
break;
case 6:    output = “6”+output;
break;
case 8:    output = “8”+output;
break;
case 9:    output = “9”+output;
break;
case 10:    output = “A”+output;
break;
case 11:    output = “B”+output;
break;
case 12:    output = “C”+output;
break;
case 13:    output = “D”+output;
break;
case 14:    output = “E”+output;
break;
case 15:    output = “F”+output;
break;
}
}
while (a!=0);

return output;
}
}

2 thoughts on “Biner on Java

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s